Skip to content
Open
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
1 change: 1 addition & 0 deletions lib/internal/webstreams/adapters.js
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ function newReadableStreamFromStreamReadable(streamReadable, options = kEmptyObj
streamReadable.pause();

streamReadable.on('data', function onData(chunk) {
if (wasCanceled) return;
// Copy the Buffer to detach it from the pool.
if (Buffer.isBuffer(chunk) && !objectMode)
chunk = new Uint8Array(chunk);
Expand Down
52 changes: 52 additions & 0 deletions test/parallel/test-webstreams-adapters-cancel-backpressure.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict';
const common = require('../common');
const { PassThrough, Readable, pipeline } = require('node:stream');
const { WritableStream } = require('node:stream/web');
const { setTimeout } = require('node:timers/promises');

// Regression test for https://github.com/nodejs/node/issues/64529
// Readable.toWeb() uncaughtException when the stream is canceled during backpressure resume

process.on('uncaughtException', (err) => {
console.error('Uncaught exception!', err);
process.exit(1);
});

async function run() {
for (let i = 0; i < 50; i++) {
const src = new Readable({
read() {
this.push(Buffer.alloc(16 * 1024, 1));
if ((this.bytes = (this.bytes || 0) + 16384) > 512 * 1024) this.push(null);
},
});
const pt = new PassThrough({ highWaterMark: 16384 });
pipeline(src, pt, () => {});
const web = Readable.toWeb(pt);

const ac = new AbortController();
const writer = new WritableStream(
{
async write() {
await setTimeout(1);
}
},
{ highWaterMark: 1 },
);

// Disconnect randomly to catch the exact tick window
setTimeout(i % 10).then(() => ac.abort()).then(common.mustCall());

try {
await web.pipeTo(writer, { signal: ac.signal });
} catch {
// Ignore abort errors
}

await new Promise((r) => setImmediate(r));
}
}

run().then(common.mustCall(() => {
process.exit(0);
}));