Skip to content

Fix premature 'drain' after reopen() - #255

Merged
mcollina merged 1 commit into
masterfrom
fix-reopen-premature-drain
Aug 29, 2026
Merged

mcollina merged 1 commit into
masterfrom
fix-reopen-premature-drain

Conversation

@mcollina

Copy link
Copy Markdown
Member

If a 'ready' listener starts a write after reopen(), the reopen path still emitted 'drain' from a nextTick before that write completed, so a listener reading the file on 'drain' could observe it empty. This is the race behind the 'reopen with file' test occasionally reading '' under load, and the same one just fixed in Node.js core's Utf8Stream port (see nodejs/node commit 2e8a4b1a8ce for the test-side analysis).

Skip the extra emit when a write is in flight: release() emits the real 'drain' once the write completes, so no event is lost.

The added regression test defers the reopened file's fs.write by one setImmediate, which deterministically lands the write after the nextTick on which the premature 'drain' used to fire — it fails without the index.js change and passes with it. Full suite passes (117/117).

If a 'ready' listener starts a write after reopen(), the reopen path
still emitted 'drain' from a nextTick before that write completed, so
listeners could observe the file before the data landed. Skip that
emit when a write is in flight: release() emits the real 'drain' once
the write completes.
@mcollina
mcollina merged commit 14dbcf0 into master Aug 29, 2026
22 checks passed
@mcollina
mcollina deleted the fix-reopen-premature-drain branch August 29, 2026 11:26
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.

2 participants