-
Notifications
You must be signed in to change notification settings - Fork 584
Update PL_main_thread on fork() and add more tests #23422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: blead
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ use strict; | |
use warnings; | ||
use Carp; | ||
|
||
our $VERSION = '1.43'; | ||
our $VERSION = '1.44'; | ||
|
||
require XSLoader; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,71 @@ | ||
#!perl | ||
use warnings; | ||
use strict; | ||
use Test2::Tools::Basic; | ||
use Test2::IPC; | ||
use Test2::V0; | ||
use Config; | ||
|
||
BEGIN { | ||
skip_all "Not pthreads or is win32" | ||
if !$Config{usethreads} || $^O eq "MSWin32"; | ||
} | ||
|
||
use XS::APItest qw(thread_id_matches); | ||
use XS::APItest qw(thread_id_matches make_signal_thread join_signal_thread); | ||
|
||
ok(thread_id_matches(), | ||
"check main thread id saved and is current thread"); | ||
|
||
# This test isn't too useful on Linux, it passes without the fix. | ||
# | ||
# thread ids are unique only within a process, so it's valid for Linux | ||
# pthread_self() to return the same id for the main thread after a | ||
# fork. | ||
# | ||
# This may be different on other POSIX-likes. | ||
SKIP: | ||
{ | ||
$Config{d_fork} | ||
or skip "Need fork", 1; | ||
my $pid = fork; | ||
defined $pid | ||
or skip "Fork failed", 1; | ||
if ($pid == 0) { | ||
ok(thread_id_matches(), "check main thread id is updated by fork"); | ||
exit; | ||
} | ||
else { | ||
waitpid($pid, 0); | ||
} | ||
} | ||
|
||
{ | ||
my $saw_signal; | ||
local $SIG{USR1} = sub { ++$saw_signal }; | ||
my $pid = make_signal_thread(); | ||
join_signal_thread($pid); | ||
ok($saw_signal, "saw signal sent to non-perl thread"); | ||
} | ||
|
||
{ | ||
$Config{d_fork} | ||
or skip "Need fork", 1; | ||
my $pid = fork; | ||
defined $pid | ||
or skip "Fork failed", 1; | ||
if ($pid == 0) { | ||
# ensure the main thread saved is valid after fork | ||
my $saw_signal; | ||
local $SIG{USR1} = sub { ++$saw_signal }; | ||
my $pid = make_signal_thread(); | ||
join_signal_thread($pid); | ||
ok($saw_signal, "saw signal sent to non-perl thread in child"); | ||
exit 0; | ||
} | ||
else { | ||
is(waitpid($pid, 0), $pid, "wait child"); | ||
# catches the child segfaulting for example | ||
is($?, 0, "child success"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would |
||
} | ||
} | ||
|
||
done_testing(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,12 +3,26 @@ XS::APItest::PtrTable T_PTROBJ | |
const WCHAR * WPV | ||
U8 * T_PV | ||
|
||
pthread_t T_THREADID | ||
|
||
INPUT | ||
|
||
WPV | ||
$var = ($type)SvPV_nolen($arg); | ||
|
||
T_THREADID | ||
{ | ||
STRLEN len; | ||
const char *pv = SvPVbyte($arg, len); | ||
if (len != sizeof(pthread_t)) | ||
croak(\"Bad thread id for $arg\"); | ||
Copy(pv, &$var, 1, pthread_t); | ||
} | ||
|
||
OUTPUT | ||
|
||
WPV | ||
sv_setpvn($arg, (const char *)($var), sizeof(WCHAR) * (1+wcslen($var))); | ||
|
||
T_THREADID | ||
sv_setpvn($arg, (const char *)&($var), sizeof($var)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see paranoia above with I am very aware this is code is inside XS-APItest, and not some high river CPAN XS module, so XS code "quality" is a very low priority here, but I saw paranoia INPUT: type entry so I'll keep the paranoia going. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2872,6 +2872,18 @@ Perl_atfork_unlock(void) | |
#endif | ||
} | ||
|
||
void | ||
Perl_atfork_child(void) { | ||
#ifdef USE_ITHREADS | ||
/* so we can resend signals received in a non-perl thread to the | ||
new main thread | ||
*/ | ||
PTHREAD_INIT_SELF(PL_main_thread); | ||
#endif | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. More of a Win32 problem than a Posix Perl problem, but what happens to the entire Perl or 3P embedder process if the 1st Perl thread or 1st process startup thread, exits itself on Unix? Instant Process kill? If not, the TID or pthread_t obj stored in PL_main_thread now points to a zombie TID? Why would a child interp/my_perl on a 2nd pthread rethrow a signal to a gone/finished/dealloced root my_perl and dealloced or zombie TID??
|
||
|
||
Perl_atfork_unlock(); | ||
} | ||
|
||
/* | ||
=for apidoc_section $concurrency | ||
=for apidoc my_fork | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why return empty list instead of
&PL_sv_undef
or&PL_sv_no
or SvPV "" empty str or less perfect, SvIV -1 or SvIV 0? I knowpp_entersub
will cast empty list to 1 x&PL_sv_undef
inG_SCALAR
cxt, but why mk `pp_entersub do the cast?