Subject: | Data corruption with fork when both parent and child use run3 |
When your program calls run3, then forks and both parent and
child continue to call run3, they may mix up their data.
The reason is the cache of temp filehandles (%fh_cache) that
IPC::Run3 uses. It gets inherited by fork, so parent and child
will read/write to the same temp files and overwrite each others data.
Suggested fix attached: remember the current pid to detect (on the
next call to run3) that a fork has occurred; clear the cache in this case.
--- Run3.pm.orig 2005-10-11 11:33:51.770793000 +0200
+++ Run3.pm 2005-10-11 11:48:22.205958000 +0200
@@ -164,6 +164,7 @@
# We cache the handles of our temp files in order to
# keep from having to incur the (largish) overhead of File::Temp
my %fh_cache;
+my $fh_cache_pid = $$;
my $profiler;
@@ -423,6 +424,13 @@
my $out_type = _type $stdout;
my $err_type = _type $stderr;
+ if ($fh_cache_pid != $$) {
+ # fork detected, close all cached filehandles and clear the cache
+ close $_ foreach values %fh_cache;
+ %fh_cache = ();
+ $fh_cache_pid = $$;
+ }
+
# This routine procedes in stages so that a failure in an early
# stage prevents later stages from running, and thus from needing
# cleanup.