🐛 BUG: Miniflare dev registry watcher crashes the process with uncaught `EMFILE: too many open files, watch` on macOS

Open
#17,216 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
69/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Domain
backend, devtools

Research direction

Start with packages/miniflare/src/shared/dev-registry.ts around DevRegistry#watch (the issue links lines 115–137) and check how its Chokidar watcher handles errors and readiness. Reproduce with the linked minimal reproduction on macOS using pnpm run repro; compare with pnpm run repro:baseline. Done means watcher failure no longer crashes Miniflare and the expected warning or polling fallback is verified.

Written by the indexing model from the issue text.

Description

[!NOTE]
🤖 Claude Opus 5.5 responding on behalf of ryoppippi

What versions & operating system are you using?
  • miniflare 4.20260730.0 (latest), as used by wrangler 4.167.0 / @cloudflare/vite-plugin 1.70.1
  • chokidar 4.0.1 (bundled into miniflare)
  • Node 24.22.3, pnpm 10.24.0
  • macOS 27.2.1 (arm64)
Please provide a link to a minimal reproduction

https://github.com/ryoppippi-bug-repro/miniflare-dev-registry-watch-emfile

Describe the Bug
What happens

On macOS, the FSEvents stream table is limited to roughly 1024 streams per machine. When it is full, Node's fs.watch on a directory fails with EMFILE. Miniflare's dev registry watcher (DevRegistry#watch, which watches unsafeDevRegistryPath whenever a worker binds to a service outside the instance) creates a chokidar watcher and listens only for "all". Chokidar re-emits the EMFILE as an "error" event, nothing listens for it, and Node throws it as an uncaught exception, killing the process (in a real setup, the Vite dev server running @cloudflare/vite-plugin).

Error: EMFILE: too many open files, watch
    at FSWatcher._handle.onchange (node:internal/fs/watchers:269:21)
Emitted 'error' event on FSWatcher instance at:
    at FSWatcher._handleError (.../[email protected]/node_modules/miniflare/dist/src/index.js:101804:12)
    at NodeFsHandler._boundHandleError (.../miniflare/dist/src/index.js:101032:47)
    at .../miniflare/dist/src/index.js:100925:5
    at foreach (.../miniflare/dist/src/index.js:100875:5)
    at fsWatchBroadcast (.../miniflare/dist/src/index.js:100924:3)
    at FSWatcher.<anonymous> (.../miniflare/dist/src/index.js:100965:9)
    at FSWatcher.emit (node:events:514:28)
    at FSWatcher._handle.onchange (node:internal/fs/watchers:275:12) {
  errno: -24,
  syscall: 'watch',
  code: 'EMFILE',
  filename: null
}
Expected

The registry watcher failing should not take the dev server down. Miniflare should handle the watcher's "error" event: log a warning that cross-process service bindings will not pick up registry changes (or fall back to polling, which the registry already uses on Windows and which is cheap for this small directory), and keep running. This is the same class of bug as #13890, fixed for the Wrangler assets watcher in #14027; the dev registry watcher still has no "error" listener.

Actual

Miniflare crashes with the uncaught EMFILE above as soon as DevRegistry#watch attaches its watcher. With the same exhausted FSEvents table but no external service binding (so no registry watch), Miniflare starts, serves a request and disposes cleanly, so the crash comes from the registry watcher alone.

Analysis (hypothesis)

packages/miniflare/src/shared/dev-registry.ts L115-L137 at 41bacf7 (comments trimmed):

const watcher = watch(this.registryPath, {
	usePolling: process.platform === "win32",
	interval: 100,
	ignored: () => !liveness.live,
}).on("all", () => {
	this.refresh();
});

Chokidar 4's FSWatcher#_handleError emits "error" for any code other than ENOENT/ENOTDIR (and EPERM/EACCES with ignorePermissionErrors). An EventEmitter with no "error" listener throws. Adding .on("error", ...) that logs through this.log (and, for EMFILE/ENOSPC, closes the native watcher and switches to usePolling: true) should fix it. I read the published [email protected] bundle and main at 41bacf7; I have not patched and rebuilt Miniflare to confirm the fix. watcher.once("ready", ...) in the same function may also need checking, since watch() awaits it after the error.

Reproduction
pnpm install --frozen-lockfile
pnpm run repro:baseline   # Miniflare ready, fetch: 200 ok, disposed cleanly
pnpm run repro            # uncaught EMFILE from the dev registry watcher

pnpm run repro sets EXHAUST=1, which first starts exhaust-fsevents.js in a separate process. That script opens 1100 FSEvents streams with the fsevents package (one stream per watch() call), filling the machine-wide table; it is killed when the repro exits. Run on macOS only. While it runs, other processes on the machine that start a new FSEvents watch may also fail.

const mf = new Miniflare({
	unsafeDevRegistryPath: path.join(scratch, "registry"),
	workers: [
		{
			name: "a",
			modules: true,
			compatibilityDate: "2026-08-01",
			script: "export default { fetch: () => new Response('ok') }",
			serviceBindings: { B: { name: "b" } },
		},
	],
});
await mf.ready;
How it was found

A multi-worker local dev stack (Vite + @cloudflare/vite-plugin 1.70.1 with Wrangler 4.167.0 / Miniflare 4.20260730.0, several workers talking through service bindings and the dev registry) died twice on start on 2026-11-27 on a machine running many file watchers, which had filled the FSEvents table. Vite and the Cloudflare plugin are not needed to reproduce it: plain Miniflare with unsafeDevRegistryPath and one external service binding is enough. The silent FSEvents failure on the fsevents side is filed separately as https://github.com/fsevents/fsevents/issues/414.

Dominant language
TypeScript
Stars
4.7k
Forks
1.7k
Avg merge
1d 22h
Merged PRs (30d)
282

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from cloudflare/workers-sdk

All issues in cloudflare/workers-sdk

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.