[PR #5863] [MERGED] fix(server): bound graceful shutdown so supervisors never SIGKILL it #6972

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

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/5863
Author: @kolacheee
Created: 9/5/2026
Status: ✅ Merged
Merged: 9/5/2026
Merged by: @kolacheee

Base: staging ← Head: fix/5838-shutdown-deadline


📝 Commits (4)

  • 27525f6 fix(server): bound graceful shutdown so supervisors never SIGKILL it (#5838)
  • 97d8ed6 fix(server): keep the force-exit watchdog referenced (CodeRabbit)
  • f99278c Merge branch 'staging' into fix/5838-shutdown-deadline
  • 57fe8f7 fix(server): drop the unused force-exit timer handle (CI TS6133)

📊 Changes

9 files changed (+276 additions, -19 deletions)

View changed files

📝 CHANGELOG.md (+1 -0)
📝 packages/client/src/lib/support-diagnostics.ts (+7 -1)
📝 packages/server/src/index.ts (+5 -0)
📝 packages/server/src/lib/session-postmortem.ts (+2 -2)
➕ packages/server/src/lib/shutdown-deadline.ts (+79 -0)
📝 packages/server/src/routes/updates.routes.ts (+5 -0)
➕ scripts/regressions/shutdown-deadline.regression.ts (+154 -0)
📝 scripts/regressions/termux-postmortem.regression.ts (+18 -15)
📝 scripts/regressions/tsconfig.character-activity.json (+5 -1)

📄 Description

Linked issue

Follow-up to #5838 (closed by #5839) — the shutdown half of that investigation. No separate issue was opened; the field evidence and decision live in this #5838 comment.

Why this change

A SteamOS reporter's logs caught our graceful shutdown taking 39 seconds: holo-earlyoom sent SIGTERM at 16:24:35 and the process exited (cleanly, status 0) at 16:25:14. The cause is that shutdown() awaits app.close() without any bound, and Fastify's close waits for open connections — a phone browser holding an SSE stream is enough. earlyoom treats the stop as failed after ~10 s; Docker's stop timeout is 10 s; systemd has its own. Past any of those windows, an orderly stop escalates to a SIGKILL that bypasses the onClose chain (closeDB → fileStore.close → flush(true)) and drops the store's debounced writes. The slow-close bug is ours and platform-independent; the SteamOS box just happened to photograph it.

What changed

  • New lib/shutdown-deadline.ts — armShutdownDeadline(app, context) arms two unref'd watchdogs: at 4 s it severs open connections (app.server.closeAllConnections()) so close can proceed to the flush — the gentle remedy for the exact hang observed; at 8 s, if close is still stuck (e.g. inside the flush itself), it logs and force-exits — whatever hasn't finished by then is precisely what the supervisor was about to SIGKILL anyway, except we leave on our own terms with the session-postmortem exit stamp written.
  • Wired into both unprotected close paths: the signal shutdown in index.ts and the update-restart in updates.routes.ts (stamp → arm → close, so the "restart" postmortem stamp always lands before any possible exit).
  • admin.routes.ts deliberately not armed: it already severs connections on its own 30 s timer, and it spawns the relaunch child only after close — a blind stage-2 force-exit would leave the server down after a settings-restart click. The lane pins this reasoning so it isn't "unified" away later.
  • Exit code 0 on a forced exit, matching the restart routes' existing convention of never signaling a crash to a supervisor for an intentional stop.

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

  • Review findings applied before opening: a forced exit now stamps a new "forced" session exit kind instead of "clean" (a cut-short close may have truncated the flush, and a pasted Support Diagnostics must say so rather than alibi it — the client renders it in plain words); the lane's children are pinned to LOG_LEVEL=warn/NODE_ENV=production so a user's documented LOG_LEVEL knob can't fail the lane (the review demonstrated that failure live, and the fix is verified by running the lane under LOG_LEVEL=error); and the failure-path timers are sized to report their own assertion messages inside the lane runner's 30 s budget.
  • New shutdown-deadline lane: three child-process functional tests (via the same tsx resolution the lane runner uses) observe the real helper — stage-2 force-exits with status 0 and the expected log line while a hung close holds the loop open; stage-1 fires before stage-2 and actually invokes closeAllConnections; unref'd watchdogs never pin an otherwise-finished process — plus source pins for the wiring order at both call sites and the admin exemption.
  • termux-postmortem lane updated: its stamp-before-close pin now asserts the full stamp → arm → close order on the updates route.
  • Both lanes, server lint, and a full zero-error build pass locally. An adversarial multi-agent review ran before opening; its findings are addressed in the commits.
  • Not exercised on real SteamOS hardware — the 39 s field case is from the #5838 reporter's logs; the bounded behavior is proven by the child-process tests.

Docs and release impact

  • No docs changes needed (CHANGELOG entry included; behavior is internal shutdown robustness)
  • 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)

UI evidence (if applicable)

Not applicable — server shutdown behavior only.

Summary by CodeRabbit

  • Bug Fixes
    • Improved shutdown reliability when browser connections remain open.
    • Lingering connections are closed after 4 seconds, with automatic process exit after 8 seconds to reduce lost unsaved changes.
    • Update-triggered restarts now follow the same bounded shutdown behavior.
    • Previous-session diagnostics now identify forcibly ended processes.
    • Improved session history accuracy for interrupted or forcibly ended sessions.

🔄 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/5863 **Author:** [@kolacheee](https://github.com/kolacheee) **Created:** 9/5/2026 **Status:** ✅ Merged **Merged:** 9/5/2026 **Merged by:** [@kolacheee](https://github.com/kolacheee) **Base:** `staging` ← **Head:** `fix/5838-shutdown-deadline` --- ### 📝 Commits (4) - [`27525f6`](https://github.com/Pasta-Devs/Marinara-Engine/commit/27525f6ecf404334fd71eac367fff671ad7df7ab) fix(server): bound graceful shutdown so supervisors never SIGKILL it (#5838) - [`97d8ed6`](https://github.com/Pasta-Devs/Marinara-Engine/commit/97d8ed603cc16c24c6e2e73a20f19abd0c9ca7fe) fix(server): keep the force-exit watchdog referenced (CodeRabbit) - [`f99278c`](https://github.com/Pasta-Devs/Marinara-Engine/commit/f99278c8b024406075dd888ad91b397373b32882) Merge branch 'staging' into fix/5838-shutdown-deadline - [`57fe8f7`](https://github.com/Pasta-Devs/Marinara-Engine/commit/57fe8f79eb9dc01a3c0c6a2a8fbe38812b7c00f7) fix(server): drop the unused force-exit timer handle (CI TS6133) ### 📊 Changes **9 files changed** (+276 additions, -19 deletions) <details> <summary>View changed files</summary> 📝 `CHANGELOG.md` (+1 -0) 📝 `packages/client/src/lib/support-diagnostics.ts` (+7 -1) 📝 `packages/server/src/index.ts` (+5 -0) 📝 `packages/server/src/lib/session-postmortem.ts` (+2 -2) ➕ `packages/server/src/lib/shutdown-deadline.ts` (+79 -0) 📝 `packages/server/src/routes/updates.routes.ts` (+5 -0) ➕ `scripts/regressions/shutdown-deadline.regression.ts` (+154 -0) 📝 `scripts/regressions/termux-postmortem.regression.ts` (+18 -15) 📝 `scripts/regressions/tsconfig.character-activity.json` (+5 -1) </details> ### 📄 Description ## Linked issue Follow-up to #5838 (closed by #5839) — the shutdown half of that investigation. No separate issue was opened; the field evidence and decision live in [this #5838 comment](https://github.com/Pasta-Devs/Marinara-Engine/issues/5838#issuecomment-5550979170). ## Why this change A SteamOS reporter's logs caught our graceful shutdown taking **39 seconds**: `holo-earlyoom` sent SIGTERM at 16:24:35 and the process exited (cleanly, status 0) at 16:25:14. The cause is that `shutdown()` awaits `app.close()` without any bound, and Fastify's close waits for open connections — a phone browser holding an SSE stream is enough. earlyoom treats the stop as failed after ~10 s; Docker's stop timeout is 10 s; systemd has its own. Past any of those windows, an orderly stop escalates to a SIGKILL that bypasses the onClose chain (`closeDB → fileStore.close → flush(true)`) and drops the store's debounced writes. The slow-close bug is ours and platform-independent; the SteamOS box just happened to photograph it. ## What changed - **New `lib/shutdown-deadline.ts`** — `armShutdownDeadline(app, context)` arms two unref'd watchdogs: at **4 s** it severs open connections (`app.server.closeAllConnections()`) so close can proceed to the flush — the gentle remedy for the exact hang observed; at **8 s**, if close is still stuck (e.g. inside the flush itself), it logs and force-exits — whatever hasn't finished by then is precisely what the supervisor was about to SIGKILL anyway, except we leave on our own terms with the session-postmortem exit stamp written. - **Wired into both unprotected close paths**: the signal shutdown in `index.ts` and the update-restart in `updates.routes.ts` (stamp → arm → close, so the "restart" postmortem stamp always lands before any possible exit). - **`admin.routes.ts` deliberately not armed**: it already severs connections on its own 30 s timer, and it spawns the relaunch child only *after* close — a blind stage-2 force-exit would leave the server down after a settings-restart click. The lane pins this reasoning so it isn't "unified" away later. - Exit code 0 on a forced exit, matching the restart routes' existing convention of never signaling a crash to a supervisor for an intentional stop. ## 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 - Review findings applied before opening: a forced exit now stamps a new `"forced"` session exit kind instead of `"clean"` (a cut-short close may have truncated the flush, and a pasted Support Diagnostics must say so rather than alibi it — the client renders it in plain words); the lane's children are pinned to `LOG_LEVEL=warn`/`NODE_ENV=production` so a user's documented LOG_LEVEL knob can't fail the lane (the review demonstrated that failure live, and the fix is verified by running the lane under `LOG_LEVEL=error`); and the failure-path timers are sized to report their own assertion messages inside the lane runner's 30 s budget. - New `shutdown-deadline` lane: three **child-process functional tests** (via the same tsx resolution the lane runner uses) observe the real helper — stage-2 force-exits with status 0 and the expected log line while a hung close holds the loop open; stage-1 fires before stage-2 and actually invokes `closeAllConnections`; unref'd watchdogs never pin an otherwise-finished process — plus source pins for the wiring order at both call sites and the admin exemption. - `termux-postmortem` lane updated: its stamp-before-close pin now asserts the full stamp → arm → close order on the updates route. - Both lanes, server lint, and a full zero-error build pass locally. An adversarial multi-agent review ran before opening; its findings are addressed in the commits. - **Not exercised on real SteamOS hardware** — the 39 s field case is from the #5838 reporter's logs; the bounded behavior is proven by the child-process tests. ## Docs and release impact - [x] No docs changes needed (CHANGELOG entry included; behavior is internal shutdown robustness) - [ ] 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) ## UI evidence (if applicable) Not applicable — server shutdown behavior only. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Bug Fixes** - Improved shutdown reliability when browser connections remain open. - Lingering connections are closed after 4 seconds, with automatic process exit after 8 seconds to reduce lost unsaved changes. - Update-triggered restarts now follow the same bounded shutdown behavior. - Previous-session diagnostics now identify forcibly ended processes. - Improved session history accuracy for interrupted or forcibly ended sessions. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-23 04:15:22 +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#6972
No description provided.