Skip to content

Commit 1711193

Browse files
committed
CSHARP-2884: Slight refactoring to match ConnectAsync.
1 parent 17adfa1 commit 1711193

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/MongoDB.Driver.Core/Core/Misc/StreamExtensionMethods.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,16 @@ public static async Task<int> ReadAsync(this Stream stream, byte[] buffer, int o
4747
try
4848
{
4949
bytesRead = await stream.ReadAsync(buffer, offset, count, cancellationToken).ConfigureAwait(false);
50-
ChangeState(2);
50+
ChangeState(2); // note: might not actually go to state 2 if already in state 3 or 4
51+
}
52+
catch when (state == 1)
53+
{
54+
try { stream.Dispose(); } catch { }
55+
throw;
5156
}
5257
catch when (state >= 3)
5358
{
54-
// a different exception will be thrown instead below
59+
// a timeout or operation cancelled exception will be thrown instead
5560
}
5661

5762
if (state == 3) { throw new TimeoutException(); }
@@ -161,11 +166,16 @@ public static async Task WriteAsync(this Stream stream, byte[] buffer, int offse
161166
try
162167
{
163168
await stream.WriteAsync(buffer, offset, count, cancellationToken).ConfigureAwait(false);
164-
ChangeState(2);
169+
ChangeState(2); // note: might not actually go to state 2 if already in state 3 or 4
170+
}
171+
catch when (state == 1)
172+
{
173+
try { stream.Dispose(); } catch { }
174+
throw;
165175
}
166176
catch when (state >= 3)
167177
{
168-
// a different exception will be thrown instead below
178+
// a timeout or operation cancelled exception will be thrown instead
169179
}
170180

171181
if (state == 3) { throw new TimeoutException(); }

0 commit comments

Comments
 (0)