Quantcast
Channel: sshnet Issue Tracker Rss Feed
Viewing all articles
Browse latest Browse all 1026

Commented Unassigned: Sftpclient.Disconnect takes very long time [2738]

$
0
0
The following code takes over two minutes to execute. It gets stuck on Disconnect(). I am trying to connect to TITAN commercial FTP Server.


SftpClient client = new SftpClient(sAddress, iPort, sUserID, password);
client.Connect();
client.Disconnect();

I can connect to other SFTP sites just fine.

Any idea what would be wrong?

-Jayesh
Comments: ** Comment from web user: kroq **

Similar issue - getting up to a two minute hang on calling disconnect. Curiously, it seemed to happen to me mostly when I was moving small amounts of data (on the order to 10KB). That might have been an effect of my test scenario, however. Most of my tests were with small chunks of data.

I tried the solution by mahowling but it did not work in my case. I dug a little deeper and discovered the hang was happening in the SocketDisconnect method of Session.NET.cs. Specifically, the call to _socket.Disconnect(true) was causing the hang. That true parameter says allow the socket to be re-used.

A call to (sftpclient).Disconnect actually calls the DisconnectAndDispose function in Session.cs - so this socket is not going to be reused anyway. My solution was to create an overload method for SocketDisconnect that passed a {false} value for the reuse parameter. That seemed to cure it.

Here's what the overload method looks like:

```
/// <summary>
/// Closes the socket and optionally flags the socket for reuse
/// </summary>
/// <param name="reuse">{true} to allow the socket to be reusable, {false} to discard the socket</param>
/// <exception cref="SocketException">An error occurred when trying to access the socket.</exception>
partial void SocketDisconnect(bool reuse)
{
_socket.Shutdown(SocketShutdown.Both);
_socket.Disconnect(false);
}

```
Since this is a partial, you have to put the stub declaration at the top of Session.cs thusly:
```
/// <summary>
/// Closes the socket and optionally flags the socket for reuse. Added kr 4/13/2016 to prevent hanging when closing a reusable socket
/// </summary>
/// <param name="reuse">{true} to allow the socket to be reusable, {false} to discard the socket</param>
/// <exception cref="SocketException">An error occurred when trying to access the socket.</exception>
partial void SocketDisconnect(bool reuse);
```
Finally - changed the call in SocketDisconnectAndDispose in Session.cs to call my overload method with the reuse parameter set to false:
```
private void SocketDisconnectAndDispose()
{
if (_socket != null)
{
lock (_socketLock)
{
if (_socket != null)
{
if (_socket.Connected)
SocketDisconnect(false);
_socket.Dispose();
_socket = null;
}
}
}
}

```


Viewing all articles
Browse latest Browse all 1026

Trending Articles