Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: fix unhandled errors in webstream adapters #54206

Merged
merged 1 commit into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/internal/webstreams/adapters.js
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ function newReadableStreamFromStreamReadable(streamReadable, options = kEmptyObj
const strategy = evaluateStrategyOrFallback(options?.strategy);

let controller;
let wasCanceled = false;

function onData(chunk) {
// Copy the Buffer to detach it from the pool.
Expand All @@ -480,6 +481,10 @@ function newReadableStreamFromStreamReadable(streamReadable, options = kEmptyObj
streamReadable.on('error', () => {});
if (error)
return controller.error(error);
// Was already canceled
if (wasCanceled) {
return;
}
controller.close();
});

Expand All @@ -491,6 +496,7 @@ function newReadableStreamFromStreamReadable(streamReadable, options = kEmptyObj
pull() { streamReadable.resume(); },

cancel(reason) {
wasCanceled = true;
destroy(streamReadable, reason);
},
}, strategy);
Expand Down
15 changes: 15 additions & 0 deletions test/parallel/test-stream-readable-from-web-termination.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';
require('../common');
const { Readable } = require('stream');

{
const r = Readable.from(['data']);

const wrapper = Readable.fromWeb(Readable.toWeb(r));

wrapper.on('data', () => {
// Destroying wrapper while emitting data should not cause uncaught
// exceptions
wrapper.destroy();
});
}
12 changes: 12 additions & 0 deletions test/parallel/test-stream-readable-to-web-termination.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';
require('../common');
const { Readable } = require('stream');

{
const r = Readable.from([]);
// Cancelling reader while closing should not cause uncaught exceptions
r.on('close', () => reader.cancel());

const reader = Readable.toWeb(r).getReader();
reader.read();
}
Loading