Skip to content

Commit fc326c8

Browse files
committed
Detect both read/write state in IsStreamConnected.
1 parent c3b1564 commit fc326c8

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/Microsoft.Diagnostics.NETCore.Client/DiagnosticsServerRouter/DiagnosticsServerRouterFactory.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,18 @@ protected bool IsStreamConnected(Stream stream, CancellationToken token)
212212
bool blockingState = networkStream.Socket.Blocking;
213213
try
214214
{
215-
// Check connection by peek one byte. Will return 0 in case connection is closed.
215+
// Check connection read state by peek one byte. Will return 0 in case connection is closed.
216216
// A closed connection could also raise exception, but then socket connected state should
217217
// be set to false.
218218
networkStream.Socket.Blocking = false;
219219
if (networkStream.Socket.Receive(new byte[1], 0, 1, System.Net.Sockets.SocketFlags.Peek) == 0)
220220
connected = false;
221+
222+
// Check connection write state by sending non-blocking zero-byte data.
223+
// A closed connection should raise exception, but then socket connected state should
224+
// be set to false.
225+
if (connected)
226+
networkStream.Socket.Send(Array.Empty<byte>(), 0, System.Net.Sockets.SocketFlags.None);
221227
}
222228
catch (Exception)
223229
{

0 commit comments

Comments
 (0)