Skip to content

Commit

Permalink
Don't use an END block to close all open files
Browse files Browse the repository at this point in the history
- END blocks can be added/removed at runtime, technically
- turn END block into a private method close-all-open-handles
- call this private method *after* having run all END blocks
  • Loading branch information
lizmat committed Sep 7, 2017
1 parent 456c439 commit 347da8e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
3 changes: 1 addition & 2 deletions src/core/IO/Handle.pm
Expand Up @@ -41,8 +41,7 @@ my class IO::Handle {
nqp::bindpos($opened,$fileno,nqp::null)
}
}

END { # assuming this is the very last END block to be run
method !close-all-open-handles() {
my int $i = 2;
my int $elems = nqp::elems($opened);
nqp::while(
Expand Down
11 changes: 9 additions & 2 deletions src/core/Rakudo/Internals.pm
@@ -1,5 +1,6 @@
my class DateTime { ... }
my role IO { ... }
my class IO::Handle { ... }
my class IO::Path { ... }
my class Rakudo::Metaops { ... }
my class X::Cannot::Lazy { ... }
Expand Down Expand Up @@ -1534,13 +1535,19 @@ my constant $?BITS = nqp::isgt_i(nqp::add_i(2147483648, 1), 0) ?? 64 !! 32;
unless $the-end-is-done {
my $comp := nqp::getcomp('perl6');
my $end := nqp::getcurhllsym('@END_PHASERS');
while nqp::elems($end) {
while nqp::elems($end) { # run all END blocks
my $result := nqp::shift($end)();
$result.sink if nqp::can($result,'sink');
CATCH { $comp.handle-exception($_) }
CONTROL { $comp.handle-control($_) }
}
nqp::not_i(($the-end-is-done = 1));

# close all open files
IO::Handle.^find_private_method(
'close-all-open-handles'
)(IO::Handle);

nqp::not_i(($the-end-is-done = 1)); # we're really done now
}
}
}
Expand Down

0 comments on commit 347da8e

Please sign in to comment.