File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -126,6 +126,12 @@ const startJob = (
126126 } , 10000 ) ;
127127
128128 try {
129+ const closePromise = once ( closeEvent , 'close' ) . then ( ( close ) => {
130+ logger . debug ( 'shutting down' ) ;
131+ shutdown = true ;
132+ safeSend ( { case : 'exiting' , value : { reason : close [ 1 ] } } ) ;
133+ } ) ;
134+
129135 // Run the job function within the AsyncLocalStorage context
130136 await runWithJobContextAsync ( ctx , async ( ) => {
131137 const { tracer, traceTypes } = await import ( '../telemetry/index.js' ) ;
@@ -138,15 +144,15 @@ const startJob = (
138144 } ,
139145 { name : 'job_entrypoint' } ,
140146 ) ;
141- } ) . finally ( ( ) => {
142- clearTimeout ( unconnectedTimeout ) ;
143- } ) ;
144-
145- await once ( closeEvent , 'close' ) . then ( ( close ) => {
146- logger . debug ( 'shutting down' ) ;
147- shutdown = true ;
148- safeSend ( { case : 'exiting' , value : { reason : close [ 1 ] } } ) ;
149- } ) ;
147+ } )
148+ . then ( async ( ) => {
149+ if ( ! shutdown ) {
150+ await closePromise ;
151+ }
152+ } )
153+ . finally ( async ( ) => {
154+ clearTimeout ( unconnectedTimeout ) ;
155+ } ) ;
150156 } catch ( error ) {
151157 logger . error ( { error } , 'error in entry function' ) ;
152158 shutdown = true ;
You can’t perform that action at this time.
0 commit comments