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

Cancellation of FtpConnection.ReadFromStreamAsync causes UnobservedTaskException #131

Open
cjzzz opened this issue Jun 8, 2022 · 1 comment · May be fixed by #141
Open

Cancellation of FtpConnection.ReadFromStreamAsync causes UnobservedTaskException #131

cjzzz opened this issue Jun 8, 2022 · 1 comment · May be fixed by #141

Comments

@cjzzz
Copy link

cjzzz commented Jun 8, 2022

In FtpConnection.ReadFromStreamAsync, readTask is started using the cancellation token passed into the method, but tcs is also created which has its result set when the cancellation token is cancelled:

protected override async Task<int> ReadFromStreamAsync(byte[] buffer, int offset, int length, CancellationToken cancellationToken)
{
	var readTask = Stream
	   .ReadAsync(buffer, offset, length, cancellationToken);

	var tcs = new TaskCompletionSource<object?>();
	using var registration = cancellationToken.Register(() => tcs.TrySetResult(null));
	var resultTask = await Task.WhenAny(readTask, tcs.Task)
	   .ConfigureAwait(false);

	if (cancellationToken.IsCancellationRequested)
	{
		Logger?.LogTrace("Cancelled through CancellationToken");
		return 0;
	}
...

This can cause resultTask to complete when tcs.Task is complete, and the exception from the cancellation of readTask is never observed. causing an UnobservedTaskException.

@fubar-coder
Copy link
Contributor

Nice catch!

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