Skip to content

More AsyncSocketListener patchwork #1408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,21 @@ private void Arrange()
})
.Returns(WaitResult.Success);

using var barrier = new Barrier(2);

var localEndpoint = new IPEndPoint(IPAddress.Loopback, 8122);
_listener = new AsyncSocketListener(localEndpoint);
_listener.Connected += socket =>
{
try
{
// We need the Connect side and the Accept side to be
// fully completed before continuing: we are implicitly
// checking that RemoteEndPoint on the Accept socket
// matches LocalEndPoint on the Connect socket when
// checking the correctness of the ChannelOpenMessage
// in the mock.
_ = barrier.SignalAndWait(TimeSpan.FromSeconds(1));
_channel = new ChannelDirectTcpip(_sessionMock.Object,
_localChannelNumber,
_localWindowSize,
Expand All @@ -156,6 +165,7 @@ private void Arrange()

_client = new Socket(localEndpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
_client.Connect(localEndpoint);
_ = barrier.SignalAndWait(TimeSpan.FromSeconds(1));

var clientReceiveThread = new Thread(
() =>
Expand Down
17 changes: 7 additions & 10 deletions test/Renci.SshNet.Tests/Common/AsyncSocketListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,7 @@ private void ReadCallback(IAsyncResult ar)
try
{
// Read data from the client socket.
bytesRead = handler.EndReceive(ar, out var errorCode);
if (errorCode != SocketError.Success)
{
bytesRead = 0;
}
bytesRead = handler.EndReceive(ar);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Despite being documented as throwing, EndReceive(IAsyncResult, out SocketError) actually suppresses exceptions and translates them into the out SocketError parameter, at which point we would continue on and call ConnectionDisconnected(true), giving another way to crash.

}
catch (SocketException ex)
{
Expand Down Expand Up @@ -275,7 +271,7 @@ private void ReadCallback(IAsyncResult ar)
return;
}

void ConnectionDisconnected(bool doShutdownIfApplicable)
void ConnectionDisconnected()
{
SignalDisconnected(handler);

Expand All @@ -290,11 +286,12 @@ void ConnectionDisconnected(bool doShutdownIfApplicable)

try
{
if (doShutdownIfApplicable)
if (handler.Connected)
{
handler.Shutdown(SocketShutdown.Send);
handler.Close();
}

handler.Close();
}
catch (SocketException ex) when (ex.SocketErrorCode == SocketError.ConnectionReset)
{
Expand Down Expand Up @@ -328,7 +325,7 @@ void ConnectionDisconnected(bool doShutdownIfApplicable)
catch (ObjectDisposedException)
{
// TODO On .NET 7, sometimes we get ObjectDisposedException when _started but only on appveyor, locally it works
ConnectionDisconnected(doShutdownIfApplicable: false);
ConnectionDisconnected();
}
catch (SocketException ex)
{
Expand All @@ -343,7 +340,7 @@ void ConnectionDisconnected(bool doShutdownIfApplicable)
}
else
{
ConnectionDisconnected(doShutdownIfApplicable: true);
ConnectionDisconnected();
}
}

Expand Down