Subject: | tempfile with UNLINK => 1 leaks file descriptors |
$ perl5.24.0 -S pmvers File::Temp
0.2304
$ perl5.24.0 -e 'use File::Temp "tempfile"; for (1..2000) { my (undef, $tempfn) = tempfile uc unlink=>1; $x++ }'
Error in tempfile() using template /var/folders/48/6ysnwsz12ync5qd2xyc3p2c00000gq/T/XXXXXXXXXX: Could not create temp file /var/folders/48/6ysnwsz12ync5qd2xyc3p2c00000gq/T/PRtregksgn: Too many open files at -e line 1.
The $x++ is just a dummy statement to make sure that mortals are freed between iterations. Although I did not assign the returned handle to anything, nevertheless it was not freed, which means that something is hanging on to it. And while I did ask it to delete the files at program exit, I did not ask it to keep the files *open* till then!
An explicit close works:
$ perl5.24.0 -e 'use File::Temp "tempfile"; for (1..2000) { my ($fh, $tempfn) = tempfile uc unlink=>1; close $fh; $x++ }'
And this is what I am doing to work around the problem in my code. But asking for the handle I’m not even going to use seems a bit weird.
It looks as though it hangs on to the file handle just to be able to make sure it is closed. Ideally it should be using a weak reference if the Perl version supports it.