private static void EndOperation <T>(ExecutionState <T> executionState) { Executor.FinishRequestAttempt(executionState); try { // If an operation has been canceled of timed out this should overwrite any exception Executor.CheckCancellation(executionState); Executor.CheckTimeout(executionState, true); // Success if (executionState.ExceptionRef == null) { // Step 9 - This will not be called if an exception is raised during stream copying Executor.ProcessEndOfRequest(executionState); executionState.OnComplete(); return; } } catch (Exception ex) { Logger.LogWarning(executionState.OperationContext, SR.TracePostProcessError, ex.Message); executionState.ExceptionRef = ExecutorBase.TranslateExceptionBasedOnParseError(ex, executionState.Cmd.CurrentResult, executionState.Resp, executionState.Cmd.ParseError); } finally { try { if (executionState.ReqStream != null) { executionState.ReqStream.Dispose(); executionState.ReqStream = null; } if (executionState.Resp != null) { executionState.Resp.Close(); executionState.Resp = null; } } catch (Exception) { // no op } } // Handle Retry try { StorageException translatedException = TranslateExceptionBasedOnParseError(executionState.ExceptionRef, executionState.Cmd.CurrentResult, executionState.Resp, executionState.Cmd.ParseError); executionState.ExceptionRef = translatedException; Logger.LogInformational(executionState.OperationContext, SR.TraceRetryCheck, executionState.RetryCount, executionState.Cmd.CurrentResult.HttpStatusCode, translatedException.IsRetryable ? "yes" : "no", translatedException.Message); bool shouldRetry = false; TimeSpan delay = TimeSpan.Zero; if (translatedException.IsRetryable && (executionState.RetryPolicy != null)) { executionState.CurrentLocation = Executor.GetNextLocation(executionState.CurrentLocation, executionState.RestCMD.LocationMode); Logger.LogInformational(executionState.OperationContext, SR.TraceNextLocation, executionState.CurrentLocation); IExtendedRetryPolicy extendedRetryPolicy = executionState.RetryPolicy as IExtendedRetryPolicy; if (extendedRetryPolicy != null) { RetryContext retryContext = new RetryContext( executionState.RetryCount++, executionState.Cmd.CurrentResult, executionState.CurrentLocation, executionState.RestCMD.LocationMode); RetryInfo retryInfo = extendedRetryPolicy.Evaluate(retryContext, executionState.OperationContext); if (retryInfo != null) { Logger.LogInformational(executionState.OperationContext, SR.TraceRetryInfo, retryInfo.TargetLocation, retryInfo.UpdatedLocationMode); shouldRetry = true; executionState.CurrentLocation = retryInfo.TargetLocation; executionState.RestCMD.LocationMode = retryInfo.UpdatedLocationMode; delay = retryInfo.RetryInterval; } } else { shouldRetry = executionState.RetryPolicy.ShouldRetry( executionState.RetryCount++, executionState.Cmd.CurrentResult.HttpStatusCode, executionState.ExceptionRef, out delay, executionState.OperationContext); } if ((delay < TimeSpan.Zero) || (delay > Constants.MaximumRetryBackoff)) { delay = Constants.MaximumRetryBackoff; } } if (!shouldRetry || (executionState.OperationExpiryTime.HasValue && (DateTime.Now + delay).CompareTo(executionState.OperationExpiryTime.Value) > 0)) { Logger.LogError(executionState.OperationContext, shouldRetry ? SR.TraceRetryDecisionTimeout : SR.TraceRetryDecisionPolicy, executionState.ExceptionRef.Message); // No Retry executionState.OnComplete(); } else { if (executionState.Cmd.RecoveryAction != null) { // I.E. Rewind stream etc. executionState.Cmd.RecoveryAction(executionState.Cmd, executionState.ExceptionRef, executionState.OperationContext); } if (delay > TimeSpan.Zero) { Logger.LogInformational(executionState.OperationContext, SR.TraceRetryDelay, (int)delay.TotalMilliseconds); executionState.UpdateCompletedSynchronously(false); if (executionState.BackoffTimer == null) { executionState.BackoffTimer = new Timer( Executor.RetryRequest <T>, executionState, (int)delay.TotalMilliseconds, Timeout.Infinite); } else { executionState.BackoffTimer.Change((int)delay.TotalMilliseconds, Timeout.Infinite); } executionState.CancelDelegate = () => { // Disabling the timer here, but there is still a scenario where the user calls cancel after // the timer starts the next retry but before it sets the CancelDelegate back to null. However, even // if that happens, next retry will start and then stop immediately because of the cancelled flag. Timer backoffTimer = executionState.BackoffTimer; if (backoffTimer != null) { executionState.BackoffTimer = null; backoffTimer.Dispose(); } Logger.LogWarning(executionState.OperationContext, SR.TraceAbortRetry); Executor.CheckCancellation(executionState); executionState.OnComplete(); }; } else { // Start next request immediately Executor.RetryRequest <T>(executionState); } } } catch (Exception ex) { // Catch all ( i.e. users retry policy throws etc.) Logger.LogWarning(executionState.OperationContext, SR.TraceRetryError, ex.Message); executionState.ExceptionRef = ExecutorBase.TranslateExceptionBasedOnParseError(ex, executionState.Cmd.CurrentResult, executionState.Resp, executionState.Cmd.ParseError); executionState.OnComplete(); } }