#15681·rspack

[Bug]: Native watcher callback order can trigger an extra empty rebuild

Author: matthewdavis-oaiCreated Sep 15, 2026Updated Sep 26, 2026

Problem

The native watcher can deliver an aggregate containing a changed file before that file's callbackUndelayed when JavaScript is briefly busy. With two real stock compilers, the late notification invalidates an already running build and queues an empty retry. An isolated two-file native candidate fixes this controlled case while retaining manual invalidation and later edits. It is not released, and these results establish no application speedup.

Reproduce and compare

Source-only reproduction and raw results use Node 24.19.0, core/binding 2.2.7 and Linux x64 GNU. The gist includes the seven unchanged stock files, the candidate patch, build instructions, and raw candidate results.

bash
gh gist clone 47f56e023379f124812fb1764a7e510d native-watcher-repro
cd native-watcher-repro
npm install
node repro.mjs > result.json
PACKAGE_ANCHOR="$PWD/package.json" node multicompiler.mjs > multicompiler-result.json

Recorded runs reused an existing exact-version dependency graph; fresh installation was not tested. Direct-binding cases prove registration with a real sentinel on both streams, then perform six separated writes. Blocked cases hold JavaScript for 260ms at aggregate timeout 200ms, or 60ms at timeout zero. Controls cover idle delivery, pause inside aggregate, and explicit pause/resume.

The actual MultiCompiler fixture uses two native watchers, both children targeting Node, and explicitly passes timeout 50ms to compiler.watch. Three writes each block JavaScript for 110ms. Recorder wrappers delegate synchronously; there are no mocked compiler callbacks or Router dependencies.

Control Stock 2.2.7 Native candidate
Blocked direct binding 24/24 aggregate-before-raw 24/24 raw-before-aggregate
Idle direct binding 24/24 raw-before-aggregate Same
Explicit pause/resume 12/12 raw while paused, aggregate after resume Same
Three blocked MultiCompiler writes Two builds per child; second empty One per child; no empty retry
Manual invalidation One intentionally empty build per child Same
New same-file edit during held builds Three builds per child; final output 9 Same

All direct-binding writes received both callbacks. All watchers closed and temporary files were removed. The candidate still produced three builds per child for the held-newer-edit control; that test establishes edit preservation, not minimal scheduling for overlapping edits. Ten candidate lifecycle controls also passed, covering queued callback generations, pause/later edits, close behavior, and original Error/string/plain-object throws from both callbacks.

Cause and proposed patch

Published executor.rs dispatches aggregate and raw work through independent tasks. native_watcher.rs uses separate one-slot thread-safe functions with unchecked nonblocking status. The reproduction measures JavaScript delivery order, not native enqueue order or event loss.

native-watcher-order.patch changes only those two files against commit 8f5779591e0913a70f60ecfbfbebb962596a96e6. Raw batches and aggregate snapshots enqueue under the same files-state mutex, then pass through one executor stream and one persistent per-watcher JavaScript delivery stream. A shared thread-safe function alone would not order independent producers. Messages retain their originating callback generation. The timer clears its running flag inside the snapshot lock, preventing a newer edit from missing timer scheduling. A fixed JavaScript dispatcher preserves arbitrary thrown values.

The callback-generation stress uses an 80ms busy interval, not a deterministic barrier proving every native enqueue completed.

The nonblocking delivery queue is unbounded: sustained writes with no JavaScript drain can grow memory. Send status is checked; close adds no callback-drain guarantee. Empty invalidations and genuinely newer edits are retained. Windows/macOS/WASI and restricted script-generation environments remain untested.

Candidate identity and reproduction limits

Patch SHA-256: 48b52e6a8f8c118761c30d2a2809791ae631a92cf8d17cc98ade70863a6ca8c1. Executed candidate addon: 32adca9e2c2f5f61f8b877873ca52b2ea83d032dbc4d0f046e8775209178c2e6; stock addon: ce4761a070713b5198e71e5fd5707e253d6fe3eb88e0a1903057496b6a03d0fc. The extension contains source, raw results, exact identities, a standalone Linux build recipe and strict candidate-count checker; it excludes binaries and private application material. The recipe's fresh installation is untested. Four local executor tests passed before the production build; their separate local-only patch is excluded.

The original preservation fixture's candidate invocation stopped before compiler construction because its cleanup instrumentation loaded stock alongside the core-selected candidate. The corrected fixture changes only that selection, retaining strict single-addon/path/hash guards and all scenarios. Exact original stock source/results remain supplied; revised bytes are not claimed as the recorded stock execution. The separate checker enforces candidate counts because the shared diagnostic fixture permits stock's extra retries.

Historical context

Earlier application controls and a local 2.2.6 trace with the accounting backport motivated this investigation; they do not establish application causality or impact. Historical 2.2.6 references remain: executor, binding, MultiCompiler, Watching. The done-accounting issue is separate. These current controls isolate the native ordering mechanism; historical application attribution remains unresolved.