Commit ba42a2efeee for nodejs

commit ba42a2efeee6db8f08fc2c89a98fe33d9144ec30
Author: Mattias Buelens <649348+MattiasBuelens@users.noreply.github.com>
Date:   Mon Nov 23 07:57:22 2026 +0100

    stream: fix stale byte stream queue size

    Re-read the byte controller's queue size before filling each pending
    read request. A released BYOB reader can leave a partially filled
    pull-into descriptor that is enqueued while a default reader has
    multiple pending reads. Caching the queue size causes the loop to
    dequeue from an empty queue after fulfilling the first read request.

    Add a regression test for one, two, and three pending reads and verify
    that any remaining queued bytes are preserved.

    Signed-off-by: Mattias Buelens <mattias@buelens.com>
    PR-URL: https://github.com/nodejs/node/pull/67499
    Reviewed-By: James M Snell <jasnell@gmail.com>
    Reviewed-By: Matteo Collina <matteo.collina@gmail.com>

diff --git a/lib/internal/webstreams/readablestream.js b/lib/internal/webstreams/readablestream.js
index 898ff3bce55..7e083286cf1 100644
--- a/lib/internal/webstreams/readablestream.js
+++ b/lib/internal/webstreams/readablestream.js
@@ -3913,15 +3913,15 @@ function readableByteStreamControllerFillReadRequestFromQueue(controller, readRe
 }

 function readableByteStreamControllerProcessReadRequestsUsingQueue(controller) {
-  const {
-    stream,
-    queueTotalSize,
-  } = controller[kState];
+  const state = controller[kState];
+  const { stream } = state;
   const { reader } = stream[kState];
   assert(isReadableStreamDefaultReader(reader));

   while (reader[kState].readRequests.length > 0) {
-    if (queueTotalSize === 0) {
+    // Filling a read request dequeues a chunk, so re-read the queue size
+    // on every iteration.
+    if (state.queueTotalSize === 0) {
       return;
     }
     readableByteStreamControllerFillReadRequestFromQueue(
diff --git a/test/parallel/test-webstreams-byte-enqueue-pending-read-requests.js b/test/parallel/test-webstreams-byte-enqueue-pending-read-requests.js
new file mode 100644
index 00000000000..d067d23fac1
--- /dev/null
+++ b/test/parallel/test-webstreams-byte-enqueue-pending-read-requests.js
@@ -0,0 +1,57 @@
+'use strict';
+
+// A released BYOB reader's partially filled pull-into is moved to the
+// queue on the next enqueue(). If a default reader is waiting with more
+// than one read request, the queue must only fill as many read requests
+// as it has chunks for, and the newly enqueued chunk must go to the next
+// read request.
+
+const common = require('../common');
+const assert = require('assert');
+const { ReadableStream } = require('stream/web');
+
+async function test(numReads) {
+  let controller;
+  const rs = new ReadableStream({
+    type: 'bytes',
+    start(c) {
+      controller = c;
+    },
+  });
+
+  // Leave a pull-into with 1 of the required 2 bytes filled.
+  const byobReader = rs.getReader({ mode: 'byob' });
+  const byobRead = byobReader.read(new Uint8Array(2), { min: 2 });
+  await new Promise(setImmediate);
+  controller.enqueue(new Uint8Array([1]));
+  byobReader.releaseLock();
+  await assert.rejects(byobRead, { name: 'TypeError' });
+
+  const reader = rs.getReader();
+  const reads = [];
+  for (let i = 0; i < numReads; i++) {
+    reads.push(reader.read());
+  }
+
+  controller.enqueue(new Uint8Array([2]));
+  controller.enqueue(new Uint8Array([3]));
+  controller.close();
+
+  const results = await Promise.all(reads);
+  const expected = [[1], [2], [3]].slice(0, numReads);
+  assert.deepStrictEqual(results.map(({ value }) => [...value]), expected);
+
+  // Chunks that were not read yet are still in the queue.
+  const rest = [];
+  for (let result = await reader.read(); !result.done;
+    result = await reader.read()) {
+    rest.push(...result.value);
+  }
+  assert.deepStrictEqual(rest, [1, 2, 3].slice(numReads));
+}
+
+(async () => {
+  for (const numReads of [1, 2, 3]) {
+    await test(numReads);
+  }
+})().then(common.mustCall());