Skip to content

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

Closed as not planned

Description

@rofrol

Summary

A native program's fatal path gives an embedder no way to undo shared state it changed, such as a terminal's termios. err_fail (bend2/comp.ts:3726 on main at 234cb9b) does:

static void err_fail(const char* msg) {
  fflush(stdout);
  fprintf(stderr, "bend: %s\n", msg);
  _exit(1);
}

It is reached from about 30 places (a deadlock, an out-of-memory in io_mem, a failed reservation or pthread_create, ...) and from err_trap, the SIGSEGV/SIGBUS handler installed with sigaction (comp.ts:4726-4728).

Why it matters

  • _exit skips atexit, so an effect that put the terminal in raw mode (our Tty.raw_on, a C effect) cannot restore it: after a runtime deadlock or memory fault the user's shell is left raw.
  • The runtime catches SIGSEGV/SIGBUS itself and turns them into _exit(1), so a host cannot catch those signals either: a crash looks like a plain exit 1.
  • fflush/fprintf are not async-signal-safe, and err_trap runs them from a signal handler (a fault while another thread holds the stdio lock can hang the process instead of ending it).

What we do now

A small supervisor process that saves the termios, runs the Bend program, waits for it and restores (it covers SIGKILL too). That works, but it costs a second process; an in-process fatal path would let simpler embedders stay correct.

Possible changes (any one would help)

  1. A documented fatal hook: a C function an effect registers, called by err_fail before _exit, with the contract that it must be async-signal-safe.
  2. write(2) instead of stdio in err_fail (async-signal-safe), and, for err_trap, re-raise the original signal with its default action after the message, so the process ends by SIGSEGV/SIGBUS and a parent sees the real cause.

Version / system

Bend v2.0.35 (we pin it); checked the same code on main at 234cb9b. Darwin arm64.

Activity

  1. added a commit that references this issue on Oct 19, 2026
    eb303c2
  2. added 3 commits that reference this issue on Oct 20, 2026
    1d5fcf8
    bce429d
    3703bc4
  3. nicolas-abril commented on Oct 29, 2026

    @nicolas-abril
    Collaborator

    The trap is async-signal-safe now (#1655) and memory faults end by their real signal (#1828), so a parent sees the cause. We're not adding a fatal hook (#1929): one runtime slot can't serve several effects. An effect that changes shared state can guard itself with a guardian process that restores it when the program dies, which also covers SIGKILL; your supervisor does the same from outside.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions