[PR #5660] [MERGED] feat(server): detect host freezes and surface the Termux wake-lock outcome #6868

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

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/5660
Author: @kolacheee
Created: 8/30/2026
Status: ✅ Merged
Merged: 8/30/2026
Merged by: @kolacheee

Base: staging ← Head: feat/5655-5656-freeze-observability


📝 Commits (2)

  • 2fb9a65 feat(server): detect host freezes and surface the Termux wake-lock outcome
  • 03ae9cb fix(client): gate diagnostics copy on health settling; align the changelog claim

📊 Changes

9 files changed (+182 additions, -1 deletions)

View changed files

📝 CHANGELOG.md (+3 -0)
📝 packages/client/src/components/panels/SettingsPanel.tsx (+7 -1)
📝 packages/client/src/lib/support-diagnostics.ts (+7 -0)
📝 packages/server/src/app.ts (+6 -0)
📝 packages/server/src/index.ts (+3 -0)
➕ packages/server/src/lib/freeze-detector.ts (+81 -0)
➕ scripts/regressions/freeze-detector.regression.ts (+50 -0)
📝 scripts/regressions/open-issues.regression.ts (+2 -0)
📝 start-termux.sh (+23 -0)

📄 Description

Closes #5655
Closes #5656

Why

The Termux background-freeze triage (see #4956 and the #5655/#5656 issue family) hit an observability wall: when Android freezes the backgrounded server, nothing a user can paste shows it happened. An idle server logs nothing, every timer silently stops and resumes, and the one decisive fact — whether the launcher's wake lock engaged — existed only as one scroll-away line at launch. Diagnosing this week's report required asking the user to dig the session log's first lines out of ~/.marinara-engine/logs/.

What this adds

Freeze detector (#5655). An unref()'d 60-second tick (packages/server/src/lib/freeze-detector.ts) measures its own lateness; a tick beyond 2× its interval records the suspension ({detectedAt, gapMs, suspendedMs}) and logs a warning with the estimated length. Design decisions, each deliberate:

  • Wall-clock, not monotonic: CLOCK_MONOTONIC does not advance through an OS suspension — a monotonic detector would miss exactly the freezes this exists to catch. The cost (a forward clock-step fabricates a record; a backward step is silently absorbed) is acceptable for evidence-gathering.
  • Hedged attribution: a severe app-internal stall (a minutes-long synchronous flush under GC thrash — the #4730 pathology) reads identically to an OS freeze, so the warning says "the host OS froze or slept the server, or the server itself stalled that long" and points at the memory figures in /api/health for disambiguation, instead of steering every thread toward battery settings.
  • Desktop laptop-sleep fires it too — a true statement that explains "it hung overnight" reports there as well; the Termux guidance in the message is conditionally worded.
  • The timer never keeps the process alive (unref), and shutdown stops it explicitly.

Wake-lock surfacing (#5656). start-termux.sh exports MARINARA_WAKE_LOCK_STATUS=acquired|failed|unavailable from its existing branches and, when the lock is not held, prints a prominent boxed warning immediately before the server starts — including the corrected guidance that termux-wake-lock ships in core termux-tools (the Termux:API claim in the docs is wrong; that correction is #5659). /api/health now returns wakeLock and lastFreeze, and Copy Support Diagnostics renders both:

Background wake lock: acquired
Last detected freeze: ~143s suspension, thawed at 2026-08-30T…

Those two lines are exactly what the next freeze report will need.

Validation

  • New regression freeze-detector.regression.ts: pins the pure gap classifier, then the wired detector by genuinely blocking the event loop with Atomics.wait(900ms) against a 200ms interval. The on-time phase tolerates real CI stalls honestly: a freeze recorded there must itself reflect a genuinely late tick, distinguishing environment stalls from detector false positives. Green locally.
  • The support-diagnostics pin list in open-issues.regression.ts now covers the two new lines' default rendering.
  • Adversarial review (2 lenses) verified: every format-guard launcher pin still passes against the edited start-termux.sh (ran the guard), env inheritance into the server launch, /health consumers are all additive-safe (keep-alive, checkVersion, e2e, auth middlewares — no strict shape validation anywhere), unref/shutdown hygiene, and module-singleton safety. Its findings (attribution hedge, on-time-phase tolerance, empty-string env normalization, formatter pins) are all applied.
  • pnpm check green; bash -n start-termux.sh clean.
  • Manually verify on a Termux device: launch via start-termux.sh, confirm the wake-lock line appears in Copy Support Diagnostics; background Termux long enough to trigger a freeze, foreground, and confirm the session log shows the suspension warning and diagnostics show the freeze record.

Related follow-ups filed separately: #5657 (client unreachable-state UX), #5658 (checkVersion fetch leak), #5659 (TROUBLESHOOTING corrections).

Summary by CodeRabbit

  • New Features

    • Added Android/Termux host-suspension detection with freeze-duration logging.
    • Health and support diagnostics now report wake-lock status and the latest detected freeze.
    • Launcher warnings provide installation and battery-optimization guidance when wake locks are unavailable or fail.
  • Bug Fixes

    • Improved visibility into background suspension and wake-lock issues affecting server availability.
  • Tests

    • Added regression coverage for freeze detection and diagnostic reporting.

🔄 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/5660 **Author:** [@kolacheee](https://github.com/kolacheee) **Created:** 8/30/2026 **Status:** ✅ Merged **Merged:** 8/30/2026 **Merged by:** [@kolacheee](https://github.com/kolacheee) **Base:** `staging` ← **Head:** `feat/5655-5656-freeze-observability` --- ### 📝 Commits (2) - [`2fb9a65`](https://github.com/Pasta-Devs/Marinara-Engine/commit/2fb9a657b0ee74e7b3d1e03f19c7da370c42c901) feat(server): detect host freezes and surface the Termux wake-lock outcome - [`03ae9cb`](https://github.com/Pasta-Devs/Marinara-Engine/commit/03ae9cb4af7f1a9d09beba5a4729bde079479bf2) fix(client): gate diagnostics copy on health settling; align the changelog claim ### 📊 Changes **9 files changed** (+182 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `CHANGELOG.md` (+3 -0) 📝 `packages/client/src/components/panels/SettingsPanel.tsx` (+7 -1) 📝 `packages/client/src/lib/support-diagnostics.ts` (+7 -0) 📝 `packages/server/src/app.ts` (+6 -0) 📝 `packages/server/src/index.ts` (+3 -0) ➕ `packages/server/src/lib/freeze-detector.ts` (+81 -0) ➕ `scripts/regressions/freeze-detector.regression.ts` (+50 -0) 📝 `scripts/regressions/open-issues.regression.ts` (+2 -0) 📝 `start-termux.sh` (+23 -0) </details> ### 📄 Description Closes #5655 Closes #5656 ## Why The Termux background-freeze triage (see #4956 and the #5655/#5656 issue family) hit an observability wall: when Android freezes the backgrounded server, **nothing a user can paste shows it happened**. An idle server logs nothing, every timer silently stops and resumes, and the one decisive fact — whether the launcher's wake lock engaged — existed only as one scroll-away line at launch. Diagnosing this week's report required asking the user to dig the session log's first lines out of `~/.marinara-engine/logs/`. ## What this adds **Freeze detector (#5655).** An `unref()`'d 60-second tick (`packages/server/src/lib/freeze-detector.ts`) measures its own lateness; a tick beyond 2× its interval records the suspension (`{detectedAt, gapMs, suspendedMs}`) and logs a warning with the estimated length. Design decisions, each deliberate: - **Wall-clock, not monotonic**: `CLOCK_MONOTONIC` does not advance through an OS suspension — a monotonic detector would miss exactly the freezes this exists to catch. The cost (a forward clock-step fabricates a record; a backward step is silently absorbed) is acceptable for evidence-gathering. - **Hedged attribution**: a severe app-internal stall (a minutes-long synchronous flush under GC thrash — the #4730 pathology) reads identically to an OS freeze, so the warning says "the host OS froze or slept the server, or the server itself stalled that long" and points at the memory figures in `/api/health` for disambiguation, instead of steering every thread toward battery settings. - **Desktop laptop-sleep fires it too** — a true statement that explains "it hung overnight" reports there as well; the Termux guidance in the message is conditionally worded. - The timer never keeps the process alive (`unref`), and shutdown stops it explicitly. **Wake-lock surfacing (#5656).** `start-termux.sh` exports `MARINARA_WAKE_LOCK_STATUS=acquired|failed|unavailable` from its existing branches and, when the lock is *not* held, prints a prominent boxed warning immediately before the server starts — including the corrected guidance that `termux-wake-lock` ships in core `termux-tools` (the Termux:API claim in the docs is wrong; that correction is #5659). `/api/health` now returns `wakeLock` and `lastFreeze`, and **Copy Support Diagnostics renders both**: ``` Background wake lock: acquired Last detected freeze: ~143s suspension, thawed at 2026-08-30T… ``` Those two lines are exactly what the next freeze report will need. ## Validation - New regression `freeze-detector.regression.ts`: pins the pure gap classifier, then the wired detector by *genuinely blocking the event loop* with `Atomics.wait(900ms)` against a 200ms interval. The on-time phase tolerates real CI stalls honestly: a freeze recorded there must itself reflect a genuinely late tick, distinguishing environment stalls from detector false positives. Green locally. - The support-diagnostics pin list in `open-issues.regression.ts` now covers the two new lines' default rendering. - Adversarial review (2 lenses) verified: every format-guard launcher pin still passes against the edited `start-termux.sh` (ran the guard), env inheritance into the server launch, `/health` consumers are all additive-safe (keep-alive, `checkVersion`, e2e, auth middlewares — no strict shape validation anywhere), unref/shutdown hygiene, and module-singleton safety. Its findings (attribution hedge, on-time-phase tolerance, empty-string env normalization, formatter pins) are all applied. - `pnpm check` green; `bash -n start-termux.sh` clean. - Manually verify on a Termux device: launch via `start-termux.sh`, confirm the wake-lock line appears in Copy Support Diagnostics; background Termux long enough to trigger a freeze, foreground, and confirm the session log shows the suspension warning and diagnostics show the freeze record. Related follow-ups filed separately: #5657 (client unreachable-state UX), #5658 (checkVersion fetch leak), #5659 (TROUBLESHOOTING corrections). <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added Android/Termux host-suspension detection with freeze-duration logging. * Health and support diagnostics now report wake-lock status and the latest detected freeze. * Launcher warnings provide installation and battery-optimization guidance when wake locks are unavailable or fail. * **Bug Fixes** * Improved visibility into background suspension and wake-lock issues affecting server availability. * **Tests** * Added regression coverage for freeze detection and diagnostic reporting. <!-- 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:14:27 +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#6868
No description provided.