[GH-ISSUE #4707] [Issue]: .env watcher stat-polls every 2s forever — highest-frequency permanent idle timer on the server #2581

Closed
opened 2026-10-21 05:50:39 +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/4707

Originally assigned to: @kolacheee on GitHub.

Summary

The .env hot-reload watcher stat-polls the env file every 2 seconds for the server's lifetime (fs.watchFile is stat-polling, not inotify). It is the highest-frequency permanent timer on an idle server — individually cheap, but 30 wakeups/minute forever prevents deep idle on a phone-hosted (Termux) install.

Follow-up from the mobile-battery investigation (passive-server round).

Expected behavior

Env changes are still picked up promptly (the no-restart security-config path stays intact), with near-zero idle wakeups.

Actual behavior

packages/server/src/config/env-watcher.ts:159 — watchFile(envPath, { interval: 2_000, persistent: false }, handler); started unconditionally at boot (packages/server/src/index.ts:41). One stat + timer wake every 2s, forever.

Steps to reproduce

  1. Start the server; observe the 2s stat cadence on .env for the process lifetime.

Environment

  • Marinara Engine version: staging d6275fa0b
  • Install type: any; battery-relevant on Termux
  • OS + version: any

Logs, screenshots, or video

Code trace, line references against staging d6275fa0b.

Additional context

Regression-safe fix (designed + red-teamed)

  1. fs.watch the parent directory of the env path, filtered to the .env basename — directory-watching survives atomic-replace editors (rename to a new inode) and the file not existing yet, both of which break naive fs.watch on the file itself. Keep the existing mtime/size dedupe (env-watcher.ts:138-148) and add a ~250ms event debounce (inotify fires multiple events per save).
  2. On fs.watch error/unsupported filesystem (some FUSE/network mounts), fall back to the existing watchFile handler at interval: 30_000 instead of 2_000 — env edits are human-timescale; 30s worst-case latency cuts wakeups 15× where inotify is unavailable. (inotify works for Termux home-directory paths, where the checkout and .env live.)
  3. Optionally: a documented MARINARA_ENV_WATCH=0 opt-out, checked once at startup — but do not default it off in the Termux launcher; that would silently kill the documented no-restart security-config reload path (env-watcher.ts:4-7) for exactly the users who rely on it.

Filed alongside #4704 / #4705 / #4706 from the same follow-up audit.

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/4707 Originally assigned to: @kolacheee on GitHub. ## Summary The `.env` hot-reload watcher stat-polls the env file **every 2 seconds for the server's lifetime** (`fs.watchFile` is stat-polling, not inotify). It is the highest-frequency permanent timer on an idle server — individually cheap, but 30 wakeups/minute forever prevents deep idle on a phone-hosted (Termux) install. Follow-up from the mobile-battery investigation (passive-server round). ## Expected behavior Env changes are still picked up promptly (the no-restart security-config path stays intact), with near-zero idle wakeups. ## Actual behavior `packages/server/src/config/env-watcher.ts:159` — `watchFile(envPath, { interval: 2_000, persistent: false }, handler)`; started unconditionally at boot (`packages/server/src/index.ts:41`). One stat + timer wake every 2s, forever. ## Steps to reproduce 1. Start the server; observe the 2s stat cadence on `.env` for the process lifetime. ## Environment - Marinara Engine version: staging d6275fa0b - Install type: any; battery-relevant on Termux - OS + version: any ## Logs, screenshots, or video Code trace, line references against staging d6275fa0b. ## Additional context ### Regression-safe fix (designed + red-teamed) 1. `fs.watch` the **parent directory** of the env path, filtered to the `.env` basename — directory-watching survives atomic-replace editors (rename to a new inode) and the file not existing yet, both of which break naive `fs.watch` on the file itself. Keep the existing mtime/size dedupe (`env-watcher.ts:138-148`) and add a ~250ms event debounce (inotify fires multiple events per save). 2. On `fs.watch` error/unsupported filesystem (some FUSE/network mounts), fall back to the existing `watchFile` handler at `interval: 30_000` instead of 2_000 — env edits are human-timescale; 30s worst-case latency cuts wakeups 15× where inotify is unavailable. (inotify works for Termux home-directory paths, where the checkout and `.env` live.) 3. Optionally: a documented `MARINARA_ENV_WATCH=0` opt-out, checked once at startup — but do **not** default it off in the Termux launcher; that would silently kill the documented no-restart security-config reload path (`env-watcher.ts:4-7`) for exactly the users who rely on it. Filed alongside #4704 / #4705 / #4706 from the same follow-up audit. ## Template check - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-21 05:50:39 +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#2581
No description provided.