Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When cancellation token is already cancelled before ProcessAsynchronously(), processing still happens. #468

Open
angusyky opened this issue Aug 15, 2023 · 1 comment

Comments

@angusyky
Copy link

angusyky commented 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 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):

  1. Inside the CancellableThrough method, check if token is cancelled and throw exception if true.
  2. Inside the delegate, do a null check for CancelEvent and throw exception if true.
  3. Somehow move token delegate registration to some point after OnCancelEvent is added to CancelEvent.
@angusyky 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
@rosenbjerg
Copy link
Owner

Sounds like some interesting suggestions. The first two sound straight-forward.
A PR will be welcomed :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants