@@ -142,14 +142,11 @@ public async Task ExecuteAsync(CancellationToken cancellationToken)
142142 {
143143 var mutexReleasedAfterSeconds = _concurrencyConfiguration . Value . MutexTimeoutSeconds ;
144144 var timeoutCancellationTokenSource = new CancellationTokenSource ( ) ;
145+ Logger . LogInformation ( "Timeout after {Timeout}" , mutexReleasedAfterSeconds ) ;
146+
145147 timeoutCancellationTokenSource . CancelAfter ( mutexReleasedAfterSeconds * 1000 ) ;
146- timeoutCancellationTokenSource . Token . Register ( ( ) => {
147- Logger . LogError ( "Scrape job {JobName} was cancelled due to timeout. However, dangling async tasks " +
148- "may be running for an unbounded amount of time. In the rare case where " +
149- "many such timeouts occur, consider restarting the Scraper Agent." , Name ) ;
150- } ) ;
151148
152- Logger . LogWarning ( "Init timeout token" ) ;
149+ Logger . LogInformation ( "Init timeout token" ) ;
153150 // to enforce timeout in addition to cancellationToken passed down by .NET
154151 var composedCancellationTokenSource = CancellationTokenSource . CreateLinkedTokenSource ( cancellationToken , timeoutCancellationTokenSource . Token ) ;
155152 var scrapeDefinitions = await GetAllScrapeDefinitions ( composedCancellationTokenSource . Token ) ;
@@ -377,7 +374,14 @@ private async Task WorkWrapper(Func<Task> work, CancellationToken cancellationTo
377374 cancellationToken . Register ( ( ) => {
378375 tcs . TrySetResult ( null ) ;
379376 } ) ;
380- await Task . WhenAny ( work ( ) , tcs . Task ) ;
377+ var completedTask = await Task . WhenAny ( work ( ) , tcs . Task ) ;
378+ if ( completedTask == tcs . Task )
379+ {
380+ Logger . LogError ( "Scrape job {JobName} was cancelled due to timeout. However, dangling async tasks " +
381+ "may be running for an unbounded amount of time. In the rare case where " +
382+ "many such timeouts occur, consider restarting the Scraper Agent or tuning the mutex timeout configuration." , Name ) ;
383+ throw new OperationCanceledException ( cancellationToken ) ;
384+ }
381385 }
382386 finally
383387 {
0 commit comments