Repository navigation
Fix try/finally: finally block now runs on Ctrl+C/SIGINT in script mode - #3
Closed
WindSoilder with Copilot wants to merge 5 commits into
Closed
WindSoilder with Copilot wants to merge 5 commits into
WindSoilder with Copilot wants to merge 5 commits into
Conversation
- In eval_ir.rs: add ShellError::Interrupted handling that bypasses catch blocks and jumps directly to the finally block, mirroring the Return/Exit behavior. Also reset the interrupt flag when jumping to any finally block so the finally block's instructions can execute. - In tester/mod.rs: add with_signals() and add_command() builder methods to NuTester for deterministic interrupt testing. - In try_.rs: add three regression tests: 1. try_finally_runs_on_interrupt (deterministic, in-process) 2. try_catch_finally_finally_runs_on_interrupt (deterministic, in-process) 3. try_finally_runs_when_script_receives_sigint (Unix integration test) Agent-Logs-Url: https://github.com/WindSoilder/nushell/sessions/a8cd72bc-56d5-49ca-9d49-28b23f7469a6 Co-authored-by: WindSoilder <[email protected]>
Copilot
AI
changed the title
[WIP] Fix try/finally to run finally block on SIGINT during external command
Fix May 2, 2026
try/finally: finally block now runs on Ctrl+C/SIGINT in script mode
Two root causes for catch not working on interrupt:
1. check_interrupt (ir/mod.rs) only fired at Jump/Return. For
try { } catch { }, the success path is TryCollect → PopErrorHandler
→ Jump. The interrupt was detected at Jump, after PopErrorHandler
had already removed the catch handler — making catch unreachable.
Fix: add TryCollect to check_interrupt so the interrupt is detected
before PopErrorHandler removes the catch/finally handlers.
2. The previous fix added an Interrupted arm that bypassed catch
entirely. Fix: remove that arm; let Interrupted go through the
generic Err(err) path which routes to catch when available.
3. The interrupt flag was not reset when entering the catch block,
causing check_interrupt to fire again at the catch block's first
instruction. Fix: reset signals when routing any error to catch
(same as was already done for finally).
Also update try_catch_finally_finally_runs_on_interrupt test: catch
now correctly runs (and finally runs after it, result is Ok).
Agent-Logs-Url: https://github.com/WindSoilder/nushell/sessions/d068b029-f86c-4a4f-a79d-32f10e1c4c19
Co-authored-by: WindSoilder <[email protected]>
Copilot stopped work on behalf of
WindSoilder due to an error
May 3, 2026 16:10
Copilot stopped work on behalf of
WindSoilder due to an error
May 3, 2026 16:11
…cess group Agent-Logs-Url: https://github.com/WindSoilder/nushell/sessions/d161ac8d-404c-4974-8e81-a13586124b79 Co-authored-by: WindSoilder <[email protected]>
WindSoilder
pushed a commit
that referenced
this pull request
Jul 7, 2026
## Description - nushell#16832 Errors raised for assignment errors did not point to the relevant assignment, just pointed to the values origin. ## User-facing changes (Release notes) ### More helpful errors for failed assignments with `enforce-runtime-annotations` With `enforce-runtime-annotations` enabled, assignments with incorrect types raise errors. Previously this error was `nu::shell::cant_convert` originally used for unsuccessful type coercions of command arguments. Unfortunately, this error only pointed at the assigned values *origin* and not at the failed assignment. This error has been switched to `nu::shell::type_mismatch` and now points to the failed assignment in addition to the value's origin. ##### example 1 <table> <tr> <td>Code</td> <td width="1000"> ```nushell mut execution_time = -1 $execution_time = $env.CMD_DURATION_MS ``` </td> </tr> <tr> <td>Before</td> <td> ``` Error: nu::shell::cant_convert × Can't convert to int. ╭─[Host Environment Variables:1:1] 1 │ "LANG"="en_US.UTF-8" · ▲ · ╰── can't convert string to int 2 │ "LC_TIME"="en_DK.UTF-8" ╰──── ``` </td> </tr> <tr> <td>After</td> <td> ``` Error: nu::shell::type_mismatch × Type mismatch. ╭─[repl_entry #3:2:17] 1 │ mut execution_time = -1 2 │ $execution_time = $env.CMD_DURATION_MS · ┬ · ╰── expected int, got string ╰──── ``` </td> </tr> </table> ##### example 2 <table> <tr> <td>Code</td> <td width="1000"> ```nushell # delibarete type erasure # we need to bypass parse-time # type checking for this example let stuff: any = { foo: "bar" } mut x = 1 $x = $stuff.foo ``` </td> </tr> <tr> <td>Before</td> <td> ``` Error: nu:🐚:cant_convert × Can't convert to int. ╭─[repl_entry nushell#7:5:7] 4 │ let stuff: any = { 5 │ foo: "bar" · ──┬── · ╰── can't convert string to int 6 │ } ╰──── ``` </td> </tr> <tr> <td>After</td> <td> ``` Error: nu:🐚:type_mismatch × Type mismatch. ╭─[repl_entry #1:5:7] 4 │ let stuff: any = { 5 │ foo: "bar" · ──┬── · ╰── the value is a string 6 │ } 7 │ 8 │ mut x = 1 9 │ 10 │ $x = $stuff.foo · ┬ · ╰── expected int, got string ╰──── ``` </td> </tr> </table> ## Additional notes - This should make it much easier to diagnose problems described in nushell#18528
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
try { ^sleep 3 } finally { ... }not running finally on Ctrl+Ctry_finally_runs_when_external_command_receives_siginttest covering the exact problem scenario (SIGINT to entire process group, matching real terminal Ctrl+C behavior)Original prompt
Create a fix for nushell
try/finallyso that thefinallyblock runs when a script is interrupted via Ctrl+C/SIGINT during an external command.Context:
try/finallydoes not runfinallyblock when script is interrupted with Ctrl+C nushell/nushell#18090, running a.nuscript containingtry { ^ping ... } finally { cleanup }and pressing Ctrl+C interrupts the external, but Nushell exits immediately and thefinallyblock does not run. This works fine in the REPL.try, Nushell should cancel/interrupt the external command, then unwind and executefinally, then exit/propagate interrupt.Repository:
Tasks:
ShellError::Interruptedor equivalent) that allows stack unwinding and finally execution.exit --abortbehavior (which should still skip finally).nu!invocation with a long-running external (ornu --testbinhelper) and triggers Ctrl+C/interrupt, asserting that finally output occurs and cleanup happens.Acceptance criteria:
finallyruns when a script is interrupted during an external command.exit --abort.Reference issue: nushell#18090
The following is the prior conversation context from the user's chat exploration (may be truncated):
User: Do you have any idea how to fix the issue? It works fine in repl, but it doesn't work while running script
Assistant: It’s very likely not the
try/finallyimplementation itself, but the script runner’s Ctrl+C/SIGINT behavior: when running a.nufile, Nushell ends up terminating the whole process immediately, so the engine never gets a chance to unwind and runfinally. In the REPL, Ctrl+C is typically handled “in-process” (set an interrupt flag, cancel the running pipeline, then return to the prompt), sofinallycan still run.There’s a strong hint of this difference in
run_file():If Ctrl+C during an external command is currently treated like an “abort the whole process now” path (or the OS delivers SIGINT in a way that kills the parent
nuwhen running non-interactively),finallywon’t run because the interpreter never resumes.What a real fix in Nushell probably looks like
Goal: on Ctrl+C during script execution, Nushell should convert the signal into a normal engine-level “interrupt”/cancellation result, not an immediate process termination, so the interpreter can unwind and run
finally, then exit.Concretely, the fix is usually some combination of:
engine_state.signals().interrupted()/ triggers signal handlers) instead of killing the process outright.ShellError::Interrupted), not “abort the process”.try { … } finally { … }path runfinallynot only for regular errors, but also for the interrupt error (unless it is explicitly an “abort” kind of exit).finallyfinishes, propagate the interrupt (exit with 130, or whatever Nushell’s convention is).There’s already an explicit notion of “abort means don’t run finally” in the tests:
So Ctrl+C is currently behaving more like “abort” in script mode, which is exactly the wrong semantic for
try/finally.A minimal thing to add (to prove/fix it)
Add a test that reproduces the bug at the engi...
This pull request was created from Copilot chat.