Skip to content

Commit 5cfc066

Browse files
author
Oleksandr Poliakov
committed
Fix failed tests
1 parent 0b67a19 commit 5cfc066

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

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

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,28 @@ public static int Read(this Stream stream, byte[] buffer, int offset, int count,
7777

7878
public static async Task<int> ReadAsync(this Stream stream, byte[] buffer, int offset, int count, TimeSpan timeout, CancellationToken cancellationToken)
7979
{
80-
var readTask = stream.ReadAsync(buffer, offset, count);
80+
Task<int> readTask = null;
8181
try
8282
{
83+
readTask = stream.ReadAsync(buffer, offset, count);
8384
return await readTask.WaitAsync(timeout, cancellationToken).ConfigureAwait(false);
8485
}
86+
catch (ObjectDisposedException)
87+
{
88+
// It's possible to get ObjectDisposedException when the connection pool was closed with interruptInUseConnections set to true.
89+
throw new IOException();
90+
}
8591
catch (Exception ex) when (ex is OperationCanceledException or TimeoutException)
8692
{
8793
// await Task.WaitAsync() throws OperationCanceledException in case of cancellation and TimeoutException in case of timeout
8894
try
8995
{
9096
stream.Dispose();
91-
// Should await on the task to avoid UnobservedTaskException
92-
await readTask.ConfigureAwait(false);
97+
if (readTask != null)
98+
{
99+
// Should await on the task to avoid UnobservedTaskException
100+
await readTask.ConfigureAwait(false);
101+
}
93102
}
94103
catch
95104
{
@@ -204,6 +213,7 @@ public static void Write(this Stream stream, byte[] buffer, int offset, int coun
204213
}
205214
catch (ObjectDisposedException)
206215
{
216+
// It's possible to get ObjectDisposedException when the connection pool was closed with interruptInUseConnections set to true.
207217
throw new IOException();
208218
}
209219

@@ -222,19 +232,28 @@ public static void Write(this Stream stream, byte[] buffer, int offset, int coun
222232

223233
public static async Task WriteAsync(this Stream stream, byte[] buffer, int offset, int count, TimeSpan timeout, CancellationToken cancellationToken)
224234
{
225-
var writeTask = stream.WriteAsync(buffer, offset, count);
235+
Task writeTask = null;
226236
try
227237
{
238+
writeTask = stream.WriteAsync(buffer, offset, count);
228239
await writeTask.WaitAsync(timeout, cancellationToken).ConfigureAwait(false);
229240
}
241+
catch (ObjectDisposedException)
242+
{
243+
// It's possible to get ObjectDisposedException when the connection pool was closed with interruptInUseConnections set to true.
244+
throw new IOException();
245+
}
230246
catch (Exception ex) when (ex is OperationCanceledException or TimeoutException)
231247
{
232248
// await Task.WaitAsync() throws OperationCanceledException in case of cancellation and TimeoutException in case of timeout
233249
try
234250
{
235251
stream.Dispose();
236252
// Should await on the task to avoid UnobservedTaskException
237-
await writeTask.ConfigureAwait(false);
253+
if (writeTask != null)
254+
{
255+
await writeTask.ConfigureAwait(false);
256+
}
238257
}
239258
catch
240259
{

0 commit comments

Comments
 (0)