Make sure SSHClient Disconnect does not hang if a cancellationToken is passed

This commit is contained in:
nicolas.dorier 2019-10-21 18:43:53 +09:00
parent 4d68b12080
commit 12264d8e74
No known key found for this signature in database
GPG Key ID: 6618763EF09186FE
2 changed files with 6 additions and 3 deletions

View File

@ -102,7 +102,7 @@ namespace BTCPayServer
}; };
} }
public static Task DisconnectAsync(this SshClient sshClient) public static async Task DisconnectAsync(this SshClient sshClient, CancellationToken cancellationToken = default)
{ {
if (sshClient == null) if (sshClient == null)
throw new ArgumentNullException(nameof(sshClient)); throw new ArgumentNullException(nameof(sshClient));
@ -121,7 +121,10 @@ namespace BTCPayServer
} }
}) })
{ IsBackground = true }.Start(); { IsBackground = true }.Start();
return tcs.Task; using (cancellationToken.Register(() => tcs.TrySetCanceled()))
{
await tcs.Task;
}
} }
} }
} }

View File

@ -45,7 +45,7 @@ namespace BTCPayServer.HostedServices
{ {
using (var connection = await _options.SSHSettings.ConnectAsync(_cancellationTokenSource.Token)) using (var connection = await _options.SSHSettings.ConnectAsync(_cancellationTokenSource.Token))
{ {
await connection.DisconnectAsync(); await connection.DisconnectAsync(_cancellationTokenSource.Token);
Logs.Configuration.LogInformation($"SSH connection succeeded"); Logs.Configuration.LogInformation($"SSH connection succeeded");
canUseSSH = true; canUseSSH = true;
} }