@@ -1532,10 +1532,12 @@ function readableStreamPipeTo(
15321532 }
15331533
15341534 async function step() {
1535- if (shuttingDown)
1536- return true;
1535+ if (shuttingDown) return true;
15371536
1538- await writer[kState].ready.promise;
1537+ if (dest[kState].backpressure) {
1538+ await writer[kState].ready.promise;
1539+ if (shuttingDown) return true;
1540+ }
15391541
15401542 const controller = source[kState].controller;
15411543
@@ -1549,7 +1551,6 @@ function readableStreamPipeTo(
15491551 while (controller[kState].queue.length > 0) {
15501552 if (shuttingDown) return true;
15511553
1552- source[kState].disturbed = true;
15531554 const chunk = dequeueValue(controller);
15541555
15551556 if (controller[kState].closeRequested && !controller[kState].queue.length) {
@@ -1563,20 +1564,17 @@ function readableStreamPipeTo(
15631564 setPromiseHandled(state.currentWrite);
15641565
15651566 // Check backpressure after each write
1566- if (dest[kState].state === 'writable' ) {
1567- const desiredSize = writer.desiredSize;
1568- if (desiredSize !== null && desiredSize <= 0) {
1569- // Backpressure - stop batch and wait for ready
1570- break;
1571- }
1567+ if (dest[kState].backpressure ) {
1568+ // Backpressure - stop batch and wait for ready
1569+ break;
1570+ } else if (dest[kState].state !== 'writable' || writableStreamCloseQueuedOrInFlight(dest)) {
1571+ // Closing or erroring - stop batch and wait for shutdown
1572+ break;
15721573 }
15731574 }
15741575
15751576 // Trigger pull if needed after batch
1576- if (source[kState].state === 'readable' &&
1577- !controller[kState].closeRequested) {
1578- readableStreamDefaultControllerCallPullIfNeeded(controller);
1579- }
1577+ readableStreamDefaultControllerCallPullIfNeeded(controller);
15801578
15811579 // Check if stream closed during batch
15821580 if (source[kState].state === 'closed') {
0 commit comments