title: Storage getting blobs TaskCanceledException
body: <p>My application uses Azure storage blobs in order to download files from remote storage to the local machine. </p>
  
  <p>How it works: </p>
  
  <ol>
  <li>The machine getting a queue message with URL (signed URL) to Azure Storage blob.</li>
  <li>The application sending GET to download the file. It's done with <code>HttpClient</code>.</li>
  <li>Application logic continues from there.</li>
  </ol>
  
  <p>This is how I downloading the file: </p>
  
  <pre><code>HttpRequestMessage msg = new HttpRequestMessage(HttpMethod.Get, signedUrl);
  HttpClient.Timeout = TimeSpan.FromSeconds(120);
  var response = await HttpClient.SendAsync(msg);
  response.EnsureSuccessStatusCode();
  </code></pre>
  
  <p>I found that few times per day, I got exception:</p>
  
  <blockquote>
    <p>System.Threading.Tasks.TaskCanceledException: The operation was canceled.
         ---> System.IO.IOException: Unable to read data from the transport connection: Operation canceled.
         ---> System.Net.Sockets.SocketException: Operation canceled
           --- End of inner exception stack trace ---
           at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error)
           at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.GetResult(Int16 token)
           at System.Net.Security.SslStreamInternal.g__InternalFillBufferAsync|38_0[TReadAdapter](TReadAdapter adap, ValueTask<code>1 task, Int32 min, Int32 initial)
           at System.Net.Security.SslStreamInternal.ReadAsyncInternal[TReadAdapter](TReadAdapter adapter, Memory</code>1 buffer)
           at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
           --- End of inner exception stack trace ---
           at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
           at System.Net.Http.HttpConnectionPool.SendWithNtConnectionAuthAsync(HttpConnection connection, HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
           at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
           at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
           at System.Net.Http.DecompressionHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
           at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task<code>1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
           at Polly.Retry.AsyncRetryEngine.ImplementationAsync[TResult](Func</code>3 action, Context context, CancellationToken cancellationToken, ExceptionPredicates shouldRetryExceptionPredicates, ResultPredicates<code>1 shouldRetryResultPredicates, Func</code>5 onRetryAsync, Int32 permittedRetryCount, IEnumerable<code>1 sleepDurationsEnumerable, Func</code>4 sleepDurationProvider, Boolean continueOnCapturedContext)
           at Polly.AsyncPolicy<code>1.ExecuteAsync(Func</code>3 action, Context context, CancellationToken cancellationToken, Boolean continueOnCapturedContext)</p>
  </blockquote>
  
  <p>At this point, I must clarify that this behavior is happening to ~0.1% of the URLs passed in the system. </p>
  
  <p>I was suspecting that the problem is with the signed-URL. I found in all cases the signed URL was correct - I just put it in the browser and it was working.</p>
  
  <p>Now when I know that the problem is not the URL itself, I thought it can be a network problem. So, by using <a href="https://github.com/App-vNext/Polly" rel="nofollow noreferrer">Polly</a> I have added 5 retries with an exponential delay between each one. Up to 5 retries. Delay formula - 3^n (where n is the number of retries). In the worst case, it will wait 4 minutes before trying again... And even now - still same error. All 5 retries were used but the file was not downloaded.</p>
  
  <p>What to do? What else I should check?</p>