Subject: | tempdir(CLEANUP=>1) broken when forking (was perlbug #33769) |
[Moving from perlbug queue at request of module maintainer]
[see http://rt.perl.org/rt3/Ticket/Display.html?id=33769 ]
File::Temp::tempdir() implements the CLEANUP option by setting an END
block to recursively delete the dir. If the process then forks, both
parent and child have that END, and so it runs when either parent or
child exit. That means when the child ends, it deletes the parent's
tempdir. Test case:
my $dir = File::Temp::tempdir(CLEANUP=>1);
printf "start: %s\n", (-d $dir ? "Y" : "N");
if (not open(KID, "-|")) {
printf "child: %s\n", (-d $dir ? "Y" : "N");
exit;
} else {
while (<KID>) {
print; # spool child's STDOUT to parent's
}
printf "later: %s\n", (-d $dir ? "Y" : "N");
}
gives me:
start: Y
child: Y
later: N
under File::Temp 0.14 (part of perl5.8.5 core, and latest in CPAN).
Neither this effect (children can delete parent's or sibling's
tempdir) nor the implementation of CLEANUP is documented in the POD.
Proposed solution: the underlying _deferred_unlink() function could
store the current pid along with the dirname, and the END would only
delete if the pid were the same.