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

Aperta
#17,216 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
69/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
javascript, node.js, typescript
Ambito
backend, devtools

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

[!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.

Lingua principale
TypeScript
Stelle
4.7k
Fork
1.7k
Merge medio
1g 22h
PR unite (30g)
282

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di cloudflare/workers-sdk

Tutte le issue di cloudflare/workers-sdk

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.