@@ -14,20 +14,20 @@ internal sealed class ProcessTerminationHandler : IDisposable
1414
1515 internal readonly TaskCompletionSource < int > ProcessTerminationCompletionSource ;
1616 private readonly CancellationTokenSource _handlerCancellationTokenSource ;
17- private readonly Task < int > _startedHandler ;
17+ private Task < int > ? _startedHandler ;
1818 private readonly TimeSpan _processTerminationTimeout ;
1919#if NET7_0_OR_GREATER
2020 private readonly IDisposable ? _sigIntRegistration , _sigTermRegistration ;
2121#endif
22-
22+
23+ internal Task < int > StartedHandler { set => Volatile . Write ( ref _startedHandler , value ) ; }
24+
2325 internal ProcessTerminationHandler (
2426 CancellationTokenSource handlerCancellationTokenSource ,
25- Task < int > startedHandler ,
2627 TimeSpan processTerminationTimeout )
2728 {
2829 ProcessTerminationCompletionSource = new ( ) ;
2930 _handlerCancellationTokenSource = handlerCancellationTokenSource ;
30- _startedHandler = startedHandler ;
3131 _processTerminationTimeout = processTerminationTimeout ;
3232
3333#if NET7_0_OR_GREATER // we prefer the new API as they allow for cancelling SIGTERM
@@ -86,8 +86,9 @@ void Cancel(int forcedTerminationExitCode)
8686
8787 try
8888 {
89+ var startedHandler = Volatile . Read ( ref _startedHandler ) ;
8990 // wait for the configured interval
90- if ( ! _startedHandler . Wait ( _processTerminationTimeout ) )
91+ if ( startedHandler is null || ! startedHandler . Wait ( _processTerminationTimeout ) )
9192 {
9293 // if the handler does not finish within configured time,
9394 // use the completion source to signal forced completion (preserving native exit code)
0 commit comments