Subject: | run_foked and exit |
Hello,
run_forked, when finishing up the "watchdog fork" uses an `exit`. This
causes the END blocks in the calling script/modules to be executed.
This can sometimes be dangerous or catch people off-guard.
The following example shows this behavior ..
##############################
#!/usr/bin/perl
use strict;
use warnings FATAL => 'all';
# Autoflush ON
local $| = 1;
use IPC::Cmd qw(run_forked);
my $parent_pid;
BEGIN { $parent_pid = $$; }
print "Calling script's PID is : $parent_pid\n";
# Do some work ...
# Call an external command with a timeout
my $out = run_forked(
'echo foo', # Command
{
# timeout at 10s
timeout => 10,
# Kills all children in case this script terminates
terminate_on_parent_sudden_death => 1, },
);
print "Run finished\n";
# Do some more work ...
END {
# Do some cleanup ...
print "END is executed in $$\n";
}
This will print:
$ ./testf.pl
Calling script's PID is : 6124
END is executed in 4284
Run finished
END is executed in 6124
##############################
Can run_forked be updated to -
1. Use POSIX::_exit(...) ? Since there is some logic to handle
child_END, the forked child/watchdog may not have to execute END blocks.
2. OR at least provide an option for users to choose between
POSIX::_exit and builtin `exit`, while the built-in is default behavior.
Please let me know which of the above if preferred. I can provide a
patch.
Thanks,
-- Mithun