You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Disclaimer: When it comes to .NET async programming, I am closer to a beginner than anything else. Appreciate any advice I can get on this topic. For context, I am trying to add a hard timeout to the underlying ffmpeg process through a CancellationTokenSource.
Situation: Cancellation token passed via CancellableThrough is cancelled before .ProcessAsynchronously() is called.
Expected: Process will not be started.
Actual: Process is started.
Reason: CancellableThrough is currently called before ProcessSync/Async. Inside CancellableThrough, the delegate is registered like so: token.Register(() => CancelEvent?.Invoke(this, timeout));. If the token is already cancelled at this point, the delegate is run immediately and synchronously. However, since OnCancelEvent is only added to CancelEvent inside Process(), CancelEvent is null at the time of invocation. Even though OnCancelEvent is eventually added to CancelEvent, the latter will no longer be triggered since the cancellation already occurred.
Suggestions (exclusive of one another):
Inside the CancellableThrough method, check if token is cancelled and throw exception if true.
Inside the delegate, do a null check for CancelEvent and throw exception if true.
Somehow move token delegate registration to some point after OnCancelEvent is added to CancelEvent.
The text was updated successfully, but these errors were encountered:
angusyky
changed the title
When cancellation token is already cancelled before ProcessAsynchronously(), processing still proceeds.
When cancellation token is already cancelled before ProcessAsynchronously(), processing still happens.
Aug 15, 2023
Disclaimer: When it comes to .NET async programming, I am closer to a beginner than anything else. Appreciate any advice I can get on this topic. For context, I am trying to add a hard timeout to the underlying ffmpeg process through a CancellationTokenSource.
Situation: Cancellation token passed via CancellableThrough is cancelled before .ProcessAsynchronously() is called.
Expected: Process will not be started.
Actual: Process is started.
Reason: CancellableThrough is currently called before ProcessSync/Async. Inside CancellableThrough, the delegate is registered like so:
token.Register(() => CancelEvent?.Invoke(this, timeout));
. If the token is already cancelled at this point, the delegate is run immediately and synchronously. However, since OnCancelEvent is only added to CancelEvent inside Process(), CancelEvent isnull
at the time of invocation. Even though OnCancelEvent is eventually added to CancelEvent, the latter will no longer be triggered since the cancellation already occurred.Suggestions (exclusive of one another):
The text was updated successfully, but these errors were encountered: