Skip to content

Commit

Permalink
add ConfigureAwait(false) to avoid deadlock
Browse files Browse the repository at this point in the history
  • Loading branch information
jnettleton authored and clemensv committed Aug 29, 2024
1 parent b55666d commit 744fb70
Show file tree
Hide file tree
Showing 11 changed files with 168 additions and 150 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 21 additions & 21 deletions src/Microsoft.Azure.Relay.Bridge/RemoteForwardBridge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public async void Close()
shuttingDown.Cancel(); ;

this.IsOpen = false;
await this.listener.CloseAsync(TimeSpan.FromSeconds(5));
await this.listener.CloseAsync(TimeSpan.FromSeconds(5)).ConfigureAwait(false);
}

public void Dispose()
Expand Down Expand Up @@ -100,7 +100,7 @@ public async Task Open()
};
}

await listener.OpenAsync(shuttingDown.Token);
await listener.OpenAsync(shuttingDown.Token).ConfigureAwait(false);
this.IsOpen = true;

AcceptLoopAsync().Fork(this);
Expand All @@ -112,7 +112,7 @@ async Task AcceptLoopAsync()
{
try
{
var hybridConnectionStream = await listener.AcceptConnectionAsync();
var hybridConnectionStream = await listener.AcceptConnectionAsync().ConfigureAwait(false);
if (hybridConnectionStream == null)
{
// we only get null if trhe listener is shutting down
Expand Down Expand Up @@ -144,10 +144,10 @@ async Task HandleRelayConnectionAsync(HybridConnectionStream hybridConnectionStr
for (int read = 0; read < versionPreamble.Length;)
{
var r = await hybridConnectionStream.ReadAsync(versionPreamble, read,
versionPreamble.Length - read);
versionPreamble.Length - read).ConfigureAwait(false);
if (r == 0)
{
await hybridConnectionStream.ShutdownAsync(CancellationToken.None);
await hybridConnectionStream.ShutdownAsync(CancellationToken.None).ConfigureAwait(false);
return;
}

Expand All @@ -162,20 +162,20 @@ async Task HandleRelayConnectionAsync(HybridConnectionStream hybridConnectionStr
// length indicator and then that number of bytes with of UTF-8 encoded
// port-name string.
var portNameBuffer = new byte[256];
var r = await hybridConnectionStream.ReadAsync(portNameBuffer, 0, 1);
var r = await hybridConnectionStream.ReadAsync(portNameBuffer, 0, 1).ConfigureAwait(false);
if (r == 0)
{
await hybridConnectionStream.ShutdownAsync(CancellationToken.None);
await hybridConnectionStream.ShutdownAsync(CancellationToken.None).ConfigureAwait(false);
return;
}

for (int read = 0; read < portNameBuffer[0];)
{
r = await hybridConnectionStream.ReadAsync(portNameBuffer, read + 1,
portNameBuffer[0] - read);
portNameBuffer[0] - read).ConfigureAwait(false);
if (r == 0)
{
await hybridConnectionStream.ShutdownAsync(CancellationToken.None);
await hybridConnectionStream.ShutdownAsync(CancellationToken.None).ConfigureAwait(false);
return;
}

Expand All @@ -188,8 +188,8 @@ async Task HandleRelayConnectionAsync(HybridConnectionStream hybridConnectionStr
{
// if we don't understand the version, we write a 0.0 version preamble back and shut down the connection
versionPreamble = new byte[] { 0, 0, 0 };
await hybridConnectionStream.WriteAsync(versionPreamble, 0, versionPreamble.Length);
await CloseConnection(hybridConnectionStream);
await hybridConnectionStream.WriteAsync(versionPreamble, 0, versionPreamble.Length).ConfigureAwait(false);
await CloseConnection(hybridConnectionStream).ConfigureAwait(false);
return;
}

Expand All @@ -212,28 +212,28 @@ async Task HandleRelayConnectionAsync(HybridConnectionStream hybridConnectionStr
{
// bad datagram indicator
versionPreamble = new byte[] { 0, 0, 1 };
await hybridConnectionStream.WriteAsync(versionPreamble, 0, versionPreamble.Length);
await CloseConnection(hybridConnectionStream);
await hybridConnectionStream.WriteAsync(versionPreamble, 0, versionPreamble.Length).ConfigureAwait(false);
await CloseConnection(hybridConnectionStream).ConfigureAwait(false);
return;
}
else if (!(forwarder is UdpRemoteForwarder) && versionPreamble[2] == 1)
{
// mismatch
versionPreamble = new byte[] { 0, 0, 255 };
await hybridConnectionStream.WriteAsync(versionPreamble, 0, versionPreamble.Length);
await CloseConnection(hybridConnectionStream);
await hybridConnectionStream.WriteAsync(versionPreamble, 0, versionPreamble.Length).ConfigureAwait(false);
await CloseConnection(hybridConnectionStream).ConfigureAwait(false);
return;
}

// write out 1.0 and handle the stream
versionPreamble = new byte[] { 1, 0, versionPreamble[2] };
await hybridConnectionStream.WriteAsync(versionPreamble, 0, versionPreamble.Length);
await forwarder.HandleConnectionAsync(hybridConnectionStream);
await CloseConnection(hybridConnectionStream);
await hybridConnectionStream.WriteAsync(versionPreamble, 0, versionPreamble.Length).ConfigureAwait(false);
await forwarder.HandleConnectionAsync(hybridConnectionStream).ConfigureAwait(false);
await CloseConnection(hybridConnectionStream).ConfigureAwait(false);
}
else
{
await CloseConnection(hybridConnectionStream);
await CloseConnection(hybridConnectionStream).ConfigureAwait(false);
}
}
}
Expand All @@ -247,8 +247,8 @@ static async Task CloseConnection(HybridConnectionStream hybridConnectionStream)
{
using (var cts = new CancellationTokenSource(TimeSpan.FromMinutes(1)))
{
await hybridConnectionStream.ShutdownAsync(cts.Token);
await hybridConnectionStream.CloseAsync(cts.Token);
await hybridConnectionStream.ShutdownAsync(cts.Token).ConfigureAwait(false);
await hybridConnectionStream.CloseAsync(cts.Token).ConfigureAwait(false);
}
}
}
Expand Down
22 changes: 11 additions & 11 deletions src/Microsoft.Azure.Relay.Bridge/SocketLocalForwardBridge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ async Task AcceptSocketLoopAsync()

try
{
socket = await this.socketListener.AcceptAsync();
socket = await this.socketListener.AcceptAsync().ConfigureAwait(false);
}
catch (ObjectDisposedException)
{
Expand Down Expand Up @@ -192,7 +192,7 @@ async Task BridgeSocketConnectionAsync(Socket socket)
socket.SendTimeout = 60000;
var tcpstream = new NetworkStream(socket);

using (var hybridConnectionStream = await HybridConnectionClient.CreateConnectionAsync())
using (var hybridConnectionStream = await HybridConnectionClient.CreateConnectionAsync().ConfigureAwait(false))
{
// read and write 4-byte header
hybridConnectionStream.WriteTimeout = 60000;
Expand All @@ -206,18 +206,18 @@ async Task BridgeSocketConnectionAsync(Socket socket)
/*stream */ 0,
(byte)portNameBytes.Length
};
await hybridConnectionStream.WriteAsync(preamble, 0, preamble.Length);
await hybridConnectionStream.WriteAsync(portNameBytes, 0, portNameBytes.Length);
await hybridConnectionStream.WriteAsync(preamble, 0, preamble.Length).ConfigureAwait(false);
await hybridConnectionStream.WriteAsync(portNameBytes, 0, portNameBytes.Length).ConfigureAwait(false);

byte[] replyPreamble = new byte[3];
for (int read = 0; read < replyPreamble.Length;)
{
var r = await hybridConnectionStream.ReadAsync(replyPreamble, read,
replyPreamble.Length - read);
replyPreamble.Length - read).ConfigureAwait(false);
if (r == 0)
{
await hybridConnectionStream.ShutdownAsync(CancellationToken.None);
await hybridConnectionStream.CloseAsync(CancellationToken.None);
await hybridConnectionStream.ShutdownAsync(CancellationToken.None).ConfigureAwait(false);
await hybridConnectionStream.CloseAsync(CancellationToken.None).ConfigureAwait(false);
throw new InvalidOperationException($"Malformed preamble from server");
}
read += r;
Expand All @@ -226,8 +226,8 @@ async Task BridgeSocketConnectionAsync(Socket socket)
if (!(replyPreamble[0] == 1 && replyPreamble[1] == 0 && replyPreamble[2] == 0))
{
// version not supported
await hybridConnectionStream.ShutdownAsync(CancellationToken.None);
await hybridConnectionStream.CloseAsync(CancellationToken.None);
await hybridConnectionStream.ShutdownAsync(CancellationToken.None).ConfigureAwait(false);
await hybridConnectionStream.CloseAsync(CancellationToken.None).ConfigureAwait(false);
throw new InvalidOperationException($"Unsupported protocol version: Server reply {replyPreamble[0]} {replyPreamble[1]} {replyPreamble[2]}");
}

Expand All @@ -243,11 +243,11 @@ await Task.WhenAll(
.ContinueWith((t)=>socketAbort.Cancel(), TaskContinuationOptions.OnlyOnFaulted),
StreamPump.RunAsync(tcpstream, hybridConnectionStream,
() => hybridConnectionStream?.Shutdown(), socketAbort.Token))
.ContinueWith((t) => socketAbort.Cancel(), TaskContinuationOptions.OnlyOnFaulted);
.ContinueWith((t) => socketAbort.Cancel(), TaskContinuationOptions.OnlyOnFaulted).ConfigureAwait(false);

using (var cts = new CancellationTokenSource(TimeSpan.FromMinutes(1)))
{
await hybridConnectionStream.CloseAsync(cts.Token);
await hybridConnectionStream.CloseAsync(cts.Token).ConfigureAwait(false);
}
}
catch
Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.Azure.Relay.Bridge/SocketRemoteForwarder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public async Task HandleConnectionAsync(Microsoft.Azure.Relay.HybridConnectionSt
{
socket.SendBufferSize = socket.ReceiveBufferSize = 65536;
socket.SendTimeout = 60000;
await socket.ConnectAsync(new UnixDomainSocketEndPoint(targetServer));
await socket.ConnectAsync(new UnixDomainSocketEndPoint(targetServer)).ConfigureAwait(false);
var tcpstream = new NetworkStream(socket);

CancellationTokenSource socketAbort = new CancellationTokenSource();
Expand All @@ -38,7 +38,7 @@ await Task.WhenAll(
.ContinueWith((t) => socketAbort.Cancel(), TaskContinuationOptions.OnlyOnFaulted),
StreamPump.RunAsync(tcpstream, hybridConnectionStream, () => hybridConnectionStream.Shutdown(),
socketAbort.Token))
.ContinueWith((t) => socketAbort.Cancel(), TaskContinuationOptions.OnlyOnFaulted);
.ContinueWith((t) => socketAbort.Cancel(), TaskContinuationOptions.OnlyOnFaulted).ConfigureAwait(false);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.Azure.Relay.Bridge/StreamPump.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static async Task RunAsync(Stream source, Stream target, Action shutdownA
int bytesRead;

bytesRead = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false);

if (bytesRead == 0)
{
shutdownAction?.Invoke();
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.Azure.Relay.Bridge/TaskEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static void Fork(this Task thisTask)
/// await TaskEx.FromAsync(
/// (c, s) => ((Transaction)s).BeginCommit(c, s),
/// (a) => ((Transaction)a.AsyncState).EndCommit(a),
/// transaction);
/// transaction).ConfigureAwait(false).ConfigureAwait(false);
/// </param>
public static Task FromAsync(Func<AsyncCallback, object, IAsyncResult> begin, Action<IAsyncResult> end, object state = null)
{
Expand Down
24 changes: 12 additions & 12 deletions src/Microsoft.Azure.Relay.Bridge/TcpLocalForwardBridge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ async Task AcceptSocketLoopAsync()

try
{
socket = await this.tcpListener.AcceptTcpClientAsync();
socket = await this.tcpListener.AcceptTcpClientAsync().ConfigureAwait(false);
}
catch (ObjectDisposedException)
{
Expand Down Expand Up @@ -191,7 +191,7 @@ async Task BridgeSocketConnectionAsync(TcpClient tcpClient)
var tcpstream = tcpClient.GetStream();
var socket = tcpClient.Client;

using (var hybridConnectionStream = await HybridConnectionClient.CreateConnectionAsync())
using (var hybridConnectionStream = await HybridConnectionClient.CreateConnectionAsync().ConfigureAwait(false))
{
// read and write version preamble
hybridConnectionStream.WriteTimeout = 60000;
Expand All @@ -205,18 +205,18 @@ async Task BridgeSocketConnectionAsync(TcpClient tcpClient)
/*stream mode*/ 0,
(byte)portNameBytes.Length
};
await hybridConnectionStream.WriteAsync(preamble, 0, preamble.Length);
await hybridConnectionStream.WriteAsync(portNameBytes, 0, portNameBytes.Length);
await hybridConnectionStream.WriteAsync(preamble, 0, preamble.Length).ConfigureAwait(false);
await hybridConnectionStream.WriteAsync(portNameBytes, 0, portNameBytes.Length).ConfigureAwait(false);

byte[] replyPreamble = new byte[3];
for (int read = 0; read < replyPreamble.Length;)
{
var r = await hybridConnectionStream.ReadAsync(replyPreamble, read,
replyPreamble.Length - read);
replyPreamble.Length - read).ConfigureAwait(false);
if (r == 0)
{
await hybridConnectionStream.ShutdownAsync(CancellationToken.None);
await hybridConnectionStream.CloseAsync(CancellationToken.None);
await hybridConnectionStream.ShutdownAsync(CancellationToken.None).ConfigureAwait(false);
await hybridConnectionStream.CloseAsync(CancellationToken.None).ConfigureAwait(false);
return;
}
read += r;
Expand All @@ -225,8 +225,8 @@ async Task BridgeSocketConnectionAsync(TcpClient tcpClient)
if (!(replyPreamble[0] == 1 && replyPreamble[1] == 0 && replyPreamble[2] == 0))
{
// version not supported
await hybridConnectionStream.ShutdownAsync(CancellationToken.None);
await hybridConnectionStream.CloseAsync(CancellationToken.None);
await hybridConnectionStream.ShutdownAsync(CancellationToken.None).ConfigureAwait(false);
await hybridConnectionStream.CloseAsync(CancellationToken.None).ConfigureAwait(false);
return;
}

Expand All @@ -242,11 +242,11 @@ await Task.WhenAll(
.ContinueWith((t)=>socketAbort.Cancel(), TaskContinuationOptions.OnlyOnFaulted),
StreamPump.RunAsync(tcpstream, hybridConnectionStream,
() => hybridConnectionStream?.Shutdown(), socketAbort.Token))
.ContinueWith((t) => socketAbort.Cancel(), TaskContinuationOptions.OnlyOnFaulted);
.ContinueWith((t) => socketAbort.Cancel(), TaskContinuationOptions.OnlyOnFaulted).ConfigureAwait(false);

using (var cts = new CancellationTokenSource(TimeSpan.FromMinutes(1)))
{
await hybridConnectionStream.CloseAsync(cts.Token);
await hybridConnectionStream.CloseAsync(cts.Token).ConfigureAwait(false);
}
}
catch
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.Azure.Relay.Bridge/TcpRemoteForwarder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ await Task.WhenAll(
() => client.Client.Shutdown(SocketShutdown.Send), socketAbort.Token)
.ContinueWith((t) => socketAbort.Cancel(), TaskContinuationOptions.OnlyOnFaulted),
StreamPump.RunAsync(tcpstream, hybridConnectionStream, () => hybridConnectionStream.Shutdown(), socketAbort.Token))
.ContinueWith((t) => socketAbort.Cancel(), TaskContinuationOptions.OnlyOnFaulted);
.ContinueWith((t) => socketAbort.Cancel(), TaskContinuationOptions.OnlyOnFaulted).ConfigureAwait(false);

}

Expand Down
Loading

0 comments on commit 744fb70

Please sign in to comment.