Skip to content

stream: add diagnostics_channel event for completion#42822

Open
bengl wants to merge 3 commits intonodejs:mainfrom
bengl:bengl/stream-done-dc
Open

stream: add diagnostics_channel event for completion#42822
bengl wants to merge 3 commits intonodejs:mainfrom
bengl:bengl/stream-done-dc

Conversation

@bengl
Copy link
Copy Markdown
Member

@bengl bengl commented Apr 22, 2022

This adds a stream.web.done diagnostics channel to Web Streams. with the intention of having a straightforward way to detect when a ReadableStream has been read to completion, without triggering the reading of the stream.

This is useful for instrumentation tools trying to measure the length of a request/response cycle triggered by a fetch().

Why draft?

Right now I'm only testing directly calling read() and async iterators. pipeThrough(), pipeTo(), and cancellation all need to be tested (and perhaps implemented).

Also, the naming is certainly open for suggestions. I based the name of the channel on the only other prior art in Node.js core.

Finally, I had a weird thing happen with mksnapshot, so I'm wondering if anyone knows how to better work around that. See the inline comment below.

All that being said, I thought it was worth opening it now anyway, to make sure the approach is sound and agreeable.

@nodejs-github-bot nodejs-github-bot added the needs-ci PRs that need a full CI run. label Apr 22, 2022
let streamDoneChannel;
function getStreamDoneChannel () {
return streamDoneChannel ||= dc.channel('stream.web.done');
}
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Instead of this lazy-loading, I had tried to just create the channel immediately. However, this resulted in the following error at build time:

$ make -j24
ninja -C out/Release 
ninja: Entering directory `out/Release'
[21/25] ACTION node: node_mksnapshot_9b7a2d2290b02e76d66661df74749f56
FAILED: gen/node_snapshot.cc 
cd ../../; out/Release/node_mksnapshot out/Release/gen/node_snapshot.cc
global handle not serialized: 0x2c1d47302cd9: [JS_API_OBJECT_TYPE] in OldSpace
 - map: 0x08188aa5fd61 <Map(HOLEY_ELEMENTS)> [FastProperties]
 - prototype: 0x12df1ab07b79 <Object map = 0x8188aa5fd19>
 - elements: 0x20e18df01329 <FixedArray[0]> [HOLEY_ELEMENTS]
 - embedder fields: 1
 - properties: 0x20e18df01329 <FixedArray[0]>
 - All own properties (excluding elements): {}
 - embedder fields = {
    21965, aligned pointer: 0x55cde06a73c0
 }

global handle not serialized: 0x2c1d47302b49: [JS_OBJECT_TYPE] in OldSpace
 - map: 0x0f4b222452b9 <Map(HOLEY_ELEMENTS)> [FastProperties]
 - prototype: 0x2c1d473027a9 <Channel map = 0xf4b22245229>
 - elements: 0x20e18df01329 <FixedArray[0]> [HOLEY_ELEMENTS]
 - properties: 0x20e18df01329 <FixedArray[0]>
 - All own properties (excluding elements): {
    0x218677ef8c49: [String] in OldSpace: #_subscribers: 0x20e18df015b9 <undefined> (const data field 0), location: in-object
    0x20e18df058f1: [String] in ReadOnlySpace: #name: 0x218677ede9d1 <String[15]: #stream.web.done> (const data field 1), location: in-object
 }



#
# Fatal error in , line 0
# Check failed: handle_checker.CheckGlobalAndEternalHandles().
#
#
#
#FailureMessage Object: 0x7ffefc883920
 1: 0x55cddae43c35  [out/Release/node_mksnapshot]
 2: 0x55cddbcc2926 V8_Fatal(char const*, ...) [out/Release/node_mksnapshot]
 3: 0x55cddb396b05 v8::SnapshotCreator::CreateBlob(v8::SnapshotCreator::FunctionCodeHandling) [out/Release/node_mksnapshot]
 4: 0x55cddae6f680 node::SnapshotBuilder::Generate(node::SnapshotData*, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >) [out/Release/node_mksnapshot]
 5: 0x55cddae707f4 node::SnapshotBuilder::Generate(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >) [out/Release/node_mksnapshot]
 6: 0x55cddad822f2 BuildSnapshot(int, char**) [out/Release/node_mksnapshot]
 7: 0x7f4802c9dfd0  [/lib/x86_64-linux-gnu/libc.so.6]
 8: 0x7f4802c9e07d __libc_start_main [/lib/x86_64-linux-gnu/libc.so.6]
 9: 0x55cdda8c9e05 _start [out/Release/node_mksnapshot]
Trace/breakpoint trap (core dumped)
ninja: build stopped: subcommand failed.
make: *** [Makefile:127: node] Error 1

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.

@bengl bengl added web streams diagnostics_channel Issues and PRs related to diagnostics channel labels Apr 22, 2022
@jasnell
Copy link
Copy Markdown
Member

jasnell commented Apr 22, 2022

So long as there are no observable differences to the actual streams API, this is great. I would be concerned about possible performance impact but this code already has performance issues and this is not likely to be the primary bottleneck.

@bengl bengl force-pushed the bengl/stream-done-dc branch from 9e62322 to f1d80b1 Compare April 29, 2026 20:35
@bengl bengl marked this pull request as ready for review April 29, 2026 20:36
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 29, 2026

Codecov Report

❌ Patch coverage is 85.71429% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.64%. Comparing base (5d578c5) to head (f1d80b1).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
lib/internal/webstreams/readablestream.js 85.71% 5 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #42822      +/-   ##
==========================================
- Coverage   89.65%   89.64%   -0.02%     
==========================================
  Files         708      708              
  Lines      220402   220441      +39     
  Branches    42269    42289      +20     
==========================================
+ Hits       197597   197604       +7     
- Misses      14671    14673       +2     
- Partials     8134     8164      +30     
Files with missing lines Coverage Δ
lib/internal/webstreams/readablestream.js 98.39% <85.71%> (-0.16%) ⬇️

... and 47 files with indirect coverage changes

🚀 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.

@bengl bengl requested a review from jasnell April 30, 2026 19:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

diagnostics_channel Issues and PRs related to diagnostics channel needs-ci PRs that need a full CI run. web streams

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants