Skip to content

fix(gateway): fall back to polling when config watcher exhausts inotify retries - #92852

Merged
vincentkoc merged 3 commits into
openclaw:mainfrom
danbao:fix/config-watcher-polling-fallback
Jun 14, 2026
Merged

vincentkoc merged 3 commits into
openclaw:mainfrom
danbao:fix/config-watcher-polling-fallback

Conversation

@danbao

@danbao danbao commented Jun 14, 2026 •

Copy link
Copy Markdown
Contributor

Summary

When fs.inotify.max_user_watches is exhausted (ENOSPC), the config hot-reload watcher permanently disables itself after 3 failed re-create attempts. This PR adds a polling-mode degradation step so config hot-reload survives on hosts where inotify resources are constrained.

Fixes #92851.

Problem

startGatewayConfigReloader creates a chokidar watcher with native inotify watching. When the kernel limit is hit, all 3 retry attempts fail identically (same native mode), and hot-reload is permanently disabled for the session. This is common on hosts running multiple Node processes (tsx watch, vite dev servers, codex agents, etc.) where the default fs.inotify.max_user_watches is insufficient.

Fix

After all native retries are exhausted, reset the retry counter and recreate the watcher with usePolling: true. Only if polling also fails does hot-reload become permanently disabled.

Real behavior proof

Behavior or issue addressed: Config hot-reload watcher permanently disables itself when fs.inotify.max_user_watches is exhausted (ENOSPC) after 3 failed native re-create attempts, with no fallback to polling mode.

Real environment tested: Debian 13 (Linux 6.12.63+deb13-amd64), Node v24.13.0, OpenClaw 2026.6.6 production gateway, fs.inotify.max_user_watches=61023, 53 inotify FDs consumed across systemd, warp remote-server, OpenAI codex, tsx/vite dev servers.

Exact steps or command run after this patch:

  1. Built a standalone Node.js script replicating the exact handleWatcherError logic from config-reload.ts (with the fix applied), using the real [email protected] library on the host where ENOSPC was occurring.
  2. Created a config file at /tmp/proof-config.json.
  3. Created a chokidar watcher in native (inotify) mode.
  4. Emitted 4 simulated ENOSPC errors via watcher.emit('error', new Error('ENOSPC: ...')) to exhaust the 3-retry budget and trigger the polling fallback.
  5. Wrote a new version of the config file: writeFileSync('/tmp/proof-config.json', JSON.stringify({ v: 2 })).
  6. Waited for the polling watcher to detect the change.

Before evidence (optional): Production gateway logs showing permanent disable without the fix:

2026-06-14T10:14:14+08:00 warn  gateway/reload  config watcher error; re-creating watcher (attempt 1/3 in 500ms): Error: ENOSPC: System limit for number of file watchers reached
2026-06-14T10:14:16+08:00 warn  gateway/reload  config watcher error; re-creating watcher (attempt 2/3 in 2000ms): Error: ENOSPC: System limit for number of file watchers reached
2026-06-14T10:14:21+08:00 warn  gateway/reload  config watcher error; re-creating watcher (attempt 3/3 in 5000ms): Error: ENOSPC: System limit for number of file watchers reached
2026-06-14T10:14:23+08:00 error gateway/reload  config hot-reload disabled: watcher failed after 3 re-create attempts: Error: ENOSPC: System limit for number of file watchers reached

Evidence after fix: Console output from the verification script on the same host:

[03:25:18.223] Watcher created (usePolling=false)
[03:25:18.242] config watcher error; re-creating watcher (attempt 1/3 in 100ms): Error: ENOSPC: System limit for number of file watchers reached
[03:25:18.343] Watcher created (usePolling=false)
[03:25:18.344] config watcher error; re-creating watcher (attempt 2/3 in 300ms): Error: ENOSPC: System limit for number of file watchers reached
[03:25:18.648] Watcher created (usePolling=false)
[03:25:18.649] config watcher error; re-creating watcher (attempt 3/3 in 800ms): Error: ENOSPC: System limit for number of file watchers reached
[03:25:19.450] Watcher created (usePolling=false)
[03:25:19.450] config watcher native retries exhausted; degrading to polling mode: Error: ENOSPC: System limit for number of file watchers reached
[03:25:19.551] Watcher created (usePolling=true)
[03:25:20.183] After 4 native errors: degradedToPolling=true, watcher alive=true
[03:25:20.783] Writing config change...
[03:25:20.990] CHANGE detected on config file (usePolling=true)
VERDICT: PASS

Observed result after fix: After 3 native retries exhausted, the watcher degraded to polling mode (usePolling=true) instead of permanently disabling. The polling watcher detected the real config file write within ~200ms. Hot-reload status remained "active" throughout. Zero inotify resources consumed in polling mode.

What was not tested: Full integration test inside a live OpenClaw gateway process (would require rebuilding the dist bundle and restarting the production gateway mid-session). The updated unit test in config-reload.test.ts covers the two-phase lifecycle with mocked chokidar.

Tradeoffs

  • Polling latency: ~200ms (controlled by awaitWriteFinish.pollInterval) vs instant inotify events. Negligible for config files that change infrequently.
  • CPU: One stat() call per polling interval on a single file. Effectively zero.
  • No inotify usage: Polling uses zero inotify watches, completely sidestepping the resource exhaustion.

Testing

  • Updated config-reload.test.ts to cover the two-phase degradation: native retries → polling fallback → polling retries → disabled.
  • 2242 other gateway-core tests pass unchanged.

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime size: XS triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. labels Jun 14, 2026
@clawsweeper

clawsweeper Bot commented Jun 14, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed June 13, 2026, 11:34 PM ET / 03:34 UTC.

Summary
The PR modifies the gateway config reloader to switch chokidar to polling after native watcher recreate retries are exhausted and updates the watcher recovery test for the two-phase failure path.

PR surface: Source +17, Tests +17. Total +34 across 2 files.

Reproducibility: Do we have a high-confidence way to reproduce the issue? Yes from source: current main and v2026.6.6 retry native chokidar watchers with usePolling false outside Vitest, then set hotReloadStatus to disabled after the retry budget; the reporter also supplied production logs, but I did not run a live ENOSPC gateway.

Review metrics: 1 noteworthy metric.

  • Watcher fallback mode: 1 fallback added. The gateway config watcher now changes operational mode after native retry exhaustion, which maintainers should notice even though no new config surface is added.

Merge readiness
Overall: 🐚 platinum hermit
Proof: 🐚 platinum hermit
Patch quality: 🐚 platinum hermit
Result: ready for maintainer review.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Rank-up moves:

  • none.

Risk before merge

  • [P1] The after-fix proof used real chokidar and a real file write in a standalone script plus unit coverage, but it did not restart and exercise a full patched OpenClaw gateway process under live inotify exhaustion.

Maintainer options:

  1. Decide the mitigation before merge
    Merge the narrow gateway fallback after maintainer review and normal gates, preserving bounded native retries and disabling hot-reload only if polling-mode recovery also fails.
  2. Pause or close
    Do not merge this PR until maintainers decide whether the risk is worth taking.

Next step before merge

  • No automated repair is needed; the remaining action is normal maintainer review and merge handling for the existing clean PR.

Security
Cleared: The diff only changes gateway config watcher recovery logic and its unit test, with no dependency, workflow, package, secret, permission, or supply-chain surface changes.

Review details

Best possible solution:

Merge the narrow gateway fallback after maintainer review and normal gates, preserving bounded native retries and disabling hot-reload only if polling-mode recovery also fails.

Do we have a high-confidence way to reproduce the issue?

Do we have a high-confidence way to reproduce the issue? Yes from source: current main and v2026.6.6 retry native chokidar watchers with usePolling false outside Vitest, then set hotReloadStatus to disabled after the retry budget; the reporter also supplied production logs, but I did not run a live ENOSPC gateway.

Is this the best way to solve the issue?

Is this the best way to solve the issue? Yes; a bounded switch to chokidar polling after native watcher retry exhaustion is narrower than adding a config knob and avoids pushing operators to global CHOKIDAR_USEPOLLING or sysctl workarounds.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 7f49f875de1b.

Label changes

Label justifications:

  • P2: The PR fixes a normal-priority gateway hot-reload resilience bug with limited blast radius and a narrow affected surface.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (live_output): The PR body includes after-fix terminal/live output from a real Debian host using chokidar 5.0.0 and a real file write to show polling fallback detection after simulated ENOSPC exhaustion.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR body includes after-fix terminal/live output from a real Debian host using chokidar 5.0.0 and a real file write to show polling fallback detection after simulated ENOSPC exhaustion.
Evidence reviewed

PR surface:

Source +17, Tests +17. Total +34 across 2 files.

View PR surface stats
Area Files Added Removed Net
Source 1 21 4 +17
Tests 1 27 10 +17
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 48 14 +34

What I checked:

  • Current main failure path: Current main creates the config watcher with polling only under Vitest and disables hot-reload after the watcher recreate retry budget is exhausted, so repeated native ENOSPC errors still end the session's config hot-reload. (src/gateway/config-reload.ts:415, 7f49f875de1b)
  • Shipped release still has the failure path: The latest release tag v2026.6.6 contains the same usePolling and retry-disable behavior, so the linked bug is not already fixed in the shipped release. (src/gateway/config-reload.ts:415, 8c802aa68351)
  • PR head fallback implementation: At PR head, handleWatcherError flips a one-way degradedToPolling flag after native retries, resets the retry counter, creates the next watcher with usePolling enabled, and only disables after polling-mode retries also fail. (src/gateway/config-reload.ts:439, 4996b65b7403)
  • PR regression coverage: The updated watcher recovery test now exercises native retries, polling fallback, polling retries, and final disabled status after polling also exhausts its retry budget. (src/gateway/config-reload.test.ts:1646, 4996b65b7403)
  • Dependency contract checked: The pinned chokidar 5.0.0 package documents usePolling as fs.watchFile-backed polling, and its handler implementation branches from fs.watch to watchFile when opts.usePolling is true. (package.json:1891, 7f49f875de1b)
  • Relevant CI and proof checks: Live PR status shows the head is mergeable/clean, the latest Real behavior proof check passed, and relevant CI jobs including gateway-core, lint, prod-types, and test-types passed for the PR head. (4996b65b7403)

Likely related people:

  • hansraj316: Merged pull request history shows this user authored the watcher retry/disabled-status recovery that this PR extends from native retries to polling fallback. (role: introduced current recovery behavior; confidence: high; commits: 8ff77c8168e4; files: src/gateway/config-reload.ts, src/gateway/config-reload.test.ts)
  • vincentkoc: The merged watcher recovery PR includes a verification comment from this user describing lifecycle fixes and focused gateway config-reload test proof before merge. (role: recent verifier; confidence: medium; commits: 8ff77c8168e4; files: src/gateway/config-reload.ts, src/gateway/config-reload.test.ts)
  • steipete: GitHub path history shows repeated recent work across gateway config reload planning, docs, and adjacent reload behavior, including plugin hot-reload and reload helper maintenance. (role: recent area contributor; confidence: medium; commits: d678bcfcc7d0, c8ac4c8aeada, f59113cfd301; files: src/gateway/config-reload.ts, src/gateway/config-reload.test.ts, src/gateway/server-reload-handlers.ts)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

How this review workflow works
  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

@openclaw-barnacle openclaw-barnacle Bot added size: S proof: supplied External PR includes structured after-fix real behavior proof. and removed size: XS triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. labels Jun 14, 2026
@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. P2 Normal backlog priority with limited blast radius. labels Jun 14, 2026
@vincentkoc vincentkoc self-assigned this Jun 14, 2026
@vincentkoc
vincentkoc force-pushed the fix/config-watcher-polling-fallback branch from 4996b65 to e56b8b7 Compare June 14, 2026 11:24
danbao and others added 3 commits June 14, 2026 19:24
…fy retries

When fs.inotify.max_user_watches is exhausted (ENOSPC), chokidar's native
watcher fails permanently after 3 re-create attempts, disabling config
hot-reload for the rest of the session. On hosts running many Node processes
this is the common failure mode rather than a transient error.

Add a polling-mode degradation step: after all native retries are exhausted,
reset the retry counter and recreate the watcher with usePolling=true. Only
if polling also fails does hot-reload become permanently disabled.
The existing test expected 'disabled' after 3 native retries. Now the
watcher degrades to polling mode first, so the test covers the full
two-phase lifecycle: 3 native retries → polling fallback → 3 polling
retries → disabled.
@vincentkoc
vincentkoc force-pushed the fix/config-watcher-polling-fallback branch from e56b8b7 to c9762c5 Compare June 14, 2026 11:24
@openclaw-barnacle openclaw-barnacle Bot removed the proof: sufficient ClawSweeper judged the real behavior proof convincing. label Jun 14, 2026
@vincentkoc

Copy link
Copy Markdown
Member

Maintainer closeout for current head c9762c5159a2ef75aa23d2af2a5435d0d8bf6b63:

  • Reviewed the gateway config watcher path and Chokidar 5.0.0 option contract. The fix now tracks the effective watcher mode, respects CHOKIDAR_USEPOLLING, degrades from native watching to polling only when polling can actually be selected, and reports native/polling disable mode accurately.
  • Tightened tests to assert actual usePolling transitions plus forced CHOKIDAR_USEPOLLING=1 and CHOKIDAR_USEPOLLING=0 behavior.
  • Local focused proof: node scripts/run-vitest.mjs src/gateway/config-reload.test.ts --maxWorkers=1 — 344 tests passed.
  • Auto review: .agents/skills/autoreview/scripts/autoreview --mode branch --base origin/main — clean, no accepted/actionable findings.
  • Testbox-through-Crabbox changed gate: tbx_01kv2xvbqkv4dmvvvsswzm75hz / Actions run https://github.com/openclaw/openclaw/actions/runs/27497233897 — OPENCLAW_CHECK_CHANGED_REMOTE_CHILD=1 OPENCLAW_CHANGED_LANES_RAW_SYNC=1 corepack pnpm check:changed, exit 0.
  • GitHub PR checks on c9762c5159a2ef75aa23d2af2a5435d0d8bf6b63: 134 pass, 27 skipped; non-blocking noise is cancelled auto-response and neutral CodeQL summary. Merge state is CLEAN.

Landing as a narrow fix for #92851.

@vincentkoc
vincentkoc merged commit c78f937 into openclaw:main Jun 14, 2026
162 of 163 checks passed
@vincentkoc

Copy link
Copy Markdown
Member

post-merge maintainer follow-up landed on main: 40093f5a93

The PR merged while the final autoreview repair was still local. The follow-up now records Chokidar's effective normalized watcher.options.usePolling value, so platform-forced polling and environment overrides use the correct recovery mode instead of the requested input.

Verification:

  • autoreview: clean (patch is correct, confidence 0.98)
  • node scripts/run-vitest.mjs src/gateway/config-reload.test.ts (348 passed)
  • Testbox changed gate unavailable in this checkout: pnpm, corepack, and blacksmith are absent

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gateway Gateway runtime P2 Normal backlog priority with limited blast radius. proof: supplied External PR includes structured after-fix real behavior proof. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: M status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Config hot-reload permanently disabled when inotify watches exhausted (no polling fallback)

2 participants