@@ -305,46 +305,47 @@ OutgoingMessage.prototype.cork = function cork() {
305305OutgoingMessage . prototype . uncork = function uncork ( ) {
306306 this [ kCorked ] -- ;
307307 const socket = this [ kSocket ] ;
308- if ( this [ kCorked ] || this [ kChunkedBuffer ] . length === 0 ) {
309- socket ?. uncork ( ) ;
310- return ;
311- }
308+ try {
309+ if ( this [ kCorked ] || this [ kChunkedBuffer ] . length === 0 ) {
310+ return ;
311+ }
312312
313- const len = this [ kChunkedLength ] ;
314- const buf = this [ kChunkedBuffer ] ;
315- if ( this . destroyed || socket ?. destroyed ) {
316- const error = this [ kErrored ] || socket ?. _writableState ?. errored || new ERR_STREAM_DESTROYED ( 'write' ) ;
317- for ( let n = 2 ; n < buf . length ; n += 3 ) {
318- if ( buf [ n ] !== nop ) {
319- process . nextTick ( buf [ n ] , error ) ;
313+ const len = this [ kChunkedLength ] ;
314+ const buf = this [ kChunkedBuffer ] ;
315+ if ( this . destroyed || socket ?. destroyed ) {
316+ const error = this [ kErrored ] || socket ?. _writableState ?. errored || new ERR_STREAM_DESTROYED ( 'write' ) ;
317+ for ( let n = 2 ; n < buf . length ; n += 3 ) {
318+ if ( buf [ n ] !== nop ) {
319+ process . nextTick ( buf [ n ] , error ) ;
320+ }
321+ }
322+ buf . length = 0 ;
323+ this [ kChunkedLength ] = 0 ;
324+ return ;
325+ }
326+ assert ( this . chunkedEncoding ) ;
327+
328+ let callbacks ;
329+ this . _send ( len . toString ( 16 ) , 'latin1' , null ) ;
330+ this . _send ( crlf_buf , null , null ) ;
331+ for ( let n = 0 ; n < buf . length ; n += 3 ) {
332+ this . _send ( buf [ n + 0 ] , buf [ n + 1 ] , null ) ;
333+ if ( buf [ n + 2 ] ) {
334+ callbacks ??= [ ] ;
335+ callbacks . push ( buf [ n + 2 ] ) ;
320336 }
321337 }
322- buf . length = 0 ;
338+ this . _send ( crlf_buf , null , callbacks . length ? ( err ) => {
339+ for ( const callback of callbacks ) {
340+ callback ( err ) ;
341+ }
342+ } : null ) ;
343+
344+ this [ kChunkedBuffer ] . length = 0 ;
323345 this [ kChunkedLength ] = 0 ;
346+ } finally {
324347 socket ?. uncork ( ) ;
325- return ;
326348 }
327- assert ( this . chunkedEncoding ) ;
328-
329- let callbacks ;
330- this . _send ( len . toString ( 16 ) , 'latin1' , null ) ;
331- this . _send ( crlf_buf , null , null ) ;
332- for ( let n = 0 ; n < buf . length ; n += 3 ) {
333- this . _send ( buf [ n + 0 ] , buf [ n + 1 ] , null ) ;
334- if ( buf [ n + 2 ] ) {
335- callbacks ??= [ ] ;
336- callbacks . push ( buf [ n + 2 ] ) ;
337- }
338- }
339- this . _send ( crlf_buf , null , callbacks . length ? ( err ) => {
340- for ( const callback of callbacks ) {
341- callback ( err ) ;
342- }
343- } : null ) ;
344-
345- this [ kChunkedBuffer ] . length = 0 ;
346- this [ kChunkedLength ] = 0 ;
347- socket ?. uncork ( ) ;
348349
349350 // If we had a pending drain and flushed all data, emit the drain event.
350351 if ( this [ kNeedDrain ] && this . writableLength === 0 ) {
0 commit comments