@@ -458,6 +458,11 @@ function onwriteError(stream, state, er, cb) {
458
458
-- state . pendingcb ;
459
459
460
460
cb ( er ) ;
461
+ // Ensure callbacks are invoked even when autoDestroy is
462
+ // not enabled. Passing `er` here doesn't make sense since
463
+ // it's related to one specific write, not to the buffered
464
+ // writes.
465
+ errorBuffer ( state , new ERR_STREAM_DESTROYED ( 'write' ) ) ;
461
466
// This can emit error, but error must always follow cb.
462
467
errorOrDestroy ( stream , er ) ;
463
468
}
@@ -531,9 +536,29 @@ function afterWrite(stream, state, count, cb) {
531
536
cb ( ) ;
532
537
}
533
538
539
+ if ( state . destroyed ) {
540
+ errorBuffer ( state , new ERR_STREAM_DESTROYED ( 'write' ) ) ;
541
+ }
542
+
534
543
finishMaybe ( stream , state ) ;
535
544
}
536
545
546
+ // If there's something in the buffer waiting, then invoke callbacks.
547
+ function errorBuffer ( state , err ) {
548
+ if ( state . writing || ! state . bufferedRequest ) {
549
+ return ;
550
+ }
551
+
552
+ for ( let entry = state . bufferedRequest ; entry ; entry = entry . next ) {
553
+ const len = state . objectMode ? 1 : entry . chunk . length ;
554
+ state . length -= len ;
555
+ entry . callback ( err ) ;
556
+ }
557
+ state . bufferedRequest = null ;
558
+ state . lastBufferedRequest = null ;
559
+ state . bufferedRequestCount = 0 ;
560
+ }
561
+
537
562
// If there's something in the buffer waiting, then process it
538
563
function clearBuffer ( stream , state ) {
539
564
state . bufferProcessing = true ;
@@ -820,12 +845,7 @@ const destroy = destroyImpl.destroy;
820
845
Writable . prototype . destroy = function ( err , cb ) {
821
846
const state = this . _writableState ;
822
847
if ( ! state . destroyed ) {
823
- for ( let entry = state . bufferedRequest ; entry ; entry = entry . next ) {
824
- process . nextTick ( entry . callback , new ERR_STREAM_DESTROYED ( 'write' ) ) ;
825
- }
826
- state . bufferedRequest = null ;
827
- state . lastBufferedRequest = null ;
828
- state . bufferedRequestCount = 0 ;
848
+ process . nextTick ( errorBuffer , state , new ERR_STREAM_DESTROYED ( 'write' ) ) ;
829
849
}
830
850
destroy . call ( this , err , cb ) ;
831
851
return this ;
0 commit comments