system() with timeout

Aaron Crane perl at
Fri Apr 17 11:48:21 BST 2009

Matt Lawrence writes:
> I recently discovered that die() inside a signal handler causes a
> memory leak.[...]  I guess that might be related to the problem you
> were having before timing out system().

Unlikely.  Internally, system() forks a child process which execs a
suitable program.  The parent process then does a waitpid(2) to wait
for the child process to complete.  The SIGALRM arrives during the
waitpid, thus interrupting it.  Perl's system() will normally try
waiting again when the waitpid is interrupted -- but in this case, the
exception handler throws an exception, so that doesn't happen, and
instead the eval is exited.

At that point, the child process is still executing, because nothing
has tried to stop it, and that's the behaviour David was seeing.  None
of this has anything to do with a memory leak; it's just how it's all
expected to work.

There's an additional problem when the underlying waitpid is interrupted
like this: nothing ever waits for the child process, until the parent
process exits, when the child gets reparented by init(8).  That can be
worked around by, for example, ignoring SIGCHLD, or calling wait() at
suitable points in your Perl code.

Aaron Crane **

More information about the mailing list