File tree Expand file tree Collapse file tree 1 file changed +34
-2
lines changed Expand file tree Collapse file tree 1 file changed +34
-2
lines changed Original file line number Diff line number Diff line change @@ -250,10 +250,24 @@ protected async Task<TestResults> RunWorker(Pipelines.AgentJobRequestMessage mes
250250
251251 await SetupMessage ( _l1HostContext , message ) ;
252252
253- using ( var cts = new CancellationTokenSource ( ) )
253+ var cts = new CancellationTokenSource ( ) ;
254+ try
254255 {
255256 cts . CancelAfter ( ( int ) JobTimeout . TotalMilliseconds ) ;
256- return await RunWorker ( _l1HostContext , message , cts . Token ) ;
257+ var result = await RunWorker ( _l1HostContext , message , cts . Token ) ;
258+
259+ // If job timed out, give it a moment to complete finalization
260+ if ( result . TimedOut )
261+ {
262+ await Task . Delay ( 100 ) ; // Allow 100ms for cleanup to complete
263+ }
264+
265+ return result ;
266+ }
267+ finally
268+ {
269+ // Dispose after ensuring cleanup had time to complete
270+ cts ? . Dispose ( ) ;
257271 }
258272 }
259273
@@ -333,6 +347,24 @@ await processChannel.SendAsync(
333347 }
334348 else
335349 {
350+ // Timeout occurred - give worker task a moment to complete cleanup gracefully
351+ try
352+ {
353+ // Wait up to 2 seconds for graceful shutdown after timeout
354+ using ( var gracefulShutdownCts = new CancellationTokenSource ( TimeSpan . FromSeconds ( 2 ) ) )
355+ {
356+ await workerTask . WaitAsync ( gracefulShutdownCts . Token ) ;
357+ }
358+ }
359+ catch ( OperationCanceledException )
360+ {
361+ // Worker didn't complete gracefully within 2 seconds, that's expected for timeout tests
362+ }
363+ catch ( Exception )
364+ {
365+ // Other exceptions during shutdown are also expected in timeout scenarios
366+ }
367+
336368 return new TestResults
337369 {
338370 TimedOut = true
You can’t perform that action at this time.
0 commit comments