Skip to content

fix(server): serialize chokidar watcher batches to prevent heap growth - #1300

Open
Schrotti77 wants to merge 1 commit into
inkeep:mainfrom
Schrotti77:fix/chokidar-batch-serialization
Open

fix(server): serialize chokidar watcher batches to prevent heap growth#1300
Schrotti77 wants to merge 1 commit into
inkeep:mainfrom
Schrotti77:fix/chokidar-batch-serialization

Conversation

@Schrotti77

Copy link
Copy Markdown

Problem

On Windows the OK server uses the chokidar watcher fallback (@parcel/watcher crashes with STATUS_STACK_BUFFER_OVERRUN on long/non-ASCII paths, see #1207). chokidar (ReadDirectoryChangesW) emits more granular events than inotify, and each 50ms batch calls handleRawEvents, which readFile()s every changed file.

When a batch is still mid-drain (slow disk, many files, active sync/backup tool) and the next 50ms timer fires, the new batch launches independently — overlapping async work piles up and the Node heap grows without bound on event storms.

Fix

Serialize batches by chaining them on an inFlight promise in startChokidarWatcher. Batches drain strictly one at a time; a batch that lands while another is draining waits its turn. The .catch remains load-bearing: it keeps the chain alive after an error so later batches don't deadlock.

Regression test

file-watcher-chokidar-fallback.test.ts — new describe "chokidar backend — batch serialization":

  • onDiskEvent counts active drains (active++, hold 200ms, active--), tracks maxActive
  • write to existing file → wait until active === 1 (batch 1 draining)
  • write again → batch 2 lands mid-drain
  • wait 600ms, expect(maxActive).toBe(1)

Verified:

  • Without fix: test fails (maxActive reaches 2 — batches overlap)
  • With fix: 11/11 tests pass, typecheck clean

Tested with forceBackend: 'chokidar' on Linux — no Windows needed to exercise the path.

chokidar (Windows ReadDirectoryChangesW) emits more granular events than
inotify, so a 50ms batch can still be mid-drain (readFile-ing every change)
when the next timer fires. Launching batches independently piles up
overlapping async work → unbounded heap growth on event storms.

Chain batches on an inFlight promise so they drain strictly one at a time.
The .catch stays load-bearing: it keeps the chain alive after an error.

Regression test: maxActive must stay 1 when a second batch lands mid-drain.
Fails without the fix (maxActive=2), passes with it.
@Schrotti77
Schrotti77 deployed to inkeep-oss-sync August 20, 2026 09:09 — with GitHub Actions Active
@github-actions

Copy link
Copy Markdown

Thanks for the contribution!

What happens next:

  • A maintainer will review your PR.
  • If you don't hear back within a few business days, please comment here to nudge our team.
  • This repository is maintained through an internal mirror. When your change is accepted, this PR will close automatically. Don't be alarmed when it closes — that's how it merges, and your authorship is preserved.

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


Hermes (Marian's AI Agent) seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@robert-inkeep robert-inkeep self-assigned this Aug 22, 2026
@robert-inkeep

Copy link
Copy Markdown
Contributor

hi @Schrotti77 could you follow the @CLAassistant instructions? and i'll see this through

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants