Skip to content

Commit 6dc2773

Browse files
committed
Remap ObjectDisposedException to IOException if needed.
1 parent 86802fe commit 6dc2773

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

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

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -259,15 +259,23 @@ private static async Task ExecuteOperationWithTimeoutAsync<TState>(Stream stream
259259
var completedTask = await Task.WhenAny(operationTask, cancellationTask).ConfigureAwait(false);
260260
if (completedTask == operationTask)
261261
{
262-
263-
await operationTask.ConfigureAwait(false); // Will re-throw exception if any
264-
return;
262+
await operationTask.ConfigureAwait(false); // Will re-throw exception if any
263+
return;
265264
}
266265
}
267-
catch (ObjectDisposedException)
266+
catch (Exception ex)
268267
{
269-
// It's possible to get ObjectDisposedException when the connection pool was closed with interruptInUseConnections set to true.
270-
throw new IOException();
268+
if (cancellationToken.IsCancellationRequested)
269+
{
270+
throw new TaskCanceledException();
271+
}
272+
273+
if (ex is ObjectDisposedException)
274+
{
275+
throw new IOException();
276+
}
277+
278+
throw;
271279
}
272280

273281
// if we reach here - then operation was either cancelled or timed out
@@ -319,19 +327,19 @@ private static void ExecuteOperationWithTimeout<TState>(Stream stream, TState st
319327
throw new IOException();
320328
}
321329
}
322-
catch (ObjectDisposedException)
323-
{
324-
// It's possible to get ObjectDisposedException when the connection pool was closed with interruptInUseConnections set to true.
325-
throw new IOException();
326-
}
327-
catch (IOException)
330+
catch (Exception ex)
328331
{
329332
if (callbackState?.OperationState == OperationState.Interrupted)
330333
{
331334
cancellationToken.ThrowIfCancellationRequested();
332335
throw new TimeoutException();
333336
}
334337

338+
if (ex is ObjectDisposedException)
339+
{
340+
throw new IOException();
341+
}
342+
335343
throw;
336344
}
337345
finally

0 commit comments

Comments
 (0)