[jifty-devel] jifty segfault with threads

Stanislav Sinyagin ssinyagin at yahoo.com
Thu Dec 9 16:57:44 EST 2010


well, I've done a lot with threading in torrus.org :)

I didn't try it with jifty, but I guess your fault is that you:
1) initialize Jifty while you're single-threaded
2) launch a thread afterward.

Creating a thread is an expensive process in Perl, and it tries to duplicate 
all memory structures. So, in your case, you have already lots of initialized 
memory and a database handle, and then Perl tries to clone everything, 
and it's not always happening well :)


If you really want to do it, then at the beginning of your program you 
launch all your threads, then in only one thread you do 

require('Jifty');
and do all your jifty-related stuff in this thread. Tthis could also be the 
parent thread. Important that you don't create new threads from a thread 
where Jifty is initialized.

then it should work, in theory :)

but I believe you can easily avoid multithreading in most cases.

Another point -- if you execute some non-threadsafe library from multiple
threads, you just protect that piece with semaphores.

Example: 
XML::LibXML is not thread-safe, as well as the underlying Gnome libxml2.
http://goo.gl/lHHVZ
Line 505 onwards: 
here I screen LibXML calls with a semaphore, ensuring that only one thread 
uses the library at a time, from the XML file opening to its closing.


I hope this helps :)








----- Original Message ----
> From: Matt Zagrabelny <mzagrabe at d.umn.edu>
> To: jifty-devel <jifty-devel at lists.jifty.org>
> Sent: Thu, December 9, 2010 9:13:07 PM
> Subject: [jifty-devel] jifty segfault with threads
> 
> Greetings,
> 
> I've got a bug report, which may be low on the totem pole, but  thought
> I ought to report nonetheless.
> 
> When using the following code,  perl segfaults:
> 
> #!/usr/bin/env perl
> 
> use  Jifty::Everything;
> BEGIN { Jifty->new() }
> 
> use threads;
> 
> my  $use_threads = 1;
> 
> if ($use_threads) {
>   my $insert_thread =  threads->create(\&insert);
>   $insert_thread->join;
> } else  {
>   &insert;
> }
> 
> sub insert {
>   my $u =  BTDT::Model::User->new( current_user =>
> BTDT::CurrentUser->superuser  );
>   my ( $id, $msg ) = $u->create(
>      id   =>  10,
>      name => 'admin',
>   );
>   print $msg  unless ($id);
> }
> 


More information about the jifty-devel mailing list