stream: avoid duplicated endReadableNT scheduling - #65310
Open
mcollina wants to merge 1 commit into
Open
Conversation
Calling read() on an ended stream multiple times before the microtask queue drains scheduled one endReadableNT tick per call, as the only guard was endEmitted, which is set inside the tick itself. A hello-world HTTP server was scheduling it four times per request while dumping the unread request body. Introduce a kEndScheduled flag armed when the tick is scheduled and cleared when it runs. Clearing it unconditionally matters for reused sockets: undestroy() resets endEmitted through the state descriptors but cannot reach this flag, and a stale value would block the 'end' event after a net.Socket reconnect.
Collaborator
|
Review requested:
|
jasnell
approved these changes
Aug 15, 2026
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #65310 +/- ##
==========================================
- Coverage 90.30% 90.13% -0.17%
==========================================
Files 759 752 -7
Lines 248294 251575 +3281
Branches 46860 47267 +407
==========================================
+ Hits 224220 226757 +2537
- Misses 15516 16177 +661
- Partials 8558 8641 +83
🚀 New features to boost your workflow:
|
jasnell
approved these changes
Aug 15, 2026
ronag
approved these changes
Aug 16, 2026
Collaborator
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.
Calling read() on an ended stream multiple times before the microtask queue drains scheduled one endReadableNT tick per call, since the only guard (endEmitted) is set inside the tick itself. A hello-world HTTP server was scheduling it four times per request while dumping the unread request body.
Add a kEndScheduled flag, armed on scheduling and cleared when the tick runs. It must be cleared unconditionally: undestroy() resets endEmitted through the state descriptors but cannot reach this flag, and a stale value would block 'end' after a net.Socket reconnect.
Benchmarks vs main:
streams/finished.jsreadable +10.45% (99.9% confidence), no significant regressions across the streams suite; ~+1-3% on HTTP server benchmarks (10 → 7 nextTicks per request).