Skip to content

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

Closed
@togarha

Description

@togarha

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions