@@ -129,28 +129,32 @@ async function startNgrokTunnel(config: {
129129 stdio : [ "ignore" , "pipe" , "pipe" ] ,
130130 } ) ;
131131
132- let resolved = false ;
133- let closed = false ;
132+ // Startup settlement and OS process closure are separate: the deadline can
133+ // win before the child has been reaped.
134+ let startupSettled = false ;
135+ let childClosed = false ;
134136 let publicUrl : string | null = null ;
135137 let outputBuffer = "" ;
136138 // Keep only enough UTF-16-safe suffix to recognize an error marker split
137139 // at the next stream chunk boundary; otherwise the caller loses the code.
138140 let stderrTail = "" ;
139141
140142 const timeout = setTimeout ( ( ) => {
141- if ( ! resolved ) {
142- resolved = true ;
143- void terminateNgrokProcess ( proc , ( ) => closed ) . then ( ( ) => {
143+ if ( ! startupSettled ) {
144+ startupSettled = true ;
145+ void terminateNgrokProcess ( proc , ( ) => childClosed ) . then ( ( ) => {
144146 reject ( new Error ( "ngrok startup timed out (30s)" ) ) ;
145147 } ) ;
146148 }
147149 } , 30000 ) ;
150+ // Do not keep the host process alive solely waiting on ngrok startup.
151+ timeout . unref ( ) ;
148152
149153 const rejectIfPending = ( message : string , kill = false ) => {
150- if ( ! resolved ) {
151- resolved = true ;
154+ if ( ! startupSettled ) {
155+ startupSettled = true ;
152156 clearTimeout ( timeout ) ;
153- if ( kill && ! closed ) {
157+ if ( kill && ! childClosed ) {
154158 proc . kill ( "SIGKILL" ) ;
155159 }
156160 reject ( new Error ( message ) ) ;
@@ -172,8 +176,8 @@ async function startNgrokTunnel(config: {
172176 }
173177
174178 // Check for ready state
175- if ( publicUrl && ! resolved ) {
176- resolved = true ;
179+ if ( publicUrl && ! startupSettled ) {
180+ startupSettled = true ;
177181 clearTimeout ( timeout ) ;
178182
179183 // Add path to the public URL
@@ -185,7 +189,7 @@ async function startNgrokTunnel(config: {
185189 publicUrl : fullUrl ,
186190 provider : "ngrok" ,
187191 stop : async ( ) => {
188- await terminateNgrokProcess ( proc , ( ) => closed ) ;
192+ await terminateNgrokProcess ( proc , ( ) => childClosed ) ;
189193 } ,
190194 } ) ;
191195 }
@@ -234,9 +238,9 @@ async function startNgrokTunnel(config: {
234238 } ) ;
235239
236240 proc . on ( "close" , ( code ) => {
237- closed = true ;
238- if ( ! resolved ) {
239- resolved = true ;
241+ childClosed = true ;
242+ if ( ! startupSettled ) {
243+ startupSettled = true ;
240244 clearTimeout ( timeout ) ;
241245 reject ( new Error ( `ngrok exited unexpectedly with code ${ code } ` ) ) ;
242246 }
0 commit comments