Skip to content

Commit 4a65abd

Browse files
committed
Better unlinking of temp files/directories
Using the default behavior of File::Temp means the files are unlinked when the filehandle is garbage collected, which can cause problems if it happens at random times. Instead do it manually in a LEAVE block.
1 parent 540aba6 commit 4a65abd

File tree

4 files changed

+52
-40
lines changed

4 files changed

+52
-40
lines changed

Benchable.p6

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ multi method irc-to-me($message where .text ~~ /^ \s* $<config>=([:i compare \s]
7373

7474
method process($message, $config, $code is copy) {
7575
my $start-time = now;
76+
my $old-dir = $*CWD;
7677

7778
my $msg-response = '';
7879
my %graph;
@@ -81,9 +82,7 @@ method process($message, $config, $code is copy) {
8182
if $config ~~ / ',' / {
8283
@commits = $config.split: ',';
8384
} elsif $config ~~ /^ $<start>=\S+ \.\. $<end>=\S+ $/ {
84-
my $old-dir = $*CWD;
8585
chdir RAKUDO;
86-
LEAVE chdir $old-dir;
8786
return "Bad start" if run('git', 'rev-parse', '--verify', $<start>).exitcode != 0;
8887
return "Bad end" if run('git', 'rev-parse', '--verify', $<end>).exitcode != 0;
8988

@@ -134,9 +133,7 @@ method process($message, $config, $code is copy) {
134133
# recursively find the commit in the middle until there are either no more large speed differences or no
135134
# more commits inbetween (i.e., the next commit is the exact one that caused the difference)
136135
if $config ~~ /:i releases / or $config ~~ / ',' / {
137-
my $old-dir = $*CWD;
138136
chdir RAKUDO;
139-
LEAVE chdir $old-dir;
140137

141138
Z: loop (my int $x = 0; $x < +@commits - 1; $x++) {
142139
if (now - $start-time > TOTAL-TIME) {
@@ -162,6 +159,7 @@ Z: loop (my int $x = 0; $x < +@commits - 1; $x++) {
162159
}
163160

164161
if @commits >= ITERATIONS {
162+
chdir $old-dir;
165163
my $gfilename = 'graph.svg';
166164
my $title = "$config $code".trans(['"'] => ['\"']);
167165
my @ydata = @commits.map({ .<err> // .<min> with %times{$_.substr(0, 7)} });
@@ -194,6 +192,11 @@ Z: loop (my int $x = 0; $x < +@commits - 1; $x++) {
194192
$msg-response ~= '¦' ~ @commits.map({ my $c = .substr(0, 7); "«$c»:" ~ (%times{$c}<err> // %times{$c}<min> // %times{$c}) }).join("\n¦");
195193

196194
return ($msg-response, %graph);
195+
196+
LEAVE {
197+
chdir $old-dir;
198+
unlink $filename;
199+
}
197200
}
198201

199202
Benchable.new.selfrun(benchable6, [bench]);

Bisectable.p6

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use lib ‘.’;
2020
use Whateverable;
2121

2222
use File::Temp;
23+
use File::Directory::Tree;
2324
use IRC::Client;
2425

2526
unit class Bisectable is Whateverable;
@@ -76,7 +77,7 @@ method test-commit($code-file, $compare-to?) {
7677
my ($output, $exit-code) = self.get-output($perl6, '--setting=RESTRICTED', '--', $code-file);
7778
$log ~= "»»»»» Script output:\n";
7879
$log ~= $output;
79-
$log ~= "»»»»» Script exit code: $exit-code\n";
80+
$log ~= "\n»»»»» Script exit code: $exit-code\n";
8081

8182
# plain bisect
8283
unless $compare-to {
@@ -104,11 +105,11 @@ method test-commit($code-file, $compare-to?) {
104105
$log ~= "»»»»» Comparing the output to:\n";
105106
$log ~= $output-good;
106107
if $output eq $output-good {
107-
$log ~= "»»»»» The output is identical\n";
108+
$log ~= "\n»»»»» The output is identical\n";
108109
$log ~= "»»»»» Final exit code: 0\n";
109110
return ($log, 0);
110111
} else {
111-
$log ~= "»»»»» The output is different\n";
112+
$log ~= "\n»»»»» The output is different\n";
112113
$log ~= "»»»»» Final exit code: 1\n";
113114
return ($log, 1);
114115
}
@@ -187,49 +188,53 @@ method process($message, $code is copy, $good, $bad) {
187188
my $output-file = ;
188189
if $exit-good == $exit-bad {
189190
$message.reply: Exit code is $exit-bad on both starting points (good=$short-good bad=$short-bad), bisecting by using the output;
190-
($output-file, my $fh) = tempfile :unlink;
191+
($output-file, my $fh) = tempfile :!unlink;
191192
$fh.print: $out-good;
192193
$fh.close;
193194
}
194195
if $exit-good != $exit-bad and $exit-good != 0 {
195196
$message.reply: For the given starting points (good=$short-good bad=$short-bad), exit code on a ‘good’ revision is $exit-good (which is bad), bisecting with inverted logic;
196197
}
197198

198-
my $result; # RT 128872
199-
{
200-
my $dir = tempdir :unlink;
201-
run(git, clone, RAKUDO, $dir);
202-
chdir $dir;
203-
LEAVE chdir $old-dir;
204-
205-
self.get-output(git, bisect, start);
206-
self.get-output(git, bisect, good, $full-good);
207-
my ($init-output, $init-status) = self.get-output(git, bisect, bad, $full-bad);
208-
if $init-status != 0 {
209-
$message.reply: bisect log: ~ self.upload({ query => $message.text,
210-
description => $message.server.current-nick,
211-
result => $init-output });
212-
return bisect init failure;
213-
}
214-
my ($bisect-output, $bisect-status);
215-
if $output-file {
216-
($bisect-output, $bisect-status) = self.run-bisect($filename, $output-file);
217-
} else {
218-
if $exit-good == 0 {
219-
($bisect-output, $bisect-status) = self.run-bisect($filename);
220-
} else {
221-
($bisect-output, $bisect-status) = self.run-bisect($filename, $exit-good);
222-
}
223-
}
199+
my $dir = tempdir :!unlink;
200+
run(git, clone, RAKUDO, $dir);
201+
chdir $dir;
202+
203+
self.get-output(git, bisect, start);
204+
self.get-output(git, bisect, good, $full-good);
205+
my ($init-output, $init-status) = self.get-output(git, bisect, bad, $full-bad);
206+
if $init-status != 0 {
224207
$message.reply: bisect log: ~ self.upload({ query => $message.text,
225208
description => $message.server.current-nick,
226-
result => $init-output\n$bisect-output });
209+
result => $init-output });
210+
return bisect init failure;
211+
}
212+
my ($bisect-output, $bisect-status);
213+
if $output-file {
214+
($bisect-output, $bisect-status) = self.run-bisect($filename, $output-file);
215+
} else {
216+
if $exit-good == 0 {
217+
($bisect-output, $bisect-status) = self.run-bisect($filename);
218+
} else {
219+
($bisect-output, $bisect-status) = self.run-bisect($filename, $exit-good);
220+
}
221+
}
222+
$message.reply: bisect log: ~ self.upload({ query => $message.text,
223+
description => $message.server.current-nick,
224+
result => $init-output\n$bisect-output });
227225

228-
return ‘bisect run’ failure if $bisect-status != 0;
226+
if $bisect-status != 0 {
227+
return ‘bisect run’ failure;
228+
} else {
229+
return self.get-output(git, show, --quiet, --date=short, --pretty=(%cd) {LINK}/%h, bisect/bad).first;
230+
}
229231

230-
($result,) = self.get-output(git, show, --quiet, --date=short, --pretty=(%cd) {LINK}/%h, bisect/bad);
232+
LEAVE {
233+
chdir $old-dir;
234+
unlink $output-file;
235+
unlink $filename;
236+
rmtree $dir;
231237
}
232-
return $result;
233238
}
234239

235240
Bisectable.new.selfrun(bisectable6, [bisect]);

Committable.p6

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ method process($message, $config, $code is copy) {
4747
{
4848
my $old_dir = $*CWD;
4949
chdir RAKUDO;
50-
LEAVE chdir $old_dir;
5150
return Bad start if run(git, rev-parse, --verify, $<start>).exitcode != 0;
5251
return Bad end if run(git, rev-parse, --verify, $<end>).exitcode != 0;
5352
($result, $exit-status, $exit-signal, $time) = self.get-output(git, rev-list, $<start>^..$<end>);
@@ -105,6 +104,11 @@ method process($message, $config, $code is copy) {
105104

106105
my $msg-response = ¦ ~ @result.map({ «{.<commits>.join(,)}»: {.<output>} }).join(\n¦);
107106
return $msg-response;
107+
108+
LEAVE {
109+
chdir $old_dir;
110+
unlink $filename;
111+
}
108112
}
109113

110114
Committable.new.selfrun(committable6, [commit]);

Whateverable.pm6

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ method to-full-commit($commit) {
114114
}
115115

116116
method write-code($code) {
117-
my ($filename, $filehandle) = tempfile :unlink;
117+
my ($filename, $filehandle) = tempfile :!unlink;
118118
$filehandle.print: $code;
119119
$filehandle.close;
120120
return $filename

0 commit comments

Comments
 (0)