sshnet / SSH.NET

SSH.NET is a Secure Shell (SSH) library for .NET, optimized for parallelism.
http://sshnet.github.io/SSH.NET/
MIT License
4.01k stars 933 forks source link

SshCommand.Execute deadlock #1506

Open valeriob opened 1 month ago

valeriob commented 1 month ago

Hi, i'm trying latest release 2024.1.0 and i think i found an issue with the api, there is a deadlock in the Renci.SshNet.SshCommand.Execute()

image

is it due the sync over async antipattern ? image

Probably makes sense to do a fully async api to avoid the issue ?

Rob-Hague commented 1 month ago

Hmm, could be. Are you running in a GUI / ASP.NET / non-console environment?

Can you reproduce the problem consistently?

If yes, does making this tweak to the library make a difference?

diff --git a/src/Renci.SshNet/SshCommand.cs b/src/Renci.SshNet/SshCommand.cs
index 6bfd5742..96188e4c 100644
--- a/src/Renci.SshNet/SshCommand.cs
+++ b/src/Renci.SshNet/SshCommand.cs
@@ -279,7 +279,7 @@ public Task ExecuteAsync(CancellationToken cancellationToken = default)
             _cts = null;
             _cancellationRequested = false;

-            _tcs = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously);
+            _tcs = new TaskCompletionSource<object>(TaskCreationOptions.None);
             _userToken = cancellationToken;

             _channel = _session.CreateChannelSession();
valeriob commented 1 month ago

Hello @Rob-Hague i'm running on a dotnet 7 console program, it blocks after 1 hour, but id depends on the load i guess. At the moment i got back to version 2023.0.1 and it's up for one day without issues.

valeriob commented 1 month ago

Probably the issue is something else, i got a lock also with the older version : image i think something gets stuck when the connection is unstable, we are connecting to devices that move around connected via GSM.

Probably more similar to : https://github.com/sshnet/SSH.NET/issues/462

Rob-Hague commented 1 month ago

In that case I'm not sure if there is something actionable here. I think my prior comment about TaskCreationOptions was nonsensical because there are no continuations attached to the Task when using the sync Execute

valeriob commented 1 month ago

At the moment i had to put the ssh command execution on a dedicated thread and add a watchdog to kill if not completed after some timeout.