avoid oprhan connections after socket.ShutDown
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 4.4k
- Forks
- 993
- Avg merge
- 9d 21h
- Merged PRs (30d)
- 1
Description
using TcpView you will see many orphan connections, loosing their process(id). After some time (1 Min), the connection will be close by the OS. (use-case: The image shows the connections, login/logout in to a Sftp-Server every 5 sec, to check for new files)
To avoid oprhan connections [TimeWait) add a delay of 100ms! I think, the wait leads to a direct thread-change to fullfill the sending the disconnect message!?
Insert a delay of 100ms between
-> TryDisconnectSend
add the delay of 100ms
-> socket.ShutDown()
In Session.cs
...
private void Disconnect(DisconnectReason reason, string message)
{
// transition to disconnecting state to avoid throwing exceptions while cleaning up, and to
// ensure any exceptions that are raised do not overwrite the exception that is set
_isDisconnecting = true;
// send disconnect message to the server if the connection is still open (IsConnected is not correct!)
// and the disconnect message has not yet been sent
//
// note that this should also cause the listener loop to be interrupted as
// the server should respond by closing the socket
_socketDisposeLock.Wait();
var socketConnected = _socket != null && _socket.IsConnected();
_socketDisposeLock.Release();
if (socketConnected || IsConnected)
{
using (var delayEvent = new ManualResetEvent(initialState: false))
{
TrySendDisconnect(reason, message);
delayEvent.WaitOne(TimeSpan.FromMilliseconds(100));
}
}
// disconnect socket, and dispose it
SocketDisconnectAndDispose();
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in Session.cs at Disconnect, focusing on the TrySendDisconnect and SocketDisconnectAndDispose sequence. Reproduce the repeated SFTP login/logout scenario and inspect connections with TcpView; done means validating that the disconnect sequence no longer leaves orphan connections before the operating system closes them.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- networking
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100