@@ -141,19 +141,23 @@ function getContextLinesFromFile(path: string, ranges: ReadlineRange[], output:
141141 const lineReaded = createInterface ( {
142142 input : stream ,
143143 } ) ;
144+
145+ function destroyStreamAndResolve ( ) : void {
146+ stream . destroy ( ) ;
147+ resolve ( ) ;
148+ }
144149
145150 // Init at zero and increment at the start of the loop because lines are 1 indexed.
146151 let lineNumber = 0 ;
147152 let currentRangeIndex = 0 ;
148153 const range = ranges [ currentRangeIndex ] ;
149154 if ( range === undefined ) {
150155 // We should never reach this point, but if we do, we should resolve the promise to prevent it from hanging.
151- resolve ( ) ;
156+ destroyStreamAndResolve ( ) ;
152157 return ;
153158 }
154159 let rangeStart = range [ 0 ] ;
155160 let rangeEnd = range [ 1 ] ;
156-
157161 // We use this inside Promise.all, so we need to resolve the promise even if there is an error
158162 // to prevent Promise.all from short circuiting the rest.
159163 function onStreamError ( e : Error ) : void {
@@ -162,14 +166,14 @@ function getContextLinesFromFile(path: string, ranges: ReadlineRange[], output:
162166 DEBUG_BUILD && logger . error ( `Failed to read file: ${ path } . Error: ${ e } ` ) ;
163167 lineReaded . close ( ) ;
164168 lineReaded . removeAllListeners ( ) ;
165- resolve ( ) ;
169+ destroyStreamAndResolve ( ) ;
166170 }
167171
168172 // We need to handle the error event to prevent the process from crashing in < Node 16
169173 // https://github.com/nodejs/node/pull/31603
170174 stream . on ( 'error' , onStreamError ) ;
171175 lineReaded . on ( 'error' , onStreamError ) ;
172- lineReaded . on ( 'close' , resolve ) ;
176+ lineReaded . on ( 'close' , destroyStreamAndResolve ) ;
173177
174178 lineReaded . on ( 'line' , line => {
175179 lineNumber ++ ;
0 commit comments