[GH-ISSUE #4706] [Issue]: Personal server-extension IPC: 25ms file polls x2 + 250ms watchdog + 1s heartbeat writes (~80 wakeups/s per extension, from boot) #2580

Closed
opened 2026-10-21 05:50:38 +07:00 by bsakaguchi · 0 comments
Owner

Originally created by @kolacheee on GitHub (Aug 7, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/4706

Originally assigned to: @kolacheee on GitHub.

Summary

Each running personal server extension costs ~80 timer wakeups per second plus one flash write per second, continuously from boot: the host polls the sandbox output file every 25ms and runs a 250ms double-stat watchdog; the runner polls its input file every 25ms and heartbeat-writes every 1s. Flagged inside #4629 during the mobile-battery investigation ("related, happy to split") but never fixed — on a Termux self-host with a server extension installed this is the largest remaining passive drain.

Expected behavior

Idle extensions cost near-zero wakeups; polling tightens only while traffic is flowing (startup handshake, storage bursts, stop), preserving today's latency where it matters.

Actual behavior

  • Host: personal-server-extension-runtime.ts:394 — outputPoller setInterval(25ms) doing an fstat (+read) per tick; :289-312 — watchdog setInterval(250ms) doing two stat()s per tick.
  • Runner: personal-extension-runner.mjs:226 — inputPoller setInterval(25ms); :228-230 — heartbeat writeFile every 1s (a flash write per second, forever).
  • Conditional: only spawns per enabled+approved extension with serverJs (runtime.ts:128, :239-242) — zero-extension installs pay nothing.

Steps to reproduce

  1. Install/enable one personal server extension.
  2. Observe the sandbox dir: input/output stat cadence + heartbeat mtime churn; ~80 wakeups/s in the two processes at full idle.

Environment

  • Marinara Engine version: staging d6275fa0b
  • Install type: any with server extensions; battery-critical on Termux
  • OS + version: any

Logs, screenshots, or video

Code trace, line references against staging d6275fa0b.

Additional context

Regression-safe fix (designed + red-teamed)

Keep the file transport untouched (it is the sandbox security design) — change only the polling discipline:

  1. Adaptive cadence, both sides. Replace the fixed intervals with self-scheduling unref'd setTimeout chains: host output poll 25ms while HOT / 1000ms while idle; runner input poll 25ms HOT / 250ms idle. HOT := startup not settled, or bytes moved / a message was sent within the last ~3s. Startup handshake and storage bursts keep today's 25ms feel; steady-state drops from 40 stats/s to ~1/s per side. Latency contract verified: no storage/tool deadline exists to trip (runner.mjs:39-45, runtime.ts:201-227 — no timeouts either direction; host→runner messages are exactly start/stop/storage-result), so the worst-case ≤1s first-message-after-silence delay is safe; stop responsiveness is bounded by the runner's 250ms idle cadence (rescale CLEANUP_TIMEOUT_MS 3000→3500 for margin).
  2. Heartbeat 1s → 5s with the watchdog stale-threshold rescaled proportionally (same missed-beat multiple), cutting flash writes 5×. Watchdog cadence 250ms → 1s.
  3. fs.watch only as an optional fast-path behind a runtime probe (inotify works on Termux home paths but is unreliable on FUSE/shared mounts) — never as the correctness mechanism; the poll chain remains the fallback.

Amendments from the adversarial pass (must be in the implementation):

  • MAX_PROTOCOL_BYTES false-kill: pollOutput reads all available bytes and checks outputBuffer.byteLength > 2MB before line-splitting (runtime.ts:336-344). At 25ms that approximates a per-message cap; at a 1s idle cadence it measures bytes accumulated across the idle gap — two individually-legal ~1.5MB messages bursting after >3s of silence would batch to ~3MB in one read and kill a healthy extension. Move the cap to the residual (incomplete-line) buffer after line extraction, which is the actual single-message semantic. Burst-after-silence is exactly the traffic shape adaptive polling creates — this is not optional.
  • Final-drain ordering on close: the "one last poll" must (a) respect the pollingOutput re-entrancy guard (runtime.ts:325-326) by awaiting any in-flight poll or looping to a clean pass, (b) complete before outputHandle.close() (:400) and before sandbox.cleanup() (:408) rm-rf's the dir, and (c) run before the expectedStop check so a drained fatal message (:375) doesn't suppress or get suppressed by the generic exit fallback (:403-406) in the wrong order.
  • Self-scheduling chains must reassign the stored timer handle every tick (active.outputPoller / inputPoller) — clearing a stale handle from a previous tick would leave the live timer running and hold the process alive.

Related: #4629 (the client-side polling half, fixed in #4643); #4704 / #4705 (remaining autonomous-polling costs).

Template check

  • I DID NOT read this template and provide the requested details.
Originally created by @kolacheee on GitHub (Aug 7, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/4706 Originally assigned to: @kolacheee on GitHub. ## Summary Each running personal **server** extension costs ~**80 timer wakeups per second plus one flash write per second, continuously from boot**: the host polls the sandbox output file every 25ms and runs a 250ms double-stat watchdog; the runner polls its input file every 25ms and heartbeat-writes every 1s. Flagged inside #4629 during the mobile-battery investigation ("related, happy to split") but never fixed — on a Termux self-host with a server extension installed this is the largest remaining passive drain. ## Expected behavior Idle extensions cost near-zero wakeups; polling tightens only while traffic is flowing (startup handshake, storage bursts, stop), preserving today's latency where it matters. ## Actual behavior - Host: `personal-server-extension-runtime.ts:394` — `outputPoller` `setInterval(25ms)` doing an fstat (+read) per tick; `:289-312` — watchdog `setInterval(250ms)` doing two `stat()`s per tick. - Runner: `personal-extension-runner.mjs:226` — `inputPoller` `setInterval(25ms)`; `:228-230` — heartbeat `writeFile` every 1s (a flash write per second, forever). - Conditional: only spawns per enabled+approved extension with `serverJs` (`runtime.ts:128`, `:239-242`) — zero-extension installs pay nothing. ## Steps to reproduce 1. Install/enable one personal server extension. 2. Observe the sandbox dir: input/output stat cadence + heartbeat mtime churn; ~80 wakeups/s in the two processes at full idle. ## Environment - Marinara Engine version: staging d6275fa0b - Install type: any with server extensions; battery-critical on Termux - OS + version: any ## Logs, screenshots, or video Code trace, line references against staging d6275fa0b. ## Additional context ### Regression-safe fix (designed + red-teamed) Keep the file transport untouched (it is the sandbox security design) — change only the **polling discipline**: 1. **Adaptive cadence, both sides.** Replace the fixed intervals with self-scheduling unref'd `setTimeout` chains: host output poll 25ms while HOT / 1000ms while idle; runner input poll 25ms HOT / 250ms idle. HOT := startup not settled, or bytes moved / a message was sent within the last ~3s. Startup handshake and storage bursts keep today's 25ms feel; steady-state drops from 40 stats/s to ~1/s per side. Latency contract verified: no storage/tool deadline exists to trip (`runner.mjs:39-45`, `runtime.ts:201-227` — no timeouts either direction; host→runner messages are exactly start/stop/storage-result), so the worst-case ≤1s first-message-after-silence delay is safe; stop responsiveness is bounded by the runner's 250ms idle cadence (rescale `CLEANUP_TIMEOUT_MS` 3000→3500 for margin). 2. **Heartbeat 1s → 5s** with the watchdog stale-threshold rescaled proportionally (same missed-beat multiple), cutting flash writes 5×. Watchdog cadence 250ms → 1s. 3. `fs.watch` only as an **optional fast-path** behind a runtime probe (inotify works on Termux home paths but is unreliable on FUSE/shared mounts) — never as the correctness mechanism; the poll chain remains the fallback. **Amendments from the adversarial pass (must be in the implementation):** - **`MAX_PROTOCOL_BYTES` false-kill:** `pollOutput` reads all available bytes and checks `outputBuffer.byteLength > 2MB` **before** line-splitting (`runtime.ts:336-344`). At 25ms that approximates a per-message cap; at a 1s idle cadence it measures *bytes accumulated across the idle gap* — two individually-legal ~1.5MB messages bursting after >3s of silence would batch to ~3MB in one read and kill a healthy extension. Move the cap to the **residual (incomplete-line) buffer after line extraction**, which is the actual single-message semantic. Burst-after-silence is exactly the traffic shape adaptive polling creates — this is not optional. - **Final-drain ordering on close:** the "one last poll" must (a) respect the `pollingOutput` re-entrancy guard (`runtime.ts:325-326`) by awaiting any in-flight poll or looping to a clean pass, (b) complete **before** `outputHandle.close()` (`:400`) and before `sandbox.cleanup()` (`:408`) rm-rf's the dir, and (c) run **before** the `expectedStop` check so a drained `fatal` message (`:375`) doesn't suppress or get suppressed by the generic exit fallback (`:403-406`) in the wrong order. - **Self-scheduling chains must reassign the stored timer handle every tick** (`active.outputPoller` / `inputPoller`) — clearing a stale handle from a previous tick would leave the live timer running and hold the process alive. Related: #4629 (the client-side polling half, fixed in #4643); #4704 / #4705 (remaining autonomous-polling costs). ## Template check - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-21 05:50:38 +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#2580
No description provided.