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

Canceling ConnectAsync lead to crash #1530

Open
MicrovisionCF opened this issue Nov 8, 2024 · 0 comments
Open

Canceling ConnectAsync lead to crash #1530

MicrovisionCF opened this issue Nov 8, 2024 · 0 comments

Comments

@MicrovisionCF
Copy link

MicrovisionCF commented Nov 8, 2024

Hello, I'm trying to establish connection with ConnectAsync and I display a window to allow the user to cancel the attempt.
I experiment a System.InvalidOperationException: 'An attempt was made to transition a task to a final state when it had already completed.' each time I cancel.

Here is my simplified code to reproduce with systematic cancelation. Am I missing something ? I'm not familiar with async/await usage so maybe I miss something important...

Thank you in advance.

public bool ConnectAndCancel()
{
	CancellationTokenSource source = new CancellationTokenSource();
	CancellationToken token = source.Token;

	Task task = _sftpClient.ConnectAsync(token);

	source.Cancel();

	try
	{
		task.Wait(token);
	}
	catch (OperationCanceledException)
	{
		Console.WriteLine("Canceled");
	}

	return _sftpClient.IsConnected;
}
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

1 participant