[PR #6195] [CLOSED] fix(server): shut down cleanly when the terminal window closes #7087

Closed
opened 2026-10-23 04:16:25 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/6195
Author: @kolacheee
Created: 9/14/2026
Status: ❌ Closed

Base: staging ← Head: fix/sighup-graceful-shutdown


📝 Commits (1)

  • 8d29574 fix(server): shut down cleanly when the terminal window closes

📊 Changes

6 files changed (+294 additions, -1 deletions)

View changed files

📝 CHANGELOG.md (+1 -0)
📝 docs/TROUBLESHOOTING.md (+1 -1)
📝 packages/server/src/app.ts (+4 -0)
📝 packages/server/src/index.ts (+12 -0)
📝 packages/server/src/lib/logger.ts (+45 -0)
➕ scripts/regressions/server-hangup-shutdown.regression.ts (+231 -0)

📄 Description

Linked issue

Closes #6183

Why this change

Closing the terminal window that runs start.sh or start-termux.sh (or losing that SSH session) hangs up the whole launcher job. The server had no SIGHUP handler, so Node ended it on the spot: the storage writer lease stayed on disk for the next start to reclaim (users see the .writer-lease folder and the reclaim warning and assume something broke), up to ~750 ms of already-confirmed saves could be lost, a running local-model sidecar was not reaped, and Support Diagnostics reported the session as a force-quit. Only Ctrl+C avoided it.

Handling the signal alone is not enough, which is the part worth knowing when reviewing: by the time the shell forwards SIGHUP the pty is gone and stdout answers EIO. Pino only tolerates EPIPE, and its exit-time flushSync retries a buffered line every 100 ms without end. A close that finishes before the failed async write reports back (the normal case, ~2 ms) therefore released the lease but then hung until the launcher's ten-second SIGKILL and never stamped its exit - measured at 10.0 s / exit 137 with a real pty before the second half of this fix.

What changed

  • packages/server/src/index.ts: SIGHUP now runs the same shutdown() as SIGTERM/SIGINT on non-Windows platforms. Windows raises SIGHUP for a closed console through a different mechanism (#6119) and is deliberately left as is.
  • packages/server/src/lib/logger.ts: guardLoggerAgainstLostOutput() extends Pino's dead-pipe tolerance to a dead terminal for a Pino instance - on an EIO/EPIPE 'error' from the underlying stream it stops writing the way Pino already does for EPIPE (other errors still surface as before). A process.on("exit") hook registered before any Pino instance exists silences the guarded streams when stdout was a terminal at startup and isatty(1) is false at exit, so Pino's exit flush has nothing to retry. Applied to the shared logger here and to Fastify's own app.log in app.ts.
  • scripts/regressions/server-hangup-shutdown.regression.ts (new): stage 1 hangs up a pipe-backed launcher job with a process-group SIGHUP the way a shell does and asserts the shutdown lines, launcher exit 0, lease released, and a clean session stamp; stage 2 hangs up a real pty through python3's pty module (skipped with a log line when python3 is missing) and additionally asserts the job ends well under the SIGKILL backstop. Red on unchanged staging (missing shutdown line), green with this change (pty stage completes in ~55 ms).
  • docs/TROUBLESHOOTING.md: the "Restart Server does not return" section now says closing the terminal on macOS/Linux/Termux stops the server the same way Ctrl+C does.
  • CHANGELOG.md: Unreleased entry.

Not changed: scripts/run-server.mjs already relays SIGHUP; that relay was never the problem (the shell delivers SIGHUP to the server directly at the same moment). No nohup special-casing: Node resets signal dispositions at startup, so a nohup'd Node server never ignored SIGHUP before this change either (verified: nohup node -e 'setInterval(()=>{},1000)' & dies on kill -HUP).

Known limitation, shared with SIGTERM/SIGINT: the handler is registered where those two are, after buildApp() returns, because shutdown() needs the app and the env watcher. A hangup that lands during the few seconds of startup still ends the process abruptly, exactly as SIGTERM does today; the next start reclaims the lease. Moving all three signals ahead of buildApp() with a pending-signal state is a separate change.

Validation

  • pnpm check passes locally
  • Container (Docker / Podman) built and ran without issue
  • Ran the app, clicked through the changes manually
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
  • Above manual verification completed (describe below)
  • Read and followed CONTRIBUTING.md

Manual verification notes

Automated evidence from the authoring machine (Fedora Linux, Node v24.21.0), not a claim of manual testing on other platforms:

  • pnpm check passed on this branch.
  • New lane node scripts/run-regressions.mjs --filter scripts/regressions/server-hangup-shutdown.regression.ts: passes here (pipe stage and real-pty stage); fails on unchanged origin/staging at the "Received SIGHUP; shutting down" assertion.
  • Existing lanes server-signal-shutdown, shutdown-deadline, restart-supervisor, file-backed-shutdown still pass.
  • Real-terminal reproduction with a Python pty harness (interactive bash in a pty running node scripts/run-server.mjs dist/index.js, then closing the pty master), on the built dist: before this change the server died instantly with the lease left behind and no exit stamp; with only the SIGHUP handler it released the lease but hung 10.0 s until the launcher's SIGKILL (exit 137) with no exit stamp; with this change it exits in 0.1 s, the lease is gone, the heartbeat reads exitKind: "clean", and the next start prints no reclaim warning and no force-quit report. Same result at the default LOG_LEVEL=warn and at LOG_LEVEL=info. A hangup with the terminal still alive (kill -HUP on the shell) prints both shutdown lines normally.

Please verify before merging:

  • Manually verify on macOS: run ./start.sh, close the Terminal window, confirm packages/server/data/storage/.writer-lease is gone and the next ./start.sh prints no "Reclaimed the writer lease" or "ended without shutting down" line.
  • Manually verify on Termux: run start-termux.sh, swipe the session closed, confirm the same two things.
  • Manually verify a local model is stopped when the terminal closes (start the sidecar first, close the window, check no llama-server remains).
  • Manually verify Windows behavior is unchanged (this PR does not register SIGHUP on win32).

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed
  • Translated docs on the docs-i18n branch updated to match, or a [docs-i18n] follow-up issue opened (see CONTRIBUTING.md § Translated documentation)
  • Version/release files updated (only if this PR includes a version bump)

docs/TROUBLESHOOTING.md changed one paragraph; the docs-i18n mirror is pending a batched [docs-i18n] follow-up.

UI evidence (if applicable)

Not applicable: server-side shutdown behavior only.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/6195 **Author:** [@kolacheee](https://github.com/kolacheee) **Created:** 9/14/2026 **Status:** ❌ Closed **Base:** `staging` ← **Head:** `fix/sighup-graceful-shutdown` --- ### 📝 Commits (1) - [`8d29574`](https://github.com/Pasta-Devs/Marinara-Engine/commit/8d295742fbe77aa816d464f812c36e6b74b634d8) fix(server): shut down cleanly when the terminal window closes ### 📊 Changes **6 files changed** (+294 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `CHANGELOG.md` (+1 -0) 📝 `docs/TROUBLESHOOTING.md` (+1 -1) 📝 `packages/server/src/app.ts` (+4 -0) 📝 `packages/server/src/index.ts` (+12 -0) 📝 `packages/server/src/lib/logger.ts` (+45 -0) ➕ `scripts/regressions/server-hangup-shutdown.regression.ts` (+231 -0) </details> ### 📄 Description ## Linked issue Closes #6183 ## Why this change Closing the terminal window that runs `start.sh` or `start-termux.sh` (or losing that SSH session) hangs up the whole launcher job. The server had no `SIGHUP` handler, so Node ended it on the spot: the storage writer lease stayed on disk for the next start to reclaim (users see the `.writer-lease` folder and the reclaim warning and assume something broke), up to ~750 ms of already-confirmed saves could be lost, a running local-model sidecar was not reaped, and Support Diagnostics reported the session as a force-quit. Only Ctrl+C avoided it. Handling the signal alone is not enough, which is the part worth knowing when reviewing: by the time the shell forwards `SIGHUP` the pty is gone and stdout answers `EIO`. Pino only tolerates `EPIPE`, and its exit-time `flushSync` retries a buffered line every 100 ms without end. A close that finishes before the failed async write reports back (the normal case, ~2 ms) therefore released the lease but then hung until the launcher's ten-second SIGKILL and never stamped its exit - measured at 10.0 s / exit 137 with a real pty before the second half of this fix. ## What changed - `packages/server/src/index.ts`: `SIGHUP` now runs the same `shutdown()` as `SIGTERM`/`SIGINT` on non-Windows platforms. Windows raises `SIGHUP` for a closed console through a different mechanism (#6119) and is deliberately left as is. - `packages/server/src/lib/logger.ts`: `guardLoggerAgainstLostOutput()` extends Pino's dead-pipe tolerance to a dead terminal for a Pino instance - on an `EIO`/`EPIPE` `'error'` from the underlying stream it stops writing the way Pino already does for `EPIPE` (other errors still surface as before). A `process.on("exit")` hook registered before any Pino instance exists silences the guarded streams when stdout was a terminal at startup and `isatty(1)` is false at exit, so Pino's exit flush has nothing to retry. Applied to the shared logger here and to Fastify's own `app.log` in `app.ts`. - `scripts/regressions/server-hangup-shutdown.regression.ts` (new): stage 1 hangs up a pipe-backed launcher job with a process-group `SIGHUP` the way a shell does and asserts the shutdown lines, launcher exit 0, lease released, and a `clean` session stamp; stage 2 hangs up a real pty through `python3`'s `pty` module (skipped with a log line when `python3` is missing) and additionally asserts the job ends well under the SIGKILL backstop. Red on unchanged `staging` (missing shutdown line), green with this change (pty stage completes in ~55 ms). - `docs/TROUBLESHOOTING.md`: the "Restart Server does not return" section now says closing the terminal on macOS/Linux/Termux stops the server the same way Ctrl+C does. - `CHANGELOG.md`: Unreleased entry. Not changed: `scripts/run-server.mjs` already relays `SIGHUP`; that relay was never the problem (the shell delivers `SIGHUP` to the server directly at the same moment). No `nohup` special-casing: Node resets signal dispositions at startup, so a `nohup`'d Node server never ignored `SIGHUP` before this change either (verified: `nohup node -e 'setInterval(()=>{},1000)' &` dies on `kill -HUP`). Known limitation, shared with `SIGTERM`/`SIGINT`: the handler is registered where those two are, after `buildApp()` returns, because `shutdown()` needs the app and the env watcher. A hangup that lands during the few seconds of startup still ends the process abruptly, exactly as `SIGTERM` does today; the next start reclaims the lease. Moving all three signals ahead of `buildApp()` with a pending-signal state is a separate change. ## Validation - [ ] `pnpm check` passes locally - [ ] Container (Docker / Podman) built and ran without issue - [ ] Ran the app, clicked through the changes manually - [ ] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [ ] Above manual verification completed (describe below) - [ ] Read and followed `CONTRIBUTING.md` ### Manual verification notes Automated evidence from the authoring machine (Fedora Linux, Node v24.21.0), not a claim of manual testing on other platforms: - `pnpm check` passed on this branch. - New lane `node scripts/run-regressions.mjs --filter scripts/regressions/server-hangup-shutdown.regression.ts`: passes here (pipe stage and real-pty stage); fails on unchanged `origin/staging` at the "Received SIGHUP; shutting down" assertion. - Existing lanes `server-signal-shutdown`, `shutdown-deadline`, `restart-supervisor`, `file-backed-shutdown` still pass. - Real-terminal reproduction with a Python pty harness (interactive bash in a pty running `node scripts/run-server.mjs dist/index.js`, then closing the pty master), on the built `dist`: before this change the server died instantly with the lease left behind and no exit stamp; with only the `SIGHUP` handler it released the lease but hung 10.0 s until the launcher's SIGKILL (exit 137) with no exit stamp; with this change it exits in 0.1 s, the lease is gone, the heartbeat reads `exitKind: "clean"`, and the next start prints no reclaim warning and no force-quit report. Same result at the default `LOG_LEVEL=warn` and at `LOG_LEVEL=info`. A hangup with the terminal still alive (`kill -HUP` on the shell) prints both shutdown lines normally. Please verify before merging: - [ ] Manually verify on macOS: run `./start.sh`, close the Terminal window, confirm `packages/server/data/storage/.writer-lease` is gone and the next `./start.sh` prints no "Reclaimed the writer lease" or "ended without shutting down" line. - [ ] Manually verify on Termux: run `start-termux.sh`, swipe the session closed, confirm the same two things. - [ ] Manually verify a local model is stopped when the terminal closes (start the sidecar first, close the window, check no `llama-server` remains). - [ ] Manually verify Windows behavior is unchanged (this PR does not register `SIGHUP` on `win32`). ## Docs and release impact - [ ] No docs changes needed - [ ] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed - [ ] Translated docs on the `docs-i18n` branch updated to match, or a `[docs-i18n]` follow-up issue opened (see CONTRIBUTING.md § Translated documentation) - [ ] Version/release files updated (only if this PR includes a version bump) `docs/TROUBLESHOOTING.md` changed one paragraph; the `docs-i18n` mirror is pending a batched `[docs-i18n]` follow-up. ## UI evidence (if applicable) Not applicable: server-side shutdown behavior only. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-23 04:16:25 +07:00
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#7087
No description provided.