toddr / Net-Daemon

Read-only release history for Net-Daemon
http://metacpan.org/release/Net-Daemon
0 stars 0 forks source link

Net::Daemon fork mode is not reaping processes [rt.cpan.org #82539] #5

Open toddr opened 4 years ago

toddr commented 4 years ago

Migrated from rt.cpan.org#82539 (status was 'new')

Requestors:

Attachments:

From kas@fi.muni.cz on 2013-01-07 19:47:16 :

We are trying to use a RPC::PlServer service on a Windows machine
running Strawberry Perl. RPC::PlServer inherits from Net::Daemon. We use
fork mode, and the server keeps dying with "resource not available"
after ~64th connection. From the list of processes/threads we think the
child processes (threads under Windows) are not reaped when they exit().

Using the standard technique of ignoring child process states (i.e.
$SIG{CHLD} = 'IGNORE'; ) did not help. Reaping the child processes
explicitly using waitpid() helped, though.

Please consider applying the attached patch or using it as a basis for
more general solution. Thanks!
toddr commented 4 years ago
--- v0.48/Net/Daemon.pm 2011-03-09 17:41:12.000000000 +0100
+++ new/Net/Daemon.pm   2013-01-07 20:11:18.000000000 +0100
@@ -33,6 +33,8 @@

 package Net::Daemon;

+use POSIX ":sys_wait_h";
+
 $Net::Daemon::VERSION = '0.48';

 # Dummy share() in case we're >= 5.10. If we are, require/import of
@@ -491,7 +493,10 @@
     } else {
    my $pid = fork();
    die "Cannot fork: $!" unless defined $pid;
-   return if $pid;        # Parent
+   if ($pid) { # Parent
+        1 while (waitpid(-1,WNOHANG) > 0);
+        return;
+   }        
    $self->$method(@args); # Child
    exit(0);
     }
toddr commented 4 years ago

This will cause the parent to hang waiting on the child which means simultaneous connections would not be possible. The server needs to track and periodically reap its connections.

More work is needed. PRs welcome.