Skip to content

make ctrl-c works with try..finally while running script - #18155

Merged
WindSoilder merged 3 commits into
nushell:mainfrom
WindSoilder:try_ctrlc
May 9, 2026
Merged

WindSoilder merged 3 commits into
nushell:mainfrom
WindSoilder:try_ctrlc

Conversation

@WindSoilder

@WindSoilder WindSoilder commented May 3, 2026 •

Copy link
Copy Markdown
Contributor

Description

It's caused when running a nushell script, and pressing ctrl-c, the signal is still set. So catch or finally block won't running.
I'm not sure why it works in repl, but the signal should be reset too.

User-facing changes (Release notes)

try blocks may now be interrupted with ctrl-c

Finally block will run if try block is interruped with ctrl-c, for example:

# cc.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)"

And run it with:

> nu cc.nu

press ctrl-c during printing, it'll output finally ran

Additional notes

fixes #18090

@WindSoilder WindSoilder added notes:fixes Noted in "Bug fixes" section notes:ready Indicates Ready for Release notes labels May 3, 2026
@WindSoilder

WindSoilder commented May 4, 2026 •

Copy link
Copy Markdown
Contributor Author

Mark it as draft because it doesn't work with ^sleep 3 case
edit: Fixed it by checking ShellError::TerminatedBySignal too

@WindSoilder
WindSoilder marked this pull request as draft May 4, 2026 09:35
@WindSoilder
WindSoilder marked this pull request as ready for review May 6, 2026 03:20
@WindSoilder
WindSoilder merged commit c52cd71 into nushell:main May 9, 2026
18 checks passed
@github-actions github-actions Bot added this to the v0.113.0 milestone May 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

notes:fixes Noted in "Bug fixes" section notes:ready Indicates Ready for Release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

try/finally does not run finally block when script is interrupted with Ctrl+C

1 participant