Closed p5pRT closed 9 years ago
For now we have two perl types: threaded and non-threaded. Threaded is linked with threads library\, non-threaded is not. The problem comes when we want to make XS module which will use system threads. In fact this module will crash on all non-threaded perls\, because threads library is not linked with this perl and proper initialization will not be done on perl start. See: http://stackoverflow.com/questions/13587325/threads-vs-pthread-in-perl
Of course someone can use '-A prepend:libswanted="pthread "' to compile non-threaded perl linked with pthreads\, but nobody do this. The only available solution to writer of the module is to say to the user: if it will crash recompile your perl linked with pthreads or use LD_PRELOAD=/path/to/pthreads.so hack.
So\, my proposal is to always link perl with system threads library to allow to write threaded XS modules for all types of perl.
On Sat\, Oct 4\, 2014 at 8:49 AM\, Ðлег Ð \perlbug\-followup@​perl\.org wrote:
# New Ticket Created by Ðлег Ð # Please include the string: [perl #122906] # in the subject line of all future correspondence about this issue. # \<URL: https://rt-archive.perl.org/perl5/Ticket/Display.html?id=122906 >
This is a bug report for perl from oleg@cpan.org\, generated with the help of perlbug 1.39 running under perl 5.14.2.
----------------------------------------------------------------- [Please describe your issue here] For now we have two perl types: threaded and non-threaded. Threaded is linked with threads library\, non-threaded is not. The problem comes when we want to make XS module which will use system threads. In fact this module will crash on all non-threaded perls\, because threads library is not linked with this perl and proper initialization will not be done on perl start. See: http://stackoverflow.com/questions/13587325/threads-vs-pthread-in-perl
Of course someone can use '-A prepend:libswanted="pthread "' to compile non-threaded perl linked with pthreads\, but nobody do this. The only available solution to writer of the module is to say to the user: if it will crash recompile your perl linked with pthreads or use LD_PRELOAD=/path/to/pthreads.so hack.
So\, my proposal is to always link perl with system threads library to allow to write threaded XS modules for all types of perl.
If you can propose how to guarantee that all Perl ops are executed in the main thread\, or\, more to the point\, how Perl's global state is only visible from a single thread\, then yes\, it might make sense to have the non-threaded Perl executable capable of loading and running threaded components. Such a component could really not be properly called a "threaded XS module." XS code does lots of manipulation of Perl's global data\, and the only sanctioned way to do that under threads is to clone most of that data once per thread and then carefully synchronize via mutexes those few remaining bits of global data that are not cloned. Please correct me if I've misunderstood what you're asking.
The RT System itself - Status changed from 'new' to 'open'
Unthreaded perl doesn't need to know about OS threads. If your app is crashing because an unthreaded perl is running in 2 different OS threads simultaneously\, well\, then you need to fix the XS code that passed a function pointer that contains a call_sv() to some non-Perl C function which started a thread\, or ran the function pointer in a thread pool thread. It is completely safe to run a perl interp on different OS threads provided that all perl C calls (and eventually Perl language calls in the runloop) are serialized. They have to be serialized using OS specific APIs (mutex\, critical section\, semaphore\, whatever).
Read http://perlmonks.org/?node_id=973277 and http://perlmonks.org/?node=870516
-- bulk88 ~ bulk88 at hotmail.com
On Sat\, 4 Oct 2014 06:49:35 -0700\, _______ _ (via RT) \perlbug\-followup@​perl\.org wrote:
For now we have two perl types: threaded and non-threaded. Threaded is linked with threads library\, non-threaded is not.
Most of them are not. Some are.
e.g. on HP-UX all perls are linked with -lpthread\, as that is required by some XS (notably Oracle) even if neither is actually using threads
The problem comes when we want to make XS module which will use system threads.
s/use/require/
If an .so is linked with -lpthread\, it needs it for loading\, even if none of the functionality of the lib is used.
In fact this module will crash on all non-threaded perls\, because
s/crash/not load/
threads library is not linked with this perl and proper initialization will not be done on perl start. See: http://stackoverflow.com/questions/13587325/threads-vs-pthread-in-perl
Of course someone can use '-A prepend:libswanted="pthread "'
yes\, that is mentioned in several places in the docs
to compile non-threaded perl linked with pthreads\, but nobody do this.
I do (for all my HP-UX perl distributions)
The only available solution to writer of the module is to say to the user: if it will crash recompile your perl linked with pthreads or use LD_PRELOAD=/path/to/pthreads.so hack.
So\, my proposal is to always link perl with system threads library to allow to write threaded XS modules for all types of perl.
I will not object\, but it should *NOT* block the build if libpthread.$so_ext is not available. As in\, it can be added to $libs_wanted\, but it shall not be a requirement. There are e.g. still systems that have/use pth.sl instead of pthread.so
-- H.Merijn Brand http://tux.nl Perl Monger http://amsterdam.pm.org/ using perl5.00307 .. 5.19 porting perl5 on HP-UX\, AIX\, and openSUSE http://mirrors.develooper.com/hpux/ http://www.test-smoke.org/ http://qa.perl.org http://www.goldmark.org/jeff/stupid-disclaimers/
In fact this report is not about manipulating perl structures in separate thread. Even if your XS code creates thread for its own needs and just calls some functions from libc in this thread it may cause perl to crash if perl itself is not linked with threads library. Example with my module:
use strict; use Net::DNS::Native;
my $dns = Net::DNS::Native->new(pool => 3);
my @sock;
for (1..300) { my $sock = $dns->getaddrinfo("localhost") ; push @sock\, $sock; }
my $buf; for my $sock (@sock) { sysread($sock\, $buf\, 1); $dns->get_result($sock); }
This will work successfully if perl linked with pthreads and always crashes with segfault or "double free or corruption" if not. Furthermore this is not specific to perl. This plain C example will also crash if we will not link mainc.c with pthreads: https://github.com/olegwtf/sandbox/tree/master/threaded-so-for-non-threaded-app In all this examples i use getaddrinfo(3)\, but i think this is not only the case and it will crash with other libc functions too. Moreover\, on NetBSD\, for example\, abort() will be called immediately after your shared library will try to create thread and main app loaded this library is not linked with pthreads.
2014-10-05 9:48 GMT+07:00 Craig Berry via RT \perlbug\-followup@​perl\.org:
On Sat\, Oct 4\, 2014 at 8:49 AM\, Ðлег Ð \perlbug\-followup@​perl\.org wrote:
# New Ticket Created by Ðлег Ð # Please include the string: [perl #122906] # in the subject line of all future correspondence about this issue. # \<URL: https://rt-archive.perl.org/perl5/Ticket/Display.html?id=122906 >
This is a bug report for perl from oleg@cpan.org\, generated with the help of perlbug 1.39 running under perl 5.14.2.
----------------------------------------------------------------- [Please describe your issue here] For now we have two perl types: threaded and non-threaded. Threaded is linked with threads library\, non-threaded is not. The problem comes when we want to make XS module which will use system threads. In fact this module will crash on all non-threaded perls\, because threads library is not linked with this perl and proper initialization will not be done on perl start. See: http://stackoverflow.com/questions/13587325/threads-vs-pthread-in-perl
Of course someone can use '-A prepend:libswanted="pthread "' to compile non-threaded perl linked with pthreads\, but nobody do this. The only available solution to writer of the module is to say to the user: if it will crash recompile your perl linked with pthreads or use LD_PRELOAD=/path/to/pthreads.so hack.
So\, my proposal is to always link perl with system threads library to allow to write threaded XS modules for all types of perl.
If you can propose how to guarantee that all Perl ops are executed in the main thread\, or\, more to the point\, how Perl's global state is only visible from a single thread\, then yes\, it might make sense to have the non-threaded Perl executable capable of loading and running threaded components. Such a component could really not be properly called a "threaded XS module." XS code does lots of manipulation of Perl's global data\, and the only sanctioned way to do that under threads is to clone most of that data once per thread and then carefully synchronize via mutexes those few remaining bits of global data that are not cloned. Please correct me if I've misunderstood what you're asking.
In fact this report is not about manipulating perl structures in separate thread. Even if your XS code creates thread for its own needs and just calls some functions from libc in this thread it may cause perl to crash if perl itself is not linked with threads library. Example with my module:
use strict; use Net::DNS::Native;
my $dns = Net::DNS::Native->new(pool => 3);
my @sock;
for (1..300) { my $sock = $dns->getaddrinfo("localhost"); push @sock\, $sock; }
my $buf; for my $sock (@sock) { sysread($sock\, $buf\, 1); $dns->get_result($sock); }
This will work successfully if perl linked with pthreads and always crashes with segfault or "double free or corruption" if not. Furthermore this is not specific to perl. This plain C example will also crash if we will not link mainc.c with pthreads: https://github.com/olegwtf/sandbox/tree/master/threaded-so-for-non-threaded-app In all this examples i use getaddrinfo(3)\, but i think this is not only the case and it will crash with other libc functions too. Moreover\, on NetBSD\, for example\, abort() will be called immediately after your shared library will try to create thread and main app loaded this library not linked with pthreads.
2014-10-05 12:51 GMT+07:00 bulk88 via RT \perlbug\-followup@​perl\.org:
Unthreaded perl doesn't need to know about OS threads. If your app is crashing because an unthreaded perl is running in 2 different OS threads simultaneously\, well\, then you need to fix the XS code that passed a function pointer that contains a call_sv() to some non-Perl C function which started a thread\, or ran the function pointer in a thread pool thread. It is completely safe to run a perl interp on different OS threads provided that all perl C calls (and eventually Perl language calls in the runloop) are serialized. They have to be serialized using OS specific APIs (mutex\, critical section\, semaphore\, whatever).
Read http://perlmonks.org/?node_id=973277 and http://perlmonks.org/?node=870516
-- bulk88 ~ bulk88 at hotmail.com
Ð ÑообÑении Ð¾Ñ 5 окÑÑбÑÑ 2014 09:48:32 авÑÐ¾Ñ Craig Berry via RT напиÑал:
If you can propose how to guarantee that all Perl ops are executed in the main thread\, or\, more to the point\, how Perl's global state is only visible from a single thread\, then yes\, it might make sense to have the non-threaded Perl executable capable of loading and running threaded components. Such a component could really not be properly called a "threaded XS module." XS code does lots of manipulation of Perl's global data\, and the only sanctioned way to do that under threads is to clone most of that data once per thread and then carefully synchronize via mutexes those few remaining bits of global data that are not cloned. Please correct me if I've misunderstood what you're asking.
In fact this report is not about manipulating perl structures in separate thread. Even if your XS code creates thread for its own needs and just calls some functions from libc in this thread it may cause perl to crash if perl itself is not linked with threads library. Example with my module:
use strict; use Net::DNS::Native;
my $dns = Net::DNS::Native->new(pool => 3);
my @sock;
for (1..300) { my $sock = $dns->getaddrinfo("localhost"); push @sock\, $sock; }
my $buf; for my $sock (@sock) { sysread($sock\, $buf\, 1); $dns->get_result($sock); }
This will work successfully if perl linked with pthreads and always crashes with segfault or "double free or corruption" if not. Furthermore this is not specific to perl. This plain C example will also crash if we will not link mainc.c with pthreads: https://github.com/olegwtf/sandbox/tree/master/threaded-so-for-non-threaded- app In all this examples i used getaddrinfo(3)\, but i think this is not only the case and it will crash with other libc functions too. Moreover\, on NetBSD\, for example\, abort() will be called immediately after your shared library will try to create thread and main app loaded this library is not linked with pthreads.
On Sat\, Oct 4\, 2014 at 3:49 PM\, Ðлег Ð \perlbug\-followup@​perl\.org wrote:
For now we have two perl types: threaded and non-threaded. Threaded is linked with threads library\, non-threaded is not. The problem comes when we want to make XS module which will use system threads. In fact this module will crash on all non-threaded perls\, because threads library is not linked with this perl and proper initialization will not be done on perl start. See: http://stackoverflow.com/questions/13587325/threads-vs-pthread-in-perl
Of course someone can use '-A prepend:libswanted="pthread "' to compile non-threaded perl linked with pthreads\, but nobody do this. The only available solution to writer of the module is to say to the user: if it will crash recompile your perl linked with pthreads or use LD_PRELOAD=/path/to/pthreads.so hack.
So\, my proposal is to always link perl with system threads library to allow to write threaded XS modules for all types of perl.
As far as I know\, that issue is only common on a some platforms\, but I may be mistaken.
There is another reason to do this though: libpthread may override symbols from libc. On current Linux the only example seems to be raise()\, but historically it has included fork (because pthread_atfork AFAICT)\, vfork\, longjmp/siglongjmp and system. In this particular case not linking to pthread in an executable that is indirectly multi-threaded may mean that a signal is raised to the wrong thread in the process. Other similar issues may exist on other platforms.
Leon
Ð ÑообÑении Ð¾Ñ 5 окÑÑбÑÑ 2014 21:10:46 авÑÐ¾Ñ Oleg G напиÑал:
Ð ÑообÑении Ð¾Ñ 5 окÑÑбÑÑ 2014 09:48:32 авÑÐ¾Ñ Craig Berry via RT напиÑал:
If you can propose how to guarantee that all Perl ops are executed in the main thread\, or\, more to the point\, how Perl's global state is only visible from a single thread\, then yes\, it might make sense to have the non-threaded Perl executable capable of loading and running threaded components. Such a component could really not be properly called a "threaded XS module." XS code does lots of manipulation of Perl's global data\, and the only sanctioned way to do that under threads is to clone most of that data once per thread and then carefully synchronize via mutexes those few remaining bits of global data that are not cloned. Please correct me if I've misunderstood what you're asking.
In fact this report is not about manipulating perl structures in separate thread. Even if your XS code creates thread for its own needs and just calls some functions from libc in this thread it may cause perl to crash if perl itself is not linked with threads library. Example with my module:
use strict; use Net::DNS::Native;
my $dns = Net::DNS::Native->new(pool => 3);
my @sock;
for (1..300) { my $sock = $dns->getaddrinfo("localhost"); push @sock\, $sock; }
my $buf; for my $sock (@sock) { sysread($sock\, $buf\, 1); $dns->get_result($sock); }
This will work successfully if perl linked with pthreads and always crashes with segfault or "double free or corruption" if not. Furthermore this is not specific to perl. This plain C example will also crash if we will not link mainc.c with pthreads: https://github.com/olegwtf/sandbox/tree/master/threaded-so-for-non-threaded- app In all this examples i used getaddrinfo(3)\, but i think this is not only the case and it will crash with other libc functions too. Moreover\, on NetBSD\, for example\, abort() will be called immediately after your shared library will try to create thread and main app loaded this library is not linked with pthreads.
Today i played with gdb a little to find out what's going on here: https://github.com/olegwtf/sandbox/tree/master/threaded-so-for-non-threaded-app
And as gdb showed\, app always crashed at rewind.c line 36: http://www.eglibc.org/cgi-bin/viewvc.cgi/branches/eglibc-2_13/libc/libio/rewind.c?annotate=12752 When i grepped elibc source i found two places where this macro was defined: *bits/stdio-lock.h line 49: http://www.eglibc.org/cgi-bin/viewvc.cgi/branches/eglibc-2_13/libc/bits/stdio-lock.h?annotate=12752 *sysdeps/pthread/bits/stdio-lock.h line 91: http://www.eglibc.org/cgi-bin/viewvc.cgi/branches/eglibc-2_13/libc/nptl/sysdeps/pthread/bits/stdio-lock.h?annotate=12752
So\, first is default and second for threaded code. And if i understood correctly this is how it works: 1. when our main app not linked with pthreads it loads default implementation of several macroses (i think not only stdio-lock has dual version) 2. when our threaded shared library loaded it starts using this non threads compatible default implementation 3. since this implementation is not thread safe it crashes
I think other libc implementations has same behavior. And only the case is to link main application (perl in our case) with threads library
P.S. Sorry for duplicates of my previous message. You can delete two of three
On Mon\, Oct 6\, 2014 at 7:34 PM\, Oleg G \verdrehung@​gmail\.com wrote:
Today i played with gdb a little to find out what's going on here:
https://github.com/olegwtf/sandbox/tree/master/threaded-so-for-non-threaded-app
And as gdb showed\, app always crashed at rewind.c line 36: http://www.eglibc.org/cgi-bin/viewvc.cgi/branches/eglibc-2_13/libc/libio/rewind.c?annotate=12752 When i grepped elibc source i found two places where this macro was defined: *bits/stdio-lock.h line 49: http://www.eglibc.org/cgi-bin/viewvc.cgi/branches/eglibc-2_13/libc/bits/stdio-lock.h?annotate=12752 *sysdeps/pthread/bits/stdio-lock.h line 91: http://www.eglibc.org/cgi-bin/viewvc.cgi/branches/eglibc-2_13/libc/nptl/sysdeps/pthread/bits/stdio-lock.h?annotate=12752
So\, first is default and second for threaded code. And if i understood correctly this is how it works: 1. when our main app not linked with pthreads it loads default implementation of several macroses (i think not only stdio-lock has dual version) 2. when our threaded shared library loaded it starts using this non threads compatible default implementation 3. since this implementation is not thread safe it crashes
I think other libc implementations has same behavior. And only the case is to link main application (perl in our case) with threads library
This all makes sense to me.
Leon
Ð ÑообÑении Ð¾Ñ 4 окÑÑбÑÑ 2014 20:49:15 авÑÐ¾Ñ Ðлег РнапиÑал:
This is a bug report for perl from oleg@cpan.org\, generated with the help of perlbug 1.39 running under perl 5.14.2.
----------------------------------------------------------------- [Please describe your issue here] For now we have two perl types: threaded and non-threaded. Threaded is linked with threads library\, non-threaded is not. The problem comes when we want to make XS module which will use system threads. In fact this module will crash on all non-threaded perls\, because threads library is not linked with this perl and proper initialization will not be done on perl start. See: http://stackoverflow.com/questions/13587325/threads-vs-pthread-in-perl
Of course someone can use '-A prepend:libswanted="pthread "' to compile non-threaded perl linked with pthreads\, but nobody do this. The only available solution to writer of the module is to say to the user: if it will crash recompile your perl linked with pthreads or use LD_PRELOAD=/path/to/pthreads.so hack.
So\, my proposal is to always link perl with system threads library to allow to write threaded XS modules for all types of perl.
I made a simple patch. See attachment. Is it correct?
Is this acceptable feature in general?
On Sun\, Oct 12\, 2014 at 3:05 PM\, Oleg G \verdrehung@​gmail\.com wrote:
I made a simple patch. See attachment. Is it correct?
No. It should only do this if there is in fact a libpthread available\, and even then there should be a way to disable it (I imagine embedded environments may not carry libpthread for example)
Leon
On Sun\, 12 Oct 2014 20:05:13 +0700\, Oleg G \verdrehung@​gmail\.com wrote:
Ð ÑообÑении Ð¾Ñ 4 окÑÑбÑÑ 2014 20:49:15 авÑÐ¾Ñ Ðлег РнапиÑал:
This is a bug report for perl from oleg@cpan.org\, generated with the help of perlbug 1.39 running under perl 5.14.2.
----------------------------------------------------------------- [Please describe your issue here] For now we have two perl types: threaded and non-threaded. Threaded is linked with threads library\, non-threaded is not. The problem comes when we want to make XS module which will use system threads. In fact this module will crash on all non-threaded perls\, because threads library is not linked with this perl and proper initialization will not be done on perl start. See: http://stackoverflow.com/questions/13587325/threads-vs-pthread-in-perl
Of course someone can use '-A prepend:libswanted="pthread "' to compile non-threaded perl linked with pthreads\, but nobody do this. The only available solution to writer of the module is to say to the user: if it will crash recompile your perl linked with pthreads or use LD_PRELOAD=/path/to/pthreads.so hack.
So\, my proposal is to always link perl with system threads library to allow to write threaded XS modules for all types of perl.
I made a simple patch. See attachment. Is it correct?
No\, pthread should be added to $libswanted like
Is this acceptable feature in general?
-- H.Merijn Brand http://tux.nl Perl Monger http://amsterdam.pm.org/ using perl5.00307 .. 5.19 porting perl5 on HP-UX\, AIX\, and openSUSE http://mirrors.develooper.com/hpux/ http://www.test-smoke.org/ http://qa.perl.org http://www.goldmark.org/jeff/stupid-disclaimers/
2014-10-13 5:19 GMT+07:00 Leon Timmermans via RT \perlbug\-followup@​perl\.org:
On Sun\, Oct 12\, 2014 at 3:05 PM\, Oleg G \verdrehung@​gmail\.com wrote:
I made a simple patch. See attachment. Is it correct?
No. It should only do this if there is in fact a libpthread available\, and even then there should be a way to disable it (I imagine embedded environments may not carry libpthread for example)
Leon
Thanks. I reworked my patch a little. So now it checks is libpthread available and someone can disable linking with pthreads by appending -U libpthread option to Configure script. I am still directly use $libs instead of $libswanted. Don't know is there any reason to use $libswanted
2014-10-20 14:45 GMT+07:00 Ðлег Ð \verdrehung@​gmail\.com:
2014-10-13 5:19 GMT+07:00 Leon Timmermans via RT \perlbug\-followup@​perl\.org:
On Sun\, Oct 12\, 2014 at 3:05 PM\, Oleg G \verdrehung@​gmail\.com wrote:
I made a simple patch. See attachment. Is it correct?
No. It should only do this if there is in fact a libpthread available\, and even then there should be a way to disable it (I imagine embedded environments may not carry libpthread for example)
Leon
Thanks. I reworked my patch a little. So now it checks is libpthread available and someone can disable linking with pthreads by appending -U libpthread option to Configure script. I am still directly use $libs instead of $libswanted. Don't know is there any reason to use $libswanted
Does anyone interested in this feature? If no\, feel free to reject this ticket. The worst thing\, I think\, is to leave this ticket opened for a years.
@tux - Status changed from 'open' to 'pending release'
Thanks for submitting this ticket
The issue should be resolved with the release today of Perl v5.22. If you find that the problem persists\, feel free to reopen this ticket
-- Karl Williamson for the Perl 5 porters team
@khwilliamson - Status changed from 'pending release' to 'resolved'
Migrated from rt.perl.org#122906 (status was 'resolved')
Searchable as RT122906$