Skip to content

Commit ddb7c39

Browse files
committed
Add a new stream only when the connection state is checked
1 parent 9d7e73b commit ddb7c39

File tree

2 files changed

+11
-14
lines changed

2 files changed

+11
-14
lines changed

src/DotNext.Tests/Net/Multiplexing/TcpMultiplexerTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,4 +267,11 @@ public static async Task WaitForDisposedConnectionAsync()
267267

268268
await ThrowsAsync<ObjectDisposedException>(Func.Constant(task));
269269
}
270+
271+
[Fact]
272+
public static async Task WaitForCanceledConnectionAsync()
273+
{
274+
await using var client = new TcpMultiplexedClient(LocalEndPoint, new() { Timeout = DefaultTimeout });
275+
await ThrowsAnyAsync<OperationCanceledException>(client.OpenStreamAsync(new CancellationToken(canceled: true)).AsTask);
276+
}
270277
}

src/cluster/DotNext.Net.Cluster/Net/Multiplexing/MultiplexedClient.cs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -89,24 +89,14 @@ public ValueTask StartAsync(CancellationToken token = default)
8989
/// <seealso cref="DotNext.IO.Pipelines.DuplexStream"/>
9090
public async ValueTask<IDuplexPipe> OpenStreamAsync(CancellationToken token = default)
9191
{
92-
for (var stream = OpenStream(out var addedStreamId);; token.ThrowIfCancellationRequested())
93-
{
94-
try
95-
{
96-
await EnsureConnectedAsync(token).ConfigureAwait(false);
97-
return stream;
98-
}
99-
catch (Exception e)
100-
{
101-
input.TryRemoveStream(addedStreamId, stream);
102-
await stream.AbortAppSideAsync(e).ConfigureAwait(false);
103-
}
104-
}
92+
await EnsureConnectedAsync(token).ConfigureAwait(false);
93+
return OpenStream();
10594
}
10695

107-
private MultiplexedStream OpenStream(out uint id)
96+
private MultiplexedStream OpenStream()
10897
{
10998
var stream = new MultiplexedStream(options, writeSignal);
99+
uint id;
110100
do
111101
{
112102
id = Interlocked.Increment(ref streamId);

0 commit comments

Comments
 (0)