Commit b2c05dec authored by Marc Gravell's avatar Marc Gravell

make _socket/_ioPipe more resilient to threading nasties

parent 221fac32
...@@ -56,6 +56,7 @@ private static readonly Message ...@@ -56,6 +56,7 @@ private static readonly Message
private IDuplexPipe _ioPipe; private IDuplexPipe _ioPipe;
private Socket _socket; private Socket _socket;
private Socket VolatileSocket => Interlocked.CompareExchange(ref _socket, null, null);
public PhysicalConnection(PhysicalBridge bridge) public PhysicalConnection(PhysicalBridge bridge)
{ {
...@@ -99,7 +100,7 @@ internal async void BeginConnectAsync(TextWriter log) ...@@ -99,7 +100,7 @@ internal async void BeginConnectAsync(TextWriter log)
{ {
_socketArgs.Completed += SocketAwaitable.Callback; _socketArgs.Completed += SocketAwaitable.Callback;
var x = _socket; var x = VolatileSocket;
if (x == null) if (x == null)
{ {
awaitable.TryComplete(0, SocketError.ConnectionAborted); awaitable.TryComplete(0, SocketError.ConnectionAborted);
...@@ -125,7 +126,7 @@ internal async void BeginConnectAsync(TextWriter log) ...@@ -125,7 +126,7 @@ internal async void BeginConnectAsync(TextWriter log)
timeoutSource.Cancel(); timeoutSource.Cancel();
timeoutSource.Dispose(); timeoutSource.Dispose();
} }
var x = _socket; var x = VolatileSocket;
if (x == null) if (x == null)
{ {
ConnectionMultiplexer.TraceWithoutContext("Socket was already aborted"); ConnectionMultiplexer.TraceWithoutContext("Socket was already aborted");
...@@ -228,10 +229,8 @@ private enum ReadMode : byte ...@@ -228,10 +229,8 @@ private enum ReadMode : byte
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times")] [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times")]
internal void Shutdown() internal void Shutdown()
{ {
var ioPipe = _ioPipe; var ioPipe = Interlocked.Exchange(ref _ioPipe, null); // compare to the critical read
var socket = _socket; var socket = Interlocked.Exchange(ref _socket, null);
_ioPipe = null;
_socket = null;
if (ioPipe != null) if (ioPipe != null)
{ {
...@@ -255,7 +254,7 @@ internal void Shutdown() ...@@ -255,7 +254,7 @@ internal void Shutdown()
public void Dispose() public void Dispose()
{ {
bool markDisposed = _socket != null; bool markDisposed = VolatileSocket != null;
Shutdown(); Shutdown();
if (markDisposed) if (markDisposed)
{ {
...@@ -1097,7 +1096,7 @@ internal static void WriteInteger(PipeWriter writer, long value) ...@@ -1097,7 +1096,7 @@ internal static void WriteInteger(PipeWriter writer, long value)
writer.Advance(bytes); writer.Advance(bytes);
} }
internal int GetAvailableInboundBytes() => _socket?.Available ?? -1; internal int GetAvailableInboundBytes() => VolatileSocket?.Available ?? -1;
private RemoteCertificateValidationCallback GetAmbientIssuerCertificateCallback() private RemoteCertificateValidationCallback GetAmbientIssuerCertificateCallback()
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment