Skip to content

Do the required JMPENV dance when invoking a nested runloop for defer… #23244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion pod/perldelta.pod
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,19 @@ manager will later use a regex to expand these into links.

=item *

XXX
Exceptions thrown and caught entirely within a C<defer {}> or C<finally {}>
block no longer stop the outer run-loop.

Code such as the following would stop running the contents of the C<defer>
block once the inner exception in the inner C<try>/C<catch> block was caught.
This has now been fixed, and runs as expected. ([GH #23064]).

defer {
try { die "It breaks\n"; }
catch ($e) { warn $e }

say "This line would never run";
}

=back

Expand Down
30 changes: 29 additions & 1 deletion pp_ctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -6475,9 +6475,37 @@ _invoke_defer_block(pTHX_ U8 type, void *_arg)
SAVETMPS;

SAVEOP();
OP *was_PL_op = PL_op;
PL_op = start;

CALLRUNOPS(aTHX);
dJMPENV;
int ret;
JMPENV_PUSH(ret);
switch (ret) {
case 0: /* normal start */
redo_body:
CALLRUNOPS(aTHX);
break;

case 3: /* exception happened */
if (PL_restartjmpenv == PL_top_env) {
if (!PL_restartop)
break;
PL_restartjmpenv = NULL;
PL_op = PL_restartop;
PL_restartop = NULL;
goto redo_body;
}

/* FALLTHROUGH */
default:
JMPENV_POP;
PL_op = was_PL_op;
JMPENV_JUMP(ret);
NOT_REACHED;
}

JMPENV_POP;

FREETMPS;
LEAVE;
Expand Down
17 changes: 16 additions & 1 deletion t/op/defer.t
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ BEGIN {
set_up_inc('../lib');
}

plan 28;
plan 29;

use feature 'defer';
no warnings 'experimental::defer';
Expand Down Expand Up @@ -285,3 +285,18 @@ no warnings 'experimental::defer';
like($e, qr/^Bareword "foo" not allowed while "strict subs" in use at /,
'Error from finalization');
}

# GH#23604
{
my $ok;
{
defer {
eval { die "Ignore this error\n" };
$ok .= "k";
}

$ok .= "o";
}

is($ok, "ok", 'eval{die} inside defer does not stop runloop');
}
19 changes: 19 additions & 0 deletions t/op/try.t
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,25 @@ no warnings 'experimental::try';
ok($finally_invoked, 'finally block still invoked for side-effects');
}

# Variant of GH#23604
{
my $ok;
try {
# nothing
}
catch ($e) {}
finally {
try {
die "Ignore this error\n"
}
catch ($e) {}

$ok = "ok";
}

is($ok, "ok", 'try{die} inside try/finally does not stop runloop');
}

# Nicer compiletime errors
{
my $e;
Expand Down
Loading