Skip to content

Commit aa70718

Browse files
authored
More AsyncSocketListener patchwork (#1408)
* More AsyncSocketListener patchwork This is Whack-a-mole part 2 in diagnosing or preventing the test host from crashing in CI. * Fix test
1 parent 1143ad3 commit aa70718

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

test/Renci.SshNet.Tests/Classes/Channels/ChannelDirectTcpipTest_Dispose_SessionIsConnectedAndChannelIsOpen.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,21 @@ private void Arrange()
130130
})
131131
.Returns(WaitResult.Success);
132132

133+
using var barrier = new Barrier(2);
134+
133135
var localEndpoint = new IPEndPoint(IPAddress.Loopback, 8122);
134136
_listener = new AsyncSocketListener(localEndpoint);
135137
_listener.Connected += socket =>
136138
{
137139
try
138140
{
141+
// We need the Connect side and the Accept side to be
142+
// fully completed before continuing: we are implicitly
143+
// checking that RemoteEndPoint on the Accept socket
144+
// matches LocalEndPoint on the Connect socket when
145+
// checking the correctness of the ChannelOpenMessage
146+
// in the mock.
147+
_ = barrier.SignalAndWait(TimeSpan.FromSeconds(1));
139148
_channel = new ChannelDirectTcpip(_sessionMock.Object,
140149
_localChannelNumber,
141150
_localWindowSize,
@@ -156,6 +165,7 @@ private void Arrange()
156165

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

160170
var clientReceiveThread = new Thread(
161171
() =>

test/Renci.SshNet.Tests/Common/AsyncSocketListener.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,7 @@ private void ReadCallback(IAsyncResult ar)
234234
try
235235
{
236236
// Read data from the client socket.
237-
bytesRead = handler.EndReceive(ar, out var errorCode);
238-
if (errorCode != SocketError.Success)
239-
{
240-
bytesRead = 0;
241-
}
237+
bytesRead = handler.EndReceive(ar);
242238
}
243239
catch (SocketException ex)
244240
{
@@ -275,7 +271,7 @@ private void ReadCallback(IAsyncResult ar)
275271
return;
276272
}
277273

278-
void ConnectionDisconnected(bool doShutdownIfApplicable)
274+
void ConnectionDisconnected()
279275
{
280276
SignalDisconnected(handler);
281277

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

291287
try
292288
{
293-
if (doShutdownIfApplicable)
289+
if (handler.Connected)
294290
{
295291
handler.Shutdown(SocketShutdown.Send);
296-
handler.Close();
297292
}
293+
294+
handler.Close();
298295
}
299296
catch (SocketException ex) when (ex.SocketErrorCode == SocketError.ConnectionReset)
300297
{
@@ -328,7 +325,7 @@ void ConnectionDisconnected(bool doShutdownIfApplicable)
328325
catch (ObjectDisposedException)
329326
{
330327
// TODO On .NET 7, sometimes we get ObjectDisposedException when _started but only on appveyor, locally it works
331-
ConnectionDisconnected(doShutdownIfApplicable: false);
328+
ConnectionDisconnected();
332329
}
333330
catch (SocketException ex)
334331
{
@@ -343,7 +340,7 @@ void ConnectionDisconnected(bool doShutdownIfApplicable)
343340
}
344341
else
345342
{
346-
ConnectionDisconnected(doShutdownIfApplicable: true);
343+
ConnectionDisconnected();
347344
}
348345
}
349346

0 commit comments

Comments
 (0)