Skip to content

Keep native memory faults as fatal signals - #1828

Merged
Lorenzobattistela merged 4 commits into
bendlang:mainfrom
oxura:fix/fatal-signal-status-1624-20261022
Oct 23, 2026
Merged

Lorenzobattistela merged 4 commits into
bendlang:mainfrom
oxura:fix/fatal-signal-status-1624-20261022

Conversation

@oxura

@oxura oxura commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Part of #1624.

Memory faults should reach the parent as SIGBUS/SIGSEGV, not exit 1. This keeps err_claim() and the try-lock stdout flush, prints the diagnostic, then re-raises the original signal.

stack_fault_trap keeps its expected output. The test parser normalizes the C lane's signal status for that comparison; the actual process status is untouched. Removed the README note.

The actual single-test shard passes on macOS arm64: check, interpreter, JS and C all agree, buffered BEFORE survives, and the native process dies by SIGBUS (shell status 138). macOS run. The real Linux stack-fault probe preserves BEFORE too, with SIGSEGV visible to its parent. Local repo gate: 54/54, caps unchanged.

The embedder fatal hook and termios restore are still open.

@Lorenzobattistela Lorenzobattistela left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Re-raising the original signal is the direction we want: the trap prints the memory-fault line, then the process dies by SIGSEGV/SIGBUS so a parent sees the real cause. But this version has a regression, and it breaks a test without updating it.

1. Keep the flush and err_claim(). Only _exit(1) should change. The try-lock flush is there because of #1655: when stdout is a pipe, output already printed is still buffered, and without the flush a fault loses it. ftrylockfile never waits, so the flush is already as safe as a signal handler allows. Dropping err_claim() also takes the trap out of the first-failure-wins handoff, so a fault racing an err_fail on another thread can print two lines again. The trap should be:

// A trap never waits on a lock: it flushes only if free, writes raw, then
// dies by its own signal so a parent sees the fault.
static void err_trap(int sig) {
  static const char line[] = ...;
  if (err_claim() && ftrylockfile(stdout) == 0) fflush(stdout);
  if (write(2, line, sizeof line - 1)) {}
  signal(sig, SIG_DFL);
  raise(sig);
}

2. tests/io/stack_fault_trap.bend fails. It expects #|BEFORE, the fault line and #|exit 1. With this PR the C lane loses BEFORE (point 1) and exits 138 (SIGBUS on the macOS minis) instead of 1. The test gate requires every probe (check, interp, JS, C) to print the same #| lines. The interpreter and JS lanes turn the overflow into a RangeError and exit 1, so no single expected line fits every lane.

The fix we want is in shard_parse in gates/test.ts: on the C lane only, read a death by SIGBUS/SIGSEGV as exit 1:

} else if (head[0] === "exit" && last !== null && head[1] !== "0") {
  const [got, kind] = last;
  // The C runtime re-raises a memory fault after its line (SIGBUS, SIGSEGV):
  // the other lanes end that fault with exit 1.
  const code = kind === "c" && (head[1] === "138" || head[1] === "139")
    ? "1" : head[1];
  const tail = code === "142" ? "timeout" : "exit " + code;
  got[kind] = got[kind] === "" ? tail : got[kind] + "\n" + tail;
}

The test's #| lines then stay as they are. Its header still says "never a silent death by signal", so please update that wording to describe the re-raise.

3. Please drop the README lines. README is capped, and this runtime detail doesn't need a user-facing note.

Please run the test gate (at least stack_fault_trap, on macOS) after the change. This is a partial fix for #1624: the embedder fatal hook / termios restore is still open, so please use "Part of #1624" rather than "Fixes".

@oxura

oxura commented Oct 22, 2026

Copy link
Copy Markdown
Contributor Author

You're right — I dropped too much. Restored err_claim() and the try-lock flush; buffered BEFORE is back. Added the C-only signal-status comparison, updated the test header, removed the README note, and switched this to ‘Part of #1624’.

Ran the actual one-test shard on macOS arm64: check/interpreter/JS/C all pass with the existing expected lines, while C still dies by SIGBUS (138). Run and artifacts. The Linux parent sees SIGSEGV too. Local repo gate is 54/54; no cap changes.

@oxura

oxura commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

For the remaining hook, I would call one process-wide, async-signal-safe callback from the winning err_claim() path, before reporting. That lets a C effect restore termios for both ordinary failures and memory faults. Is that the shape you want? comp.ts is currently 65,994/66,000 with the pinned counter, so I am keeping the signal fix separate rather than going over cap or trimming unrelated code.

@Lorenzobattistela Lorenzobattistela self-assigned this Oct 23, 2026
@Lorenzobattistela
Lorenzobattistela merged commit 6161e58 into bendlang:main Oct 23, 2026
1 check passed
@Lorenzobattistela Lorenzobattistela mentioned this pull request Oct 23, 2026
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.

2 participants