[GH-ISSUE #6183] Closing the terminal window ends the server abruptly instead of shutting it down, leaving the storage writer lease behind #3445

Closed
opened 2026-10-21 06:03:11 +07:00 by bsakaguchi · 2 comments
Owner

Originally created by @kolacheee on GitHub (Sep 14, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/6183

Originally assigned to: @SpicyMarinara on GitHub.

On macOS, Linux, and Termux, closing the terminal window that runs start.sh (or losing the SSH session it runs in) ends the server on the spot instead of shutting it down. Nothing is flushed or released: the storage writer lease stays on disk, up to about three quarters of a second of saves the app already confirmed can be lost, a running local-model sidecar is not reaped, and the next start reports that the previous session "ended without shutting down" as if the process had been force-quit or lost power - even though the user only closed the window. Pressing Ctrl+C first avoids all of it, which is not something users know to do.

The lease itself recovers: the next start reclaims it through the same-host process check and logs a warning. The abrupt ending is the bug, and the leftover lease is its most visible symptom (users see the .writer-lease folder and the reclaim warning and reasonably assume something is wrong).

How to reproduce (try it yourself)

Linux or macOS, any recent staging (verified at f2219d815).

  1. Open a terminal window and run ./start.sh. Wait for the server's Marinara Engine server listening line.
  2. Close the terminal window with its close button (do not press Ctrl+C). The same thing happens if you run kill -HUP <pid of the shell> from another terminal.
  3. Look at packages/server/data/storage/.
  4. Run ./start.sh again and read the first few seconds of output.

What you'll see: at step 3 the .writer-lease folder is still there. At step 4 the server logs [file-storage] Reclaimed the writer lease after confirming its same-host PID exited, and Support Diagnostics records the session as ended without shutting down (the Previous session (pid N) ended without shutting down ... a force-quit or power loss warning appears once the session is old enough to have a heartbeat).

What should happen: the same clean stop as Ctrl+C. Shutdown complete is reached (it cannot be printed - the terminal is gone - but the lease folder is removed, the session heartbeat is stamped clean, and the next start prints no reclaim warning and no force-quit report).

Where it is

At staging f2219d815:

  • packages/server/src/index.ts:116-121 - the only signal handlers are SIGTERM and SIGINT. There is no SIGHUP listener, so Node applies the default action (terminate immediately) when the hangup arrives.
  • scripts/run-server.mjs:9-20 - the launcher supervisor already relays SIGHUP to the server (child?.kill(signal)), but relaying does not help: when a terminal closes, the shell hangs up the whole foreground job, so the server receives SIGHUP directly at the same moment and dies before the relay lands.
  • packages/server/src/lib/logger.ts:17-20 and packages/server/src/app.ts:94-98 - the two Pino instances write to stdout through Pino's default asynchronous stream. Pino's own guard (buildSafeSonicBoom in pino/lib/tools.js) only tolerates EPIPE; a hung-up terminal answers every write with EIO, which Pino re-emits as an unhandled 'error'. And Pino's exit-time flushSync (sonic-boom/index.js, the flushSync loop) retries a failed write every 100 ms without end for any error that is not EAGAIN/EBUSY.

What's happening

Two layers, and the second only shows up once the first is fixed.

  1. No SIGHUP handler, so the process is terminated by the signal itself. The debounced store flush, the onClose hook that releases the writer lease, the sidecar reaper, and the session exit stamp (process.on("exit") hooks) never run. That is exactly what the next start reports.

  2. Simply adding a SIGHUP handler that runs the existing shutdown() is not enough, and this is the part worth knowing before fixing it. By the time the shell forwards SIGHUP, the pty is already gone, so stdout returns EIO. The graceful close finishes within a couple of milliseconds - before the failed asynchronous write for the "Received SIGHUP" line has even reported back - so Pino never learns the stream is dead. At process.exit, Pino's exit hook calls flushSync on the buffered "Shutdown complete" line, gets EIO, and retries forever. Measured with a real pty: the store flushed and the lease was released, but the process then sat in that loop for 10.0 s until the launcher's SIGKILL backstop killed it (exit status 137), so the session exit stamp was never written and the next start still reported a force-quit. With the terminal alive (kill -HUP on the shell while the pty stays open) the same handler works perfectly, which is why this is easy to get wrong in a pipe-based test.

Suggested fix

  • In index.ts, on non-Windows platforms, treat SIGHUP like SIGTERM: process.on("SIGHUP", () => void shutdown("SIGHUP")). Windows raises SIGHUP for a closed console through a different mechanism (#6119) and should be left alone until someone can verify it there.
  • In logger.ts, extend Pino's dead-pipe tolerance to a dead terminal, for both Pino instances (the shared logger and Fastify's app.log): on an EIO/EPIPE 'error' from the underlying stream, replace write/end/flushSync/destroy with no-ops the way Pino already does for EPIPE; and register a process.on("exit") hook before any Pino instance exists that checks "stdout was a terminal at startup and isatty(1) is false now" and silences the streams, so Pino's exit flush has nothing to retry. isatty(1) reliably flips to false after a pty hangup (verified).
  • Coverage: a regression that hangs up a real pty, not just a pipe - the pipe case passes without the second half of the fix. python3's pty module or util-linux script can provide the pty on Linux CI; the pipe-based SIGHUP case can run everywhere the existing server-signal-shutdown lane runs.

A Podman user on the v2.4.4 image reported having to delete data/storage/.writer-lease before almost every launch. That is a different, already-fixed cause: on v2.4.4 a podman stop with any browser connection still open exceeds the 10 s stop window and is SIGKILLed (fixed on staging by #5838), and the lease it leaves can never be reclaimed after a host reboot because v2.4.4 has no boot-id proof for a container's hostId: null lease (fixed on staging by #5744). Both were verified against the latest (v2.4.4) and staging GHCR images under rootless Podman 5.8.4: latest is blocked after a simulated reboot and leaves the lease on a stop with an in-flight request; staging reclaims the lease in every case (SIGKILL, container recreate, reboot) and stops cleanly in 4.3 s. That report needs the next release, not a code change; this issue is only about the terminal-close path.

Context

Found while investigating that Podman report and re-verified on Fedora Linux with Node v24.21.0 against staging f2219d815 on 2026-09-13, using a Python pty harness (interactive bash in a pty running node scripts/run-server.mjs dist/index.js, then closing the pty master). The two-layer diagnosis above comes from instrumenting the exit hooks in the built server; the numbers (10.0 s, exit 137) are measured, not estimated. Please verify on macOS before closing; the Linux behavior is confirmed.

Originally created by @kolacheee on GitHub (Sep 14, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/6183 Originally assigned to: @SpicyMarinara on GitHub. On macOS, Linux, and Termux, closing the terminal window that runs `start.sh` (or losing the SSH session it runs in) ends the server on the spot instead of shutting it down. Nothing is flushed or released: the storage writer lease stays on disk, up to about three quarters of a second of saves the app already confirmed can be lost, a running local-model sidecar is not reaped, and the next start reports that the previous session "ended without shutting down" as if the process had been force-quit or lost power - even though the user only closed the window. Pressing Ctrl+C first avoids all of it, which is not something users know to do. The lease itself recovers: the next start reclaims it through the same-host process check and logs a warning. The abrupt ending is the bug, and the leftover lease is its most visible symptom (users see the `.writer-lease` folder and the reclaim warning and reasonably assume something is wrong). ## How to reproduce (try it yourself) Linux or macOS, any recent `staging` (verified at f2219d815). 1. Open a terminal window and run `./start.sh`. Wait for the server's `Marinara Engine server listening` line. 2. Close the terminal window with its close button (do not press Ctrl+C). The same thing happens if you run `kill -HUP <pid of the shell>` from another terminal. 3. Look at `packages/server/data/storage/`. 4. Run `./start.sh` again and read the first few seconds of output. **What you'll see:** at step 3 the `.writer-lease` folder is still there. At step 4 the server logs `[file-storage] Reclaimed the writer lease after confirming its same-host PID exited`, and Support Diagnostics records the session as ended without shutting down (the `Previous session (pid N) ended without shutting down ... a force-quit or power loss` warning appears once the session is old enough to have a heartbeat). **What should happen:** the same clean stop as Ctrl+C. `Shutdown complete` is reached (it cannot be printed - the terminal is gone - but the lease folder is removed, the session heartbeat is stamped `clean`, and the next start prints no reclaim warning and no force-quit report). ## Where it is At staging f2219d815: - `packages/server/src/index.ts:116-121` - the only signal handlers are `SIGTERM` and `SIGINT`. There is no `SIGHUP` listener, so Node applies the default action (terminate immediately) when the hangup arrives. - `scripts/run-server.mjs:9-20` - the launcher supervisor already relays `SIGHUP` to the server (`child?.kill(signal)`), but relaying does not help: when a terminal closes, the shell hangs up the whole foreground job, so the server receives `SIGHUP` directly at the same moment and dies before the relay lands. - `packages/server/src/lib/logger.ts:17-20` and `packages/server/src/app.ts:94-98` - the two Pino instances write to stdout through Pino's default asynchronous stream. Pino's own guard (`buildSafeSonicBoom` in `pino/lib/tools.js`) only tolerates `EPIPE`; a hung-up terminal answers every write with `EIO`, which Pino re-emits as an unhandled `'error'`. And Pino's exit-time `flushSync` (`sonic-boom/index.js`, the `flushSync` loop) retries a failed write every 100 ms without end for any error that is not `EAGAIN`/`EBUSY`. ## What's happening Two layers, and the second only shows up once the first is fixed. 1. No `SIGHUP` handler, so the process is terminated by the signal itself. The debounced store flush, the `onClose` hook that releases the writer lease, the sidecar reaper, and the session exit stamp (`process.on("exit")` hooks) never run. That is exactly what the next start reports. 2. Simply adding a `SIGHUP` handler that runs the existing `shutdown()` is not enough, and this is the part worth knowing before fixing it. By the time the shell forwards `SIGHUP`, the pty is already gone, so stdout returns `EIO`. The graceful close finishes within a couple of milliseconds - before the failed asynchronous write for the "Received SIGHUP" line has even reported back - so Pino never learns the stream is dead. At `process.exit`, Pino's exit hook calls `flushSync` on the buffered "Shutdown complete" line, gets `EIO`, and retries forever. Measured with a real pty: the store flushed and the lease was released, but the process then sat in that loop for 10.0 s until the launcher's SIGKILL backstop killed it (exit status 137), so the session exit stamp was never written and the next start still reported a force-quit. With the terminal alive (`kill -HUP` on the shell while the pty stays open) the same handler works perfectly, which is why this is easy to get wrong in a pipe-based test. ## Suggested fix - In `index.ts`, on non-Windows platforms, treat `SIGHUP` like `SIGTERM`: `process.on("SIGHUP", () => void shutdown("SIGHUP"))`. Windows raises `SIGHUP` for a closed console through a different mechanism (#6119) and should be left alone until someone can verify it there. - In `logger.ts`, extend Pino's dead-pipe tolerance to a dead terminal, for both Pino instances (the shared logger and Fastify's `app.log`): on an `EIO`/`EPIPE` `'error'` from the underlying stream, replace `write`/`end`/`flushSync`/`destroy` with no-ops the way Pino already does for `EPIPE`; and register a `process.on("exit")` hook before any Pino instance exists that checks "stdout was a terminal at startup and `isatty(1)` is false now" and silences the streams, so Pino's exit flush has nothing to retry. `isatty(1)` reliably flips to `false` after a pty hangup (verified). - Coverage: a regression that hangs up a real pty, not just a pipe - the pipe case passes without the second half of the fix. `python3`'s `pty` module or util-linux `script` can provide the pty on Linux CI; the pipe-based `SIGHUP` case can run everywhere the existing `server-signal-shutdown` lane runs. ## Related report A Podman user on the v2.4.4 image reported having to delete `data/storage/.writer-lease` before almost every launch. That is a different, already-fixed cause: on v2.4.4 a `podman stop` with any browser connection still open exceeds the 10 s stop window and is SIGKILLed (fixed on staging by #5838), and the lease it leaves can never be reclaimed after a host reboot because v2.4.4 has no boot-id proof for a container's `hostId: null` lease (fixed on staging by #5744). Both were verified against the `latest` (v2.4.4) and `staging` GHCR images under rootless Podman 5.8.4: `latest` is blocked after a simulated reboot and leaves the lease on a stop with an in-flight request; `staging` reclaims the lease in every case (SIGKILL, container recreate, reboot) and stops cleanly in 4.3 s. That report needs the next release, not a code change; this issue is only about the terminal-close path. ## Context Found while investigating that Podman report and re-verified on Fedora Linux with Node v24.21.0 against staging f2219d815 on 2026-09-13, using a Python pty harness (interactive bash in a pty running `node scripts/run-server.mjs dist/index.js`, then closing the pty master). The two-layer diagnosis above comes from instrumenting the exit hooks in the built server; the numbers (10.0 s, exit 137) are measured, not estimated. Please verify on macOS before closing; the Linux behavior is confirmed.
bsakaguchi 2026-10-21 06:03:11 +07:00
Author
Owner

@SpicyMarinara commented on GitHub (Sep 14, 2026):

Taking this in the assigned sweep for @SpicyMarinara, with Codex implementation tracked in draft #6193. Existing open PRs were checked before starting. I will preserve the current staging fixes and verify this issue’s remaining behavior before closing it.

<!-- gh-comment-id:5662613761 --> @SpicyMarinara commented on GitHub (Sep 14, 2026): Taking this in the assigned sweep for @SpicyMarinara, with Codex implementation tracked in draft #6193. Existing open PRs were checked before starting. I will preserve the current staging fixes and verify this issue’s remaining behavior before closing it.
Author
Owner

@SpicyMarinara commented on GitHub (Sep 14, 2026):

Fixed on staging in #6193. POSIX SIGHUP now follows normal shutdown, and both direct and development pretty stdout loggers tolerate a genuinely closed terminal. Real macOS PTY tests cover idle, busy and live-terminal SIGHUP in production and development: confirmed pending saves persist, the writer lease is released, the heartbeat is clean and no child survives. File/custom/unrelated logging errors remain visible. Linux and Windows checks are also covered by CI.

<!-- gh-comment-id:5664677118 --> @SpicyMarinara commented on GitHub (Sep 14, 2026): Fixed on staging in #6193. POSIX SIGHUP now follows normal shutdown, and both direct and development pretty stdout loggers tolerate a genuinely closed terminal. Real macOS PTY tests cover idle, busy and live-terminal SIGHUP in production and development: confirmed pending saves persist, the writer lease is released, the heartbeat is clean and no child survives. File/custom/unrelated logging errors remain visible. Linux and Windows checks are also covered by CI.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#3445
No description provided.