Skip to content

Commit 21b3035

Browse files
committed
close handle before unlink file; and close all handles in END
1 parent e1431d3 commit 21b3035

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

lib/File/Temp.pm

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,19 @@ my @filechars = flat('a'..'z', 'A'..'Z', 0..9, '_');
77
constant MAX-RETRIES = 10;
88

99
my %roster = ();
10+
my %keptfd = ();
1011
my Lock $roster-lock;
12+
my Lock $keptfd-lock;
1113
BEGIN { # Because --doc runs END
1214
$roster-lock = Lock.new;
15+
$keptfd-lock = Lock.new;
1316
}
1417

1518
my role File::Temp::AutoUnlink {
1619
submethod DESTROY {
1720
given self.path {
1821
if $_.path.IO ~~ :f { # Workaround, should just be $_ ~~ :f
22+
self.close;
1923
my $did;
2024
$roster-lock.protect: {
2125
$did = %roster{$_.path}:delete;
@@ -48,9 +52,13 @@ sub make-temp($type, $template, $tempdir, $prefix, $suffix, $unlink) {
4852
}
4953
if $unlink {
5054
$roster-lock.protect: {
51-
%roster{$name} = True;
55+
%roster{$name} = $fh;
5256
};
5357
$fh &&= $fh does File::Temp::AutoUnlink;
58+
} elsif ($type eq 'file') {
59+
$keptfd-lock.protect: {
60+
%keptfd{$name} = $fh;
61+
}
5462
}
5563
return $type eq 'file' ?? ($name,$fh) !! $name;
5664
}
@@ -86,6 +94,7 @@ END {
8694
for @rk -> $fn {
8795
if $fn.IO ~~ :f
8896
{
97+
%roster{$fn}.close;
8998
unlink($fn);
9099
}
91100
elsif $fn.IO ~~ :d
@@ -95,6 +104,13 @@ END {
95104
}
96105
%roster = ();
97106
}
107+
$keptfd-lock.protect: {
108+
my @kk = %keptfd.keys;
109+
for @kk -> $fn {
110+
%keptfd{$fn}.close;
111+
}
112+
%keptfd = ();
113+
}
98114
}
99115

100116

0 commit comments

Comments
 (0)