Skip to content

System.InvalidOperationException: Renci.SshNet.Abstractions.SocketExtensions+_ConnectAsync_d__1.MoveNext #913

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

Closed
paulmaybee opened this issue Jan 20, 2022 · 10 comments · Fixed by #918
Labels

Comments

@paulmaybee
Copy link

We are seeing crashes when an async connect operation is canceled. Analyses of the crash leads me to believe that SocketAsyncEventArgsAwaitable.SetCompleted is being called twice: once during the handling of the cancel and once when the IOCompletion fires. However the code that runs the continuationAction does not handle this condition appropriately and runs the continuationAction twice, causing the InvalidOperationException. Changing this line in SetCompleted:

var continuation = continuationAction ?? Interlocked.CompareExchange(ref continuationAction, SENTINEL, null);

to:

var continuation = Interlocked.Exchange(ref continuationAction, SENTINEL);

seems to fix the problem.

@IgorMilavec
Copy link
Collaborator

Can you please provide the exception call stack?

@paulmaybee
Copy link
Author

Call stack for the failure:
mscorlib.ni!System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1[[System.Threading.Tasks.VoidTaskResult, mscorlib]].SetException
VS.Renci.SshNet!Renci.SshNet.Abstractions.SocketExtensions
mscorlib.ni!System.Threading.ExecutionContext.RunInternal
mscorlib.ni!System.Threading.ExecutionContext.Run
mscorlib.ni!System.Runtime.CompilerServices.AsyncMethodBuilderCore
VS.Renci.SshNet!Renci.SshNet.Abstractions.SocketExtensions
System.ni!System.Net.Sockets.SocketAsyncEventArgs.OnCompleted
mscorlib.ni!System.Threading.ExecutionContext.RunInternal
mscorlib.ni!System.Threading.ExecutionContext.Run
mscorlib.ni!System.Threading.ExecutionContext.Run
System.ni!System.Net.Sockets.SocketAsyncEventArgs.FinishOperationAsyncFailure
System.ni!System.Net.Sockets.SocketAsyncEventArgs.CompletionPortCallback
mscorlib.ni!System.Threading._IOCompletionCallback.PerformIOCompletionCallback

@IgorMilavec
Copy link
Collaborator

Hm, not very readable... I'll try to do a repro...

@paulmaybee
Copy link
Author

paulmaybee commented Feb 10, 2022

A little more detail might help to reproduce it. The failure would occur after canceling a dialog box that was waiting for a connection to complete. The connection was being made to an address that was not responding so normally would have timed out after 15 seconds.

The InvalidOperationException had text to the effect that an attempt was made to Complete a task that was already marked as Completed.

@IgorMilavec
Copy link
Collaborator

I am unable to reproduce this error; I tried a console app on net472 and netcoreapp3.1. It could be that the error is due to SynchronizationContext flow in UI application.
Can you create a simplified repro based on your code?

@paulmaybee
Copy link
Author

paulmaybee commented Feb 11, 2022

I was able to repro with the attached program.
SshCrashFWTest.zip

Start the program then hit the OK button on the dialog box when it pops up.

@IgorMilavec
Copy link
Collaborator

At first I thought the problem was caused by sync/async used in the repro, but when I added a button to the form with this handler:

        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            await AddConnectionAsync();
        }

the problem persists.
My build environment is occupied at this time so I cannot look into the root cause for this immediatelly, but I will do it ASAP.

@IgorMilavec
Copy link
Collaborator

@paulmaybee can you please test the fix if it solves the problem in your real application?

@paulmaybee
Copy link
Author

@IgorMilavec This fix seems to work. Thanks.

@IgorMilavec
Copy link
Collaborator

Great, thanks for feedback. Let me do some more tests in real life and then I'll "un-draft" the PR.

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

Successfully merging a pull request may close this issue.

2 participants