CancelAsync() Always seems to timeout

Nov 26, 2013 at 10:04 AM
Edited Nov 26, 2013 at 10:05 AM
Hi,

I'm currently trying to implement a Job Manager in C# that executes various tasks on Linux and Windows machines via SSH. The idea is to asynchronously execute the task so that the Job Manager can monitor in case the user aborts the job (The job manager is multi-threaded to allow multiple jobs to execute at once).
If the user does abort the job, this is where the problems come in. I'm using CancelAsync() to try and cancel the running process on the remote machine, but for some reason it will always time out and not abort the remote process. Any advice on what I might be doing wrong / could do differently / investigate further would be much appreciate! Sample code for the manager thread below:
client.Connect();
var cmd = client.CreateCommand("python /my/python/script.py 1 2 3");

var async = cmd.BeginExecute();

while (!async.IsComplete)
{
    Thread.Sleep(2000);
    if (job.State == jobState.Aborted)
    {
        cmd.CancelAsync();
    }
}

cmd.EndExecute(async);
...
client.Disconnect();
Many thanks!
Mike
Sep 24, 2014 at 10:55 AM
Hi Mike,

I have tried to do it "nasty" - just disconnect/dispose the command and the connection. It returned immediately - however the job was kept running forked.

If you found any way to cancel a command, I would really appreciate if you could share.

Thanks,
gyoreg
Sep 24, 2014 at 1:46 PM
Hi Gyoreg,

Unfortunately I never managed to get this working as I hoped. I had the same experience as you with the nasty approach as well. In the end we had to turn the responsibility around and have jobs occasionally ask the server if they should still be running or not. It's not ideal and not as clean or responsive as I hoped but it was the best we could manage with CancelAsync() not really working.

I haven't actually looked at this for quite some time, but I've just noticed that there has been a new release since my OP. I'm guessing you're using the newest version and still seeing the problem? I guess it's probably worth raising as an issue, but I don't know how actively this project is maintained anymore...

Sorry I can't be any more help!
Mike
Sep 24, 2014 at 3:06 PM
Edited Sep 24, 2014 at 3:09 PM
Hi,

What I found, that even in Putty/PLink I see the same approach: when I start a command it continues after cancelling the command itself (press ctrl+c in command prompt) by default.

However when I forced plink to allocate a pty, (-t) it was working fine, and commands could be cancelled easily. So I assume, that the problem comes from PTY allocation.

Here I did not found any support for this on SshCommand level. A Shell/ShellStream can be used, or we should ask for PTY reservation feature for SshCommand. However this second option sounds much better. :)

(I found here only this about the topic: discussion:541597)


Thanks a lot for your help,
gyoreg