Repository navigation
Description
Bug report form
- I have done a basic search of the issue tracker to find any existing issues that are similar.
- I have checked that my version is at least the latest stable release available via my installation method.
Describe the bug
When an external command in a try block is interrupted by Ctrl+C (SIGINT), the finally block does not run. The nushell process itself appears to be terminated by the signal before finally gets a chance to execute, so any cleanup code there is skipped.
This makes try/finally unusable for its primary intended use case: guaranteed cleanup of temporary files, directories, or other resources in scripts that may be interrupted by the user.
This was expected to be addressed by the try/finally feature (#15279, which was filed as a fix for #13394 "the catch block doesn't run when script is interrupted by the user"). However, finally behaves the same way as catch did -- it does not run on Ctrl+C.
How to reproduce
Save the following as repro.nu:
#!/usr/bin/env nu
"test content" | save -f cleanup-test.txt
print $"before try: cleanup-test.txt exists = ('cleanup-test.txt' | path exists)"
try {
^ping -n 30 127.0.0.1 # or `^ping -c 30 127.0.0.1` on Linux/macOS
} finally {
print "finally ran"
rm -f cleanup-test.txt
}
print $"after try: cleanup-test.txt exists = ('cleanup-test.txt' | path exists)"Steps:
- Run
nu repro.nu - Wait for ping output to start
- Press Ctrl+C to interrupt the ping
- Check whether
cleanup-test.txtstill exists
Observed output:
before try: cleanup-test.txt exists = true
Pinging 127.0.0.1 with 32 bytes of data:
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
^C
Nushell exits immediately after Ctrl+C. finally ran is never printed. cleanup-test.txt is still on disk after the script terminates.
Contrast with error-based trigger
When the try block raises an error instead of being interrupted, finally runs correctly:
try {
error make { msg: "simulated failure" }
} finally {
print "finally ran"
rm -f cleanup-test.txt
}Output:
finally ran
Error: ...
The file is deleted.
Related issues
- the catch block doesn't run when script is interrupted by the user #13394 (closed as duplicate of Please add
try {} finally {}#15279) -- "the catch block doesn't run when script is interrupted by the user". Closed on the assumption thattry/finallywould solve it. - Please add
try {} finally {}#15279 (closed, milestone 0.111.0) -- feature request fortry/finally. The feature shipped but does not cover the SIGINT case. try/finallyrunsfinallyblock before external command completes #17715 (closed, milestone 0.112.0) -- separate but adjacent bug wherefinallyran before the external command completed. Fixed in 0.112.1, but the Ctrl+C case remains.
Expected behavior
I expected the finally block to execute after the try block terminates, regardless of whether it terminated normally, by error, or by user interrupt (Ctrl+C / SIGINT). Specifically, when Ctrl+C is pressed during an external command in try, nushell should:
- Send the signal to the external command (terminating it)
- Run the
finallyblock - Then propagate the interrupt (or exit)
This is the semantics expected fromtry/finallyin every mainstream language (Python, Java, C#, JavaScript, etc.) and fromtrapin POSIX shells. Without this,try/finallyin nushell cannot be used for its primary cleanup use case in any script that might be interrupted.
Configuration
| key | value |
|---|---|
| version | 0.112.2 |
| major | 0 |
| minor | 112 |
| patch | 2 |
| branch | |
| commit_hash | |
| build_os | windows-x86_64 |
| build_target | x86_64-pc-windows-msvc |
| rust_version | rustc 1.93.0 (254b59607 2026-01-19) |
| rust_channel | stable-x86_64-pc-windows-msvc |
| cargo_version | cargo 1.93.0 (083ac5135 2025-12-15) |
| build_time | 2026-04-22 16:13:17 +05:00 |
| build_rust_channel | release |
| allocator | standard |
| features | default, full, mcp, network, plugin, rustls-tls, sqlite, system-clipboard, trash-support |
| installed_plugins | |
| experimental_options | example=false, reorder-cell-paths=true, pipefail=true, enforce-runtime-annotations=false, native-clip=true, cell-path-types=false |
Activity
fdncred commentedon Apr 22, 2026
Thoughts @WindSoilder ?
I get this on macos.
and about the same thing on Windows
make ctrl-c works with try..finally while running script (#18155)