Skip to content

fix: replace finished() with writable lifecycle tracking#5001

Merged
mcollina merged 1 commit into
nodejs:mainfrom
trivikr:stream-finished
May 11, 2026
Merged

fix: replace finished() with writable lifecycle tracking#5001
mcollina merged 1 commit into
nodejs:mainfrom
trivikr:stream-finished

Conversation

@trivikr

@trivikr trivikr commented Apr 9, 2026

Copy link
Copy Markdown
Member

This relates to...

  • Hot-path listener overhead in lib/api/api-stream.js
  • The existing TODO about avoiding stream.finished() in StreamHandler

Rationale

StreamHandler used stream.finished(res, { readable: false }) to observe completion of the user-provided writable. That helper is broader than this path needs and registers extra listeners, which adds avoidable overhead on a hot path.

This change narrows lifecycle tracking to the writable side only while preserving the behavior stream() depends on for completion, writable errors, and premature close handling.

Changes

  • Replaced stream.finished() usage in StreamHandler with a small writable lifecycle helper that listens only for finish, error, and close
  • Preserved premature close behavior by reporting ERR_STREAM_PREMATURE_CLOSE when the writable closes before finishing
  • Preserved existing error propagation by aborting the request with the writable error
  • Avoided re-destroying the writable with the same error after an 'error' event, which keeps existing error handling semantics intact
  • Added regression coverage for premature writable close without an explicit error
  • Added regression coverage for a factory returning an already-finished writable

Features

N/A

Bug Fixes

  • Reduced listener registration overhead in stream() response handling
  • Kept stream() callback and abort behavior consistent for writable finish, error, and premature close edge cases

Breaking Changes and Deprecations

  • None

Status

Assisted-by: openai:gpt-5.4

@codecov-commenter

codecov-commenter commented Apr 9, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.19608% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.27%. Comparing base (296b424) to head (0bdfcbb).

Files with missing lines Patch % Lines
lib/api/api-stream.js 90.19% 5 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #5001   +/-   ##
=======================================
  Coverage   93.27%   93.27%           
=======================================
  Files         110      110           
  Lines       36384    36430   +46     
=======================================
+ Hits        33938    33981   +43     
- Misses       2446     2449    +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Signed-off-by: Kamat, Trivikram <16024985+trivikr@users.noreply.github.com>
@trivikr trivikr force-pushed the stream-finished branch from ec6c073 to 0bdfcbb Compare May 11, 2026 00:30
@trivikr trivikr marked this pull request as ready for review May 11, 2026 00:30

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollina mcollina merged commit af45b4a into nodejs:main May 11, 2026
60 of 61 checks passed
@github-actions github-actions Bot mentioned this pull request May 14, 2026
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.

3 participants