Skip to content

fix(native): run effect cleanup before fatal exit - #1929

Closed
oxura wants to merge 1 commit into
bendlang:mainfrom
oxura:feat/1624-native-fatal-hook-20261029
Closed

oxura wants to merge 1 commit into
bendlang:mainfrom
oxura:feat/1624-native-fatal-hook-20261029

Conversation

@oxura

@oxura oxura commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

Closes #1624.

This fills the cleanup gap left after #1655 and #1828: native effect constructors can set err_cleanup, and the first fatal claimant runs it before exiting. The hook must be async-signal-safe. It doesn’t recover the program or swallow its original signal.

I checked this with real PTYs on Linux and macOS arm64. Before the change, failstop/SIGSEGV/SIGBUS left the terminal raw; afterwards, its configuration is restored and the exit/signal stays the same. Normal exit is a separate control. macOS before/after and stack-fault run.

The native consumer also covers one-shot cleanup and a fault inside the hook; it passed through native, Bun and Node on Linux. The existing stack-fault case still terminates by signal on both OSes.

Repo gate: 54/54. Compiler: 65,992/66,000 tokens, with no budget or gate changes.

@nicolas-abril nicolas-abril self-assigned this Oct 29, 2026
@nicolas-abril

Copy link
Copy Markdown
Collaborator

Thanks, and the PTY checks on both OSes are thorough. We're not taking a runtime cleanup slot, though. err_cleanup is a single pointer set in a constructor, so two effects that each need cleanup can't both register (constructor order decides, silently), and the hook is armed whether or not the effect ever ran. A fatal stop is a bug or resource exhaustion, and an effect that changes shared state can guard itself without the runtime's help. For example, raw_on can fork a small guardian that holds the saved termios and blocks on a pipe. When the program dies, by err_fail, a signal or even SIGKILL, the pipe closes and the guardian restores the terminal. Effects compose that way, and it covers more than an async-signal-safe hook can. An external supervisor, as #1624 describes, works too.

@oxura

oxura commented Oct 29, 2026

Copy link
Copy Markdown
Contributor Author

Got it. A single slot is not composable, and a runtime hook cannot cover SIGKILL anyway. I’ll drop this approach rather than turn it into a cleanup registry. The guardian/supervisor belongs with the effect or embedder; I’m not treating this closed PR as a fix for #1624.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

err_fail's _exit and stdio leave an embedder no fatal path (terminal state, signals)

2 participants