mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[PR #6195] [CLOSED] fix(server): shut down cleanly when the terminal window closes #7087
Labels
No labels
android
bug
bugfix
ci
client
code freeze pause
container
dependencies
docs
documentation
duplicate
enhancement
feature
feature
feature
feature-creep
fixed-in-refactor
fixed-in-staging
good first issue
help wanted
in-pr
in-pr
needs-codeowner-decision
normal-scope
old-codebase
old-codebase
pull-request
qol
question
refactor
regression
revisit later
roadmap
scope-risk
scope-risk
server
server
shared
soup
upstream
windows
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Pasta-Devs/Marinara-Engine#7087
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 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)
8d29574fix(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.shorstart-termux.sh(or losing that SSH session) hangs up the whole launcher job. The server had noSIGHUPhandler, so Node ended it on the spot: the storage writer lease stayed on disk for the next start to reclaim (users see the.writer-leasefolder 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
SIGHUPthe pty is gone and stdout answersEIO. Pino only toleratesEPIPE, and its exit-timeflushSyncretries 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:SIGHUPnow runs the sameshutdown()asSIGTERM/SIGINTon non-Windows platforms. Windows raisesSIGHUPfor 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 anEIO/EPIPE'error'from the underlying stream it stops writing the way Pino already does forEPIPE(other errors still surface as before). Aprocess.on("exit")hook registered before any Pino instance exists silences the guarded streams when stdout was a terminal at startup andisatty(1)is false at exit, so Pino's exit flush has nothing to retry. Applied to the shared logger here and to Fastify's ownapp.loginapp.ts.scripts/regressions/server-hangup-shutdown.regression.ts(new): stage 1 hangs up a pipe-backed launcher job with a process-groupSIGHUPthe way a shell does and asserts the shutdown lines, launcher exit 0, lease released, and acleansession stamp; stage 2 hangs up a real pty throughpython3'sptymodule (skipped with a log line whenpython3is missing) and additionally asserts the job ends well under the SIGKILL backstop. Red on unchangedstaging(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.mjsalready relaysSIGHUP; that relay was never the problem (the shell deliversSIGHUPto the server directly at the same moment). Nonohupspecial-casing: Node resets signal dispositions at startup, so anohup'd Node server never ignoredSIGHUPbefore this change either (verified:nohup node -e 'setInterval(()=>{},1000)' &dies onkill -HUP).Known limitation, shared with
SIGTERM/SIGINT: the handler is registered where those two are, afterbuildApp()returns, becauseshutdown()needs the app and the env watcher. A hangup that lands during the few seconds of startup still ends the process abruptly, exactly asSIGTERMdoes today; the next start reclaims the lease. Moving all three signals ahead ofbuildApp()with a pending-signal state is a separate change.Validation
pnpm checkpasses locallyCONTRIBUTING.mdManual verification notes
Automated evidence from the authoring machine (Fedora Linux, Node v24.21.0), not a claim of manual testing on other platforms:
pnpm checkpassed on this branch.node scripts/run-regressions.mjs --filter scripts/regressions/server-hangup-shutdown.regression.ts: passes here (pipe stage and real-pty stage); fails on unchangedorigin/stagingat the "Received SIGHUP; shutting down" assertion.server-signal-shutdown,shutdown-deadline,restart-supervisor,file-backed-shutdownstill pass.node scripts/run-server.mjs dist/index.js, then closing the pty master), on the builtdist: before this change the server died instantly with the lease left behind and no exit stamp; with only theSIGHUPhandler 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 readsexitKind: "clean", and the next start prints no reclaim warning and no force-quit report. Same result at the defaultLOG_LEVEL=warnand atLOG_LEVEL=info. A hangup with the terminal still alive (kill -HUPon the shell) prints both shutdown lines normally.Please verify before merging:
./start.sh, close the Terminal window, confirmpackages/server/data/storage/.writer-leaseis gone and the next./start.shprints no "Reclaimed the writer lease" or "ended without shutting down" line.start-termux.sh, swipe the session closed, confirm the same two things.llama-serverremains).SIGHUPonwin32).Docs and release impact
docs-i18nbranch updated to match, or a[docs-i18n]follow-up issue opened (see CONTRIBUTING.md § Translated documentation)docs/TROUBLESHOOTING.mdchanged one paragraph; thedocs-i18nmirror 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.