Skip to content

Commit 09aa20f

Browse files
committed
await all reads & writes before disposing the sockets
1 parent 7efcd64 commit 09aa20f

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

RfpProxy/TcpProxy.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,21 @@ private async Task HandleClientAsync(TcpClient client, CancellationToken cancell
9595
{
9696
await server.ConnectAsync(_server, _port, cts.Token).ConfigureAwait(false);
9797
var clientData = OnClientConnected(client, server);
98+
var tasks = new List<Task>();
9899
try
99100
{
100101
var clientPipe = new Pipe();
101102
var fillClientPipe = PipeHelper.FillPipeAsync(client.Client, clientPipe.Writer, cts.Token);
103+
tasks.Add(fillClientPipe);
102104
var readClientPipe = ReadFromClientAsync(clientData, clientPipe.Reader, cts.Token);
105+
tasks.Add(readClientPipe);
103106

104107
var serverPipe = new Pipe();
105108
var fillServerPipe = PipeHelper.FillPipeAsync(server.Client, serverPipe.Writer, cts.Token);
109+
tasks.Add(fillServerPipe);
106110
var readServerPipe = ReadFromServerAsync(clientData, serverPipe.Reader, cts.Token);
107-
108-
var t = await Task.WhenAny(fillClientPipe, readClientPipe, fillServerPipe, readServerPipe).ConfigureAwait(false);
111+
tasks.Add(readServerPipe);
112+
var t = await Task.WhenAny(tasks).ConfigureAwait(false);
109113
await t;
110114
}
111115
catch (Exception ex)
@@ -117,6 +121,7 @@ private async Task HandleClientAsync(TcpClient client, CancellationToken cancell
117121
{
118122
cts.Cancel();
119123
OnClientDisconnected(clientData);
124+
await Task.WhenAll(tasks);
120125
}
121126
}
122127
}

0 commit comments

Comments
 (0)