Skip to content

Commit b7929ab

Browse files
committed
update PL_main_thread on fork()
85e9706 modified the perl signal handler to forward signals to the main thread if it received a signal in a non-perl thread, which required saving the id of the main perl thread. Unfortunately I forgot to handle a possible change in the main thread id on a fork, this fixes that by re-saving the new main thread id immediately after a fork (via pthread_atfork()) On Linux it appears that the main thread id returned by pthread_seld() is constant between processes, but this may not be true on other platforms. Discussed at: #23326 (comment)
1 parent e78ee04 commit b7929ab

File tree

7 files changed

+45
-3
lines changed

7 files changed

+45
-3
lines changed

embed.fnc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,7 @@ Apx |void |apply_attrs_string \
678678
Adp |OP * |apply_builtin_cv_attributes \
679679
|NN CV *cv \
680680
|NULLOK OP *attrlist
681+
CTp |void |atfork_child
681682
CTp |void |atfork_lock
682683
CTp |void |atfork_unlock
683684
Cop |SV ** |av_arylen_p |NN AV *av

embed.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130
# define amagic_deref_call(a,b) Perl_amagic_deref_call(aTHX_ a,b)
131131
# define apply_attrs_string(a,b,c,d) Perl_apply_attrs_string(aTHX_ a,b,c,d)
132132
# define apply_builtin_cv_attributes(a,b) Perl_apply_builtin_cv_attributes(aTHX_ a,b)
133+
# define atfork_child Perl_atfork_child
133134
# define atfork_lock Perl_atfork_lock
134135
# define atfork_unlock Perl_atfork_unlock
135136
# define av_clear(a) Perl_av_clear(aTHX_ a)

ext/ExtUtils-Miniperl/lib/ExtUtils/Miniperl.pm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use Exporter 'import';
55
use ExtUtils::Embed 1.31, qw(xsi_header xsi_protos xsi_body);
66

77
our @EXPORT = qw(writemain);
8-
our $VERSION = '1.14';
8+
our $VERSION = '1.15';
99

1010
# blead will run this with miniperl, hence we can't use autodie or File::Temp
1111
my $temp;
@@ -122,7 +122,7 @@ main(int argc, char **argv, char **env)
122122
* --GSAR 2001-07-20 */
123123
PTHREAD_ATFORK(Perl_atfork_lock,
124124
Perl_atfork_unlock,
125-
Perl_atfork_unlock);
125+
Perl_atfork_child);
126126
#endif
127127
128128
PERL_SYS_FPU_INIT;

ext/XS-APItest/t/thread.t

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!perl
22
use warnings;
33
use strict;
4+
use Test2::IPC;
45
use Test2::Tools::Basic;
56
use Config;
67

@@ -14,4 +15,27 @@ use XS::APItest qw(thread_id_matches);
1415
ok(thread_id_matches(),
1516
"check main thread id saved and is current thread");
1617

18+
# This test isn't too useful on Linux, it passes without the fix.
19+
#
20+
# thread ids are unique only within a process, so it's valid for Linux
21+
# pthread_self() to return the same id for the main thread after a
22+
# fork.
23+
#
24+
# This may be different on other POSIX-likes.
25+
SKIP:
26+
{
27+
$Config{d_fork}
28+
or skip "Need fork", 1;
29+
my $pid = fork;
30+
defined $pid
31+
or skip "Fork failed", 1;
32+
if ($pid == 0) {
33+
ok(thread_id_matches(), "check main thread id is updated by fork");
34+
exit;
35+
}
36+
else {
37+
waitpid($pid, 0);
38+
}
39+
}
40+
1741
done_testing();

miniperlmain.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ main(int argc, char **argv, char **env)
9696
* --GSAR 2001-07-20 */
9797
PTHREAD_ATFORK(Perl_atfork_lock,
9898
Perl_atfork_unlock,
99-
Perl_atfork_unlock);
99+
Perl_atfork_child);
100100
#endif
101101

102102
PERL_SYS_FPU_INIT;

proto.h

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

util.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2872,6 +2872,18 @@ Perl_atfork_unlock(void)
28722872
#endif
28732873
}
28742874

2875+
void
2876+
Perl_atfork_child(void) {
2877+
#ifdef USE_ITHREADS
2878+
/* so we can resend signals received in a non-perl thread to the
2879+
new main thread
2880+
*/
2881+
PTHREAD_INIT_SELF(PL_main_thread);
2882+
#endif
2883+
2884+
Perl_atfork_unlock();
2885+
}
2886+
28752887
/*
28762888
=for apidoc_section $concurrency
28772889
=for apidoc my_fork

0 commit comments

Comments
 (0)