[PR #5797] [MERGED] feat(server): report how the previous session ended (#5506 diagnostics) #6936

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

📋 Pull Request Information

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

Base: staging ← Head: feat/termux-unclean-exit-postmortem


📝 Commits (4)

  • a9de922 feat(server): session heartbeat postmortem for external kills (#5506)
  • 58ecbf0 fix(server): classify session endings honestly (adversarial review)
  • 57a19cb fix(server): validate the record and keep heartbeat ownership (CodeRabbit)
  • 266a7a9 fix(server): validate the history file and de-flake the lane (CodeRabbit)

📊 Changes

9 files changed (+699 additions, -0 deletions)

View changed files

📝 CHANGELOG.md (+2 -0)
📝 packages/client/src/components/panels/SettingsPanel.tsx (+21 -0)
📝 packages/client/src/lib/support-diagnostics.ts (+67 -0)
📝 packages/server/src/app.ts (+8 -0)
📝 packages/server/src/index.ts (+11 -0)
➕ packages/server/src/lib/session-postmortem.ts (+359 -0)
📝 packages/server/src/routes/admin.routes.ts (+4 -0)
📝 packages/server/src/routes/updates.routes.ts (+4 -0)
➕ scripts/regressions/termux-postmortem.regression.ts (+223 -0)

📄 Description

Linked issue

Diagnostics for #5506 (Termux server dies silently in the background). Not a fix for the root cause — a fix needs to know who kills the process, which is exactly what no current log can say.

Why this change

  • When Android ends a Termux server (phantom process killer, battery management, low-memory killer), the process gets SIGKILL: no handler runs, nothing is logged, and the launcher log simply stops mid-stream. From a support thread, that is indistinguishable from a crash, a reboot, or a network problem — as two logs from an affected tester this week show, both ending in silence after a clean startup.
  • The next startup is the only witness available, so this makes it one. The result is that a tester's pasted Support Diagnostics answers "what ended the last session, when, after how long, at what memory, and did the device reboot" without anyone having to ask.
  • Concretely, the tester whose logs prompted this is on Android 15 with RSS ~119 MiB against a 1120 MiB heap limit — memory pressure is ruled out, and the profile fits the phantom process killer. The evidence this feature produces (repeated stops, modest RSS, no reboot in between) is what confirms or refutes that.

What changed

  • Silent heartbeat (packages/server/src/lib/session-postmortem.ts): a small JSON file in DATA_DIR/diagnostics refreshed every 30s with pid, boot id, start time, last-alive time and RSS/heap. Written atomically (tmp + rename) so a kill mid-write cannot corrupt the previous beat, unref'd so it never holds the process open, and console-silent at runtime by design — users read the console for other things. The only console surface is one line at startup; the regression pins the whole module to exactly two logger calls, both in the startup path.
  • Endings stamp themselves via process.on("exit"): every deliberate ending reaches that event (signal shutdown, in-app update, Advanced Settings restart, a fatal crash) and a SIGKILL reaches nothing — which is precisely the signal. Hooking the process event rather than each shutdown path means a future fifth exit path cannot silently rot the classification.
  • Honest classification — the previous session reports as one of:
    • ended with its kind: clean, crash (uncaughtException / unhandledRejection name themselves), or restart (update / settings restart name themselves);
    • unclean with the full record (last alive, uptime, RSS/heap at death, boot-id reboot detection);
    • unknown with a reason — first run, unreadable record, or a still-live previous pid (a restart's detached child racing its parent, or two servers sharing one DATA_DIR). "unknown" never renders as a clean shutdown.
  • Surfaces: one startup log line; previousSession + uncleanExitCount on /api/health; and Previous session: / Sessions ended without shutdown: lines in Support Diagnostics. The count reports even when the last session ended normally — a history of kills is the pattern worth seeing.
  • New termux-postmortem regression lane and a CHANGELOG entry.

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

  • Commands run locally (all passing): node ./scripts/run-regressions.mjs --filter termux-postmortem (classifier semantics, a real temp-DATA_DIR round trip, source pins), server lint, client lint, pnpm build.
  • A multi-agent adversarial review ran over the first cut; every confirmed finding is folded in (details in the second commit message — notably that the update/restart flows were being reported as Android kills, and that an unknown fate rendered as "shut down cleanly").
  • Manual verification that would still be valuable, ideally on a phone: start the server, kill -9 it, restart and confirm the startup line and the Support Diagnostics lines describe the kill; then stop it with Ctrl+C, restart, and confirm it reports a clean shutdown; then use the in-app updater and confirm the next start says "restarted itself", not a kill.

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)

UI evidence (if applicable)

Not applicable — server-side diagnostics plus two text lines in the Support Diagnostics copy block.

Summary by CodeRabbit

  • New Features
    • Added previous-session detection for normal shutdowns, crashes, restarts, updates, and unknown outcomes.
    • Support Diagnostics now displays the previous session’s status, runtime details, last activity, memory usage, and device reboot information.
    • Added a count of sessions that ended without a recorded shutdown.
    • Unknown or unavailable session records are clearly reported without making assumptions.

🔄 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/5797 **Author:** [@kolacheee](https://github.com/kolacheee) **Created:** 9/3/2026 **Status:** ✅ Merged **Merged:** 9/3/2026 **Merged by:** [@kolacheee](https://github.com/kolacheee) **Base:** `staging` ← **Head:** `feat/termux-unclean-exit-postmortem` --- ### 📝 Commits (4) - [`a9de922`](https://github.com/Pasta-Devs/Marinara-Engine/commit/a9de9223c75b97c219b1e3e9333f17e8a7fcebd8) feat(server): session heartbeat postmortem for external kills (#5506) - [`58ecbf0`](https://github.com/Pasta-Devs/Marinara-Engine/commit/58ecbf01ee4d8d8159e0f2e5acd97f7c339ab6ca) fix(server): classify session endings honestly (adversarial review) - [`57a19cb`](https://github.com/Pasta-Devs/Marinara-Engine/commit/57a19cb78bef6170dbfa897a55b3f80bc211af02) fix(server): validate the record and keep heartbeat ownership (CodeRabbit) - [`266a7a9`](https://github.com/Pasta-Devs/Marinara-Engine/commit/266a7a936aa66d763f5ed4940e53722c99582cf9) fix(server): validate the history file and de-flake the lane (CodeRabbit) ### 📊 Changes **9 files changed** (+699 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `CHANGELOG.md` (+2 -0) 📝 `packages/client/src/components/panels/SettingsPanel.tsx` (+21 -0) 📝 `packages/client/src/lib/support-diagnostics.ts` (+67 -0) 📝 `packages/server/src/app.ts` (+8 -0) 📝 `packages/server/src/index.ts` (+11 -0) ➕ `packages/server/src/lib/session-postmortem.ts` (+359 -0) 📝 `packages/server/src/routes/admin.routes.ts` (+4 -0) 📝 `packages/server/src/routes/updates.routes.ts` (+4 -0) ➕ `scripts/regressions/termux-postmortem.regression.ts` (+223 -0) </details> ### 📄 Description ## Linked issue Diagnostics for #5506 (Termux server dies silently in the background). Not a fix for the root cause — a fix needs to know *who* kills the process, which is exactly what no current log can say. ## Why this change - When Android ends a Termux server (phantom process killer, battery management, low-memory killer), the process gets `SIGKILL`: no handler runs, nothing is logged, and the launcher log simply stops mid-stream. From a support thread, that is indistinguishable from a crash, a reboot, or a network problem — as two logs from an affected tester this week show, both ending in silence after a clean startup. - The next startup is the only witness available, so this makes it one. The result is that a tester's pasted Support Diagnostics answers "what ended the last session, when, after how long, at what memory, and did the device reboot" without anyone having to ask. - Concretely, the tester whose logs prompted this is on Android 15 with RSS ~119 MiB against a 1120 MiB heap limit — memory pressure is ruled out, and the profile fits the phantom process killer. The evidence this feature produces (repeated stops, modest RSS, no reboot in between) is what confirms or refutes that. ## What changed - **Silent heartbeat** (`packages/server/src/lib/session-postmortem.ts`): a small JSON file in `DATA_DIR/diagnostics` refreshed every 30s with pid, boot id, start time, last-alive time and RSS/heap. Written atomically (tmp + rename) so a kill mid-write cannot corrupt the previous beat, `unref`'d so it never holds the process open, and **console-silent at runtime by design** — users read the console for other things. The only console surface is one line at startup; the regression pins the whole module to exactly two logger calls, both in the startup path. - **Endings stamp themselves via `process.on("exit")`**: every deliberate ending reaches that event (signal shutdown, in-app update, Advanced Settings restart, a fatal crash) and a `SIGKILL` reaches nothing — which is precisely the signal. Hooking the process event rather than each shutdown path means a future fifth exit path cannot silently rot the classification. - **Honest classification** — the previous session reports as one of: - `ended` with its kind: `clean`, `crash` (uncaughtException / unhandledRejection name themselves), or `restart` (update / settings restart name themselves); - `unclean` with the full record (last alive, uptime, RSS/heap at death, boot-id reboot detection); - `unknown` with a reason — first run, unreadable record, or a still-live previous pid (a restart's detached child racing its parent, or two servers sharing one `DATA_DIR`). **"unknown" never renders as a clean shutdown.** - **Surfaces**: one startup log line; `previousSession` + `uncleanExitCount` on `/api/health`; and `Previous session:` / `Sessions ended without shutdown:` lines in Support Diagnostics. The count reports even when the last session ended normally — a history of kills is the pattern worth seeing. - New `termux-postmortem` regression lane and a CHANGELOG entry. ## 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 - Commands run locally (all passing): `node ./scripts/run-regressions.mjs --filter termux-postmortem` (classifier semantics, a real temp-`DATA_DIR` round trip, source pins), server lint, client lint, `pnpm build`. - A multi-agent adversarial review ran over the first cut; every confirmed finding is folded in (details in the second commit message — notably that the update/restart flows were being reported as Android kills, and that an unknown fate rendered as "shut down cleanly"). - Manual verification that would still be valuable, ideally on a phone: start the server, `kill -9` it, restart and confirm the startup line and the Support Diagnostics lines describe the kill; then stop it with Ctrl+C, restart, and confirm it reports a clean shutdown; then use the in-app updater and confirm the next start says "restarted itself", **not** a kill. ## Docs and release impact - [ ] No docs changes needed - [x] 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-side diagnostics plus two text lines in the Support Diagnostics copy block. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Added previous-session detection for normal shutdowns, crashes, restarts, updates, and unknown outcomes. - Support Diagnostics now displays the previous session’s status, runtime details, last activity, memory usage, and device reboot information. - Added a count of sessions that ended without a recorded shutdown. - Unknown or unavailable session records are clearly reported without making assumptions. <!-- 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:03 +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#6936
No description provided.