Skip Menu |

This queue is for tickets about the File-Temp CPAN distribution.

Report information
The Basics
Id: 120308
Status: open
Priority: 0/
Queue: File-Temp

People
Owner: Nobody in particular
Requestors: 'spro^^*%*^6ut# [...] &$%*c
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



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.
On Mon Feb 20 18:19:23 2017, SPROUT wrote: Show quoted text
> $ 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.
I don't think this is a bug. The documentation says the following: tmpfile Returns the filehandle of a temporary file. $fh = tmpfile(); The file is removed when the filehandle is closed or when the program exits. No access to the filename is provided. If called in scalar context, only the filehandle is returned and the file will automatically be deleted when closed on operating systems that support this (see the description of tmpfile() elsewhere in this document). ..... The "UNLINK" flag is ignored if present. (undef, $filename) = tempfile($template, OPEN => 0); This will return the filename based on the template but will not open this file. Cannot be used in conjunction with UNLINK set to true. Default is to always open the file to protect from possible race conditions. A warning is issued if warnings are turned on. Consider using the tmpnam() and mktemp() functions described elsewhere in this document if opening the file is not required.