mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[GH-ISSUE #6183] Closing the terminal window ends the server abruptly instead of shutting it down, leaving the storage writer lease behind #3445
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#3445
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?
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-leasefolder and the reclaim warning and reasonably assume something is wrong).How to reproduce (try it yourself)
Linux or macOS, any recent
staging(verified atf2219d815)../start.sh. Wait for the server'sMarinara Engine server listeningline.kill -HUP <pid of the shell>from another terminal.packages/server/data/storage/../start.shagain and read the first few seconds of output.What you'll see: at step 3 the
.writer-leasefolder 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 (thePrevious session (pid N) ended without shutting down ... a force-quit or power losswarning appears once the session is old enough to have a heartbeat).What should happen: the same clean stop as Ctrl+C.
Shutdown completeis reached (it cannot be printed - the terminal is gone - but the lease folder is removed, the session heartbeat is stampedclean, 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 areSIGTERMandSIGINT. There is noSIGHUPlistener, so Node applies the default action (terminate immediately) when the hangup arrives.scripts/run-server.mjs:9-20- the launcher supervisor already relaysSIGHUPto 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 receivesSIGHUPdirectly at the same moment and dies before the relay lands.packages/server/src/lib/logger.ts:17-20andpackages/server/src/app.ts:94-98- the two Pino instances write to stdout through Pino's default asynchronous stream. Pino's own guard (buildSafeSonicBoominpino/lib/tools.js) only toleratesEPIPE; a hung-up terminal answers every write withEIO, which Pino re-emits as an unhandled'error'. And Pino's exit-timeflushSync(sonic-boom/index.js, theflushSyncloop) retries a failed write every 100 ms without end for any error that is notEAGAIN/EBUSY.What's happening
Two layers, and the second only shows up once the first is fixed.
No
SIGHUPhandler, so the process is terminated by the signal itself. The debounced store flush, theonClosehook 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.Simply adding a
SIGHUPhandler that runs the existingshutdown()is not enough, and this is the part worth knowing before fixing it. By the time the shell forwardsSIGHUP, the pty is already gone, so stdout returnsEIO. 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. Atprocess.exit, Pino's exit hook callsflushSyncon the buffered "Shutdown complete" line, getsEIO, 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 -HUPon 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
index.ts, on non-Windows platforms, treatSIGHUPlikeSIGTERM:process.on("SIGHUP", () => void shutdown("SIGHUP")). Windows raisesSIGHUPfor a closed console through a different mechanism (#6119) and should be left alone until someone can verify it there.logger.ts, extend Pino's dead-pipe tolerance to a dead terminal, for both Pino instances (the shared logger and Fastify'sapp.log): on anEIO/EPIPE'error'from the underlying stream, replacewrite/end/flushSync/destroywith no-ops the way Pino already does forEPIPE; and register aprocess.on("exit")hook before any Pino instance exists that checks "stdout was a terminal at startup andisatty(1)is false now" and silences the streams, so Pino's exit flush has nothing to retry.isatty(1)reliably flips tofalseafter a pty hangup (verified).python3'sptymodule or util-linuxscriptcan provide the pty on Linux CI; the pipe-basedSIGHUPcase can run everywhere the existingserver-signal-shutdownlane runs.Related report
A Podman user on the v2.4.4 image reported having to delete
data/storage/.writer-leasebefore almost every launch. That is a different, already-fixed cause: on v2.4.4 apodman stopwith 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'shostId: nulllease (fixed on staging by #5744). Both were verified against thelatest(v2.4.4) andstagingGHCR images under rootless Podman 5.8.4:latestis blocked after a simulated reboot and leaves the lease on a stop with an in-flight request;stagingreclaims 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
f2219d815on 2026-09-13, using a Python pty harness (interactive bash in a pty runningnode 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.@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.
@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.