@@ -246,27 +246,29 @@ private static async Task ExecuteOperationWithTimeoutAsync<TState>(Stream stream
246246 cancellationTask = Task . Delay ( timeoutMs , cancellationToken ) ;
247247 }
248248
249- var operationTask = operation ( stream , state ) ;
250- if ( cancellationTask == null )
251- {
252- await operationTask . ConfigureAwait ( false ) ;
253- return ;
254- }
255-
256- var completedTask = await Task . WhenAny ( operationTask , cancellationTask ) . ConfigureAwait ( false ) ;
257- if ( completedTask == operationTask )
249+ Task operationTask ;
250+ try
258251 {
259- try
252+ operationTask = operation ( stream , state ) ;
253+ if ( cancellationTask == null )
260254 {
261- await operationTask . ConfigureAwait ( false ) ; // Will re-throw exception if any
255+ await operationTask . ConfigureAwait ( false ) ;
262256 return ;
263257 }
264- catch ( ObjectDisposedException )
258+
259+ var completedTask = await Task . WhenAny ( operationTask , cancellationTask ) . ConfigureAwait ( false ) ;
260+ if ( completedTask == operationTask )
265261 {
266- // It's possible to get ObjectDisposedException when the connection pool was closed with interruptInUseConnections set to true.
267- throw new IOException ( ) ;
262+
263+ await operationTask . ConfigureAwait ( false ) ; // Will re-throw exception if any
264+ return ;
268265 }
269266 }
267+ catch ( ObjectDisposedException )
268+ {
269+ // It's possible to get ObjectDisposedException when the connection pool was closed with interruptInUseConnections set to true.
270+ throw new IOException ( ) ;
271+ }
270272
271273 // if we reach here - then operation was either cancelled or timed out
272274 operationTask . IgnoreExceptions ( ) ;
0 commit comments