Fix premature 'drain' after reopen() - #255
Merged
Merged
Conversation
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.
jsumners
approved these changes
Aug 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
If a
'ready'listener starts a write afterreopen(), the reopen path still emitted'drain'from anextTickbefore 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'sUtf8Streamport (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.writeby onesetImmediate, which deterministically lands the write after thenextTickon which the premature'drain'used to fire — it fails without theindex.jschange and passes with it. Full suite passes (117/117).