Skip to content

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

Closed

Description

@MSameerAbbas

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:

  1. Run nu repro.nu
  2. Wait for ping output to start
  3. Press Ctrl+C to interrupt the ping
  4. Check whether cleanup-test.txt still 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

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:

  1. Send the signal to the external command (terminating it)
  2. Run the finally block
  3. Then propagate the interrupt (or exit)
    This is the semantics expected from try/finally in every mainstream language (Python, Java, C#, JavaScript, etc.) and from trap in POSIX shells. Without this, try/finally in 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

  1. added
    status:needs-triage
    on Apr 22, 2026
  2. fdncred commented on Apr 22, 2026

    @fdncred
    Contributor

    Thoughts @WindSoilder ?

    I get this on macos.

    ❯ nu repro.nu
    before try: cleanup-test.txt exists = true
    PING 127.0.0.1 (127.0.0.1): 56 data bytes
    64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.079 ms
    64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.357 ms
    64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.267 ms
    ^C
    --- 127.0.0.1 ping statistics ---
    3 packets transmitted, 3 packets received, 0.0% packet loss
    round-trip min/avg/max/stddev = 0.079/0.234/0.357/0.116 ms
    Error: nu::shell::error
    
      × Operation interrupted
       ╭─[/Users/fdncred/src/nushell/fin.nu:7:11]
     6 │ } finally {
     7 │     print "finally ran"
       ·           ──────┬──────
       ·                 ╰── This operation was interrupted
     8 │     rm -f cleanup-test.txt
       ╰────

    and about the same thing on Windows

    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
    
    Ping statistics for 127.0.0.1:
        Packets: Sent = 2, Received = 2, Lost = 0 (0% loss),
    Approximate round trip times in milli-seconds:
        Minimum = 0ms, Maximum = 0ms, Average = 0ms
    Control-C
    Error: nu::shell::error
    
      × Operation interrupted
       ╭─[C:\Users\username\source\repos\forks\nushell\t.nu:7:11]
     6 │ } finally {
     7 │     print "finally ran"
       ·           ──────┬──────
       ·                 ╰── This operation was interrupted
     8 │     rm -f cleanup-test.txt
       ╰────
  3. added a commit that references this issue on May 9, 2026
    c52cd71
  4. added this to the v0.113.0 milestone on May 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    status:needs-triage

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions