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

CancelAsync does not cancel the command started by BeginExecute and blocks execution #1147

Closed
togarha opened this issue Jun 26, 2023 · 0 comments · Fixed by #1345
Closed

Comments

@togarha
Copy link

togarha commented Jun 26, 2023

When calling CancelAsync for a process started by BeginExecute, the process is not canceled and the result of the command cannot be retrieved.

With enough time for executing the command and without CancelAsync everything works fine:

            using (SshClient client = new SshClient(host, username, password))
            {
                client.Connect();
                using (SshCommand command = client.CreateCommand("sleep 10; echo run"))
                {
                    IAsyncResult task = command.BeginExecute();
                    Thread.Sleep(15000);
                    //command.CancelAsync();
                    var result = command.EndExecute(task);
                    System.Console.WriteLine(result);
                }
                client.Disconnect();
            }

After 15s, the following correctly appears in the console and the execution finishes:

run

If I reduce the sleep to 5s, and uncomment the CancelAsync:

            using (SshClient client = new SshClient(host, username, password))
            {
                client.Connect();
                using (SshCommand command = client.CreateCommand("sleep 10; echo run"))
                {
                    IAsyncResult task = command.BeginExecute();
                    Thread.Sleep(5000);
                    command.CancelAsync();
                    var result = command.EndExecute(task);
                    System.Console.WriteLine(result);
                }
                client.Disconnect();
            }

The process gets blocked at EndExecute instruction, it doesn't finish even though the command hasn't been canceled and it finishes after 10s from the beginning.

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

Successfully merging a pull request may close this issue.

1 participant