mirror of
https://github.com/nodejs/node.git
synced 2026-11-25 23:01:06 -05:00
stream: fix ERR_INVALID_STATE when cancelling Readable.toWeb()
When a web ReadableStream returned by Readable.toWeb() is cancelled while the underlying Readable is actively producing data, a pending onData callback can still fire after the controller has been closed and attempt to enqueue a chunk, throwing ERR_INVALID_STATE. Check wasCanceled in the onData handler and return early to avoid calling controller.enqueue() on a closed controller. Refs: https://github.com/nodejs/node/issues/54205 Signed-off-by: Richard Scarrott <riscarrott@googlemail.com> PR-URL: https://github.com/nodejs/node/pull/62773 Reviewed-By: Xuguang Mei <meixuguang@gmail.com> Reviewed-By: Filip Skokan <panva.ip@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
1 parent
70117640f7
commit
f8737b8ee5
2 files changed
+23
No files matched your search
@@ -612,6 +612,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);
|
||||
|
||||
@@ -42,3 +42,25 @@ const { setTimeout: delay } = require('timers/promises');
|
||||
assert.deepStrictEqual(closeResult, { value: undefined, done: true });
|
||||
})().then(common.mustCall());
|
||||
}
|
||||
|
||||
// Cancelling a web ReadableStream while the underlying Readable is actively
|
||||
// producing data should not throw ERR_INVALID_STATE. The 'data' handler in
|
||||
// newReadableStreamFromStreamReadable must check wasCanceled before calling
|
||||
// controller.enqueue(). See: https://github.com/nodejs/node/issues/54205
|
||||
{
|
||||
const readable = new Readable({
|
||||
read() {
|
||||
this.push(Buffer.alloc(1024));
|
||||
},
|
||||
});
|
||||
|
||||
const webStream = Readable.toWeb(readable);
|
||||
const reader = webStream.getReader();
|
||||
|
||||
(async () => {
|
||||
await reader.read();
|
||||
await reader.read();
|
||||
reader.releaseLock();
|
||||
await webStream.cancel();
|
||||
})().then(common.mustCall());
|
||||
}
|
||||
Reference in new issue
Block a user