@@ -77,19 +77,28 @@ public static int Read(this Stream stream, byte[] buffer, int offset, int count,
77
77
78
78
public static async Task < int > ReadAsync ( this Stream stream , byte [ ] buffer , int offset , int count , TimeSpan timeout , CancellationToken cancellationToken )
79
79
{
80
- var readTask = stream . ReadAsync ( buffer , offset , count ) ;
80
+ Task < int > readTask = null ;
81
81
try
82
82
{
83
+ readTask = stream . ReadAsync ( buffer , offset , count ) ;
83
84
return await readTask . WaitAsync ( timeout , cancellationToken ) . ConfigureAwait ( false ) ;
84
85
}
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
+ }
85
91
catch ( Exception ex ) when ( ex is OperationCanceledException or TimeoutException )
86
92
{
87
93
// await Task.WaitAsync() throws OperationCanceledException in case of cancellation and TimeoutException in case of timeout
88
94
try
89
95
{
90
96
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
+ }
93
102
}
94
103
catch
95
104
{
@@ -204,6 +213,7 @@ public static void Write(this Stream stream, byte[] buffer, int offset, int coun
204
213
}
205
214
catch ( ObjectDisposedException )
206
215
{
216
+ // It's possible to get ObjectDisposedException when the connection pool was closed with interruptInUseConnections set to true.
207
217
throw new IOException ( ) ;
208
218
}
209
219
@@ -222,19 +232,28 @@ public static void Write(this Stream stream, byte[] buffer, int offset, int coun
222
232
223
233
public static async Task WriteAsync ( this Stream stream , byte [ ] buffer , int offset , int count , TimeSpan timeout , CancellationToken cancellationToken )
224
234
{
225
- var writeTask = stream . WriteAsync ( buffer , offset , count ) ;
235
+ Task writeTask = null ;
226
236
try
227
237
{
238
+ writeTask = stream . WriteAsync ( buffer , offset , count ) ;
228
239
await writeTask . WaitAsync ( timeout , cancellationToken ) . ConfigureAwait ( false ) ;
229
240
}
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
+ }
230
246
catch ( Exception ex ) when ( ex is OperationCanceledException or TimeoutException )
231
247
{
232
248
// await Task.WaitAsync() throws OperationCanceledException in case of cancellation and TimeoutException in case of timeout
233
249
try
234
250
{
235
251
stream . Dispose ( ) ;
236
252
// Should await on the task to avoid UnobservedTaskException
237
- await writeTask . ConfigureAwait ( false ) ;
253
+ if ( writeTask != null )
254
+ {
255
+ await writeTask . ConfigureAwait ( false ) ;
256
+ }
238
257
}
239
258
catch
240
259
{
0 commit comments