I've written a program to dump MySQL data in parallel. Unfortunately it is behaving oddly. While two children are forked off running mysqldump shell commands, the parent just... hangs. It runs for a while, running waitpid() and forking off more children, but then sometimes it stops and does nothing anymore.

Without posting all the code, which is quite long, here's what I was able to isolate it to:

# Wait for the MySQL server to become responsive. my $tries = 0; while ( !$dbh->ping && $tries++ < $opts{w} ) { sleep(1); eval {

It hangs just after the ping and before the sleep(). I used DBI tracing to verify that it does call $dbh->ping. But it never gets past that.

So I commented this out. This is non-essential code: it's only there to handle the MySQL server going away and returning, in case there's a crash while you dump the data.

That moved the point of no return just a few lines down, to here:

# Start a new child process. while ( @work_to_do && $opts{m} > keys %kids ) { my $todo = shift @work_to_do;

Again, the hang is in the while() condition: it never reaches a 'print' placed just inside the loop. Meanwhile, top shows that the forked children go on to complete their work and exit, at which point they are defunct but never get reaped.

This is not the only forking program I've written. I'm not an expert at it, but I've never had anything like this happen before.

So my question is, do you have any general advice for me as I try to figure out the problem?


In reply to fork() and defunct children by xaprb

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.