title: why my polly timeout policy seems not firing
body: <p>This is my Polly implementation, it has two policy, one timeout and one retry. The idea is that when sql time out, the timeout span will become longer, so sql server got more time to do the work.</p>
  <p>However, when using a sp that takes minutes to finish to simulate timeout, I do not see the timeout policy fired 3 times(either attach a debugger or just search the output log). It fired once and then <code>TimeoutRejectedException</code> will be thrown.</p>
  <pre><code>var timeoutPerTry = Policy
      .TimeoutAsync(context =&gt;
      {
          ////enlarge timeout every time it happens
  
          taskTimeoutInSeconds = (int)(timeoutMs / 1000);
  
          Log.LogVerbose(
              $&quot;log something&quot;);
          return TimeSpan.FromMilliseconds(timeoutMs);
      }, TimeoutStrategy.Optimistic);
  
  // retry SqlException up to MaxRetries
  var retryPolicy = Policy
      .Handle&lt;SqlException&gt;()
      .RetryAsync(Constants.MaxRetries,
          (response, calculatedWaitDuration, context) =&gt;
          {
              Log.LogError(
                  $&quot;Failed dynamic execution attempt. Retrying. {response.Message} - {response.StackTrace}&quot;);
          });
  
  try
  {
      ////combine timeout policy and retry policy
      var combinedPolicy = retryPolicy.WrapAsync(timeoutPerTry);
      // ReSharper disable once AccessToDisposedClosure
      var results =
          await combinedPolicy.ExecuteAsync&lt;IEnumerable&lt;T&gt;&gt;(async () =&gt; {
  
              var connectionString = ConnectionStringHelper.GetConnectionString(warehouseId);
              using (var connection = new SqlConnection(connectionString))  // assumed no need for using block as closed by caller
              {
                  await connection.OpenAsync();
                  using (var cmd = new SqlCommand
                  {
                      CommandType = commandType,
                      CommandTimeout = taskTimeoutInSeconds, // in secs
                      CommandText = &quot;JerrySimulateSlowSp&quot;
                  })
                  {
                      cmd.Parameters.AddRange(parameters.ToArray());
                      cmd.Connection = connection;
  
                      using (var reader = await cmd.ExecuteReaderAsync(CommandBehavior.CloseConnection))
                      {
                          return mapper.Map&lt;IDataReader, IEnumerable&lt;T&gt;&gt;(reader);
                      }
                  }
              }
          });
      return results;
      //cmd.Connection = null;        
  }
  catch (SqlException ex) when (ex.Number == -2)  // -2 is a sql timeout
  {
      throw new ThunderTimeoutException(Constants.HttpResponseTimeoutSql);
  }
  catch (TimeoutRejectedException)
  {
      throw new ThunderTimeoutException(Constants.HttpResponseTimeoutTask);
  }
  </code></pre>