I am having a situation that is comlpetely stumping me. The overview is this: I have a perl daemon using Thread::Pool that runs fine if I do not daemonize the process, the worker thread pool starts fine, the main process submits jobs to the pool, everything runs, etc. However, if I daemonize(fork) the process first then it stops dead at initializing the thread pool.

I though at first that this was exclusively a Thread::Pool problem, but I'm not entirely sure. Being relatively new to using threads, is there some common mistake with forking before starting threads that I may be running into? I'm not entirely sure what to ask, but here is some relevant info:

Perl v5.8.0 on Debian Linux

Modules, etc:

use strict; use warnings; #use Proc::Daemon; # tried this also, with the same # results as the snippet below use threads; # Do I even need these since use threads::shared; # Thread::Pool uses 'threads'? use Thread::Pool; use POSIX;
For the kids:
$SIG{CHLD} = sub { while ( POSIX::waitpid(-1,POSIX::WNOHANG) > 0 ) { } + };
The daemonizing snippet:
die "ERROR: unable to fork!! Dying." unless defined (my $child = fork) +; exit 0 if $child; POSIX::setsid(); open(STDIN, "</dev/null"); open(STDOUT, ">/dev/null"); open(STDERR, ">&STDOUT"); chdir '/'; umask (0); $ENV{PATH} = "/bin:/sbin:/usr/bin:/usr/sbin"; return $$;

Normally, it should fork first, then spawn threads and run. However, I have tried starting the thread pool before forking and I get "A thread exited while 3 others were running", though in stracing it I can't find a thread dying with an error. (is it the main proc exiting after forking?) Despite this error, the process continues until it tries to submit a job to the thread pool, then it hangs as before. Here is the last bit of a trace:

1408 open("/usr/lib/perl/5.8.0/auto/Storable/_freeze.al", O_RDONLY|O_ +LARGEFILE) = 11 1408 fstat64(11, {st_mode=S_IFREG|0644, st_size=706, ...}) = 0 1408 old_mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONY +MOUS, -1, 0) = 0x402af000 1408 read(11, "# NOTE: Derived from ../../lib/S"..., 4096) = 706 1408 read(11, "", 4096) = 0 1408 close(11) = 0 1408 munmap(0x402af000, 4096) = 0 1408 brk(0x8ab5000) = 0x8ab5000 1408 brk(0x8abd000) = 0x8abd000 1408 kill(1405, SIGRTMIN) = -1 ESRCH (No such process) 1408 rt_sigprocmask(SIG_SETMASK, NULL, [RTMIN], 8) = 0 1408 rt_sigsuspend([]

I've no idea if the trace is helpful or not...
I feel as though I'm missing somthing basic here but I cannot seem to figure out what it is...or maybe this is exclusively a Thread::Pool problem (ahem, or my own). There are way too many possibilities to bring up in one post, but hopefully this will be enough to ring a bell with someone. Thanks in advance, good luck deciphering my confusion :)

mike

edited: Tue Dec 3 16:12:33 2002 by jeffa - added reamore tag


In reply to daemonizing and threads by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.