title: Setting HttpClient to a too short timeout crashes process
body: <p>I've noticed that when I'm using <code>System.Net.HttpClient</code> with a short timeout, it may sometimes crash the process, even when it is wrapped in a try-catch block. Here's a short program to reproduce this.</p>
  
  <pre><code>public static void Main(string[] args)
  {
      var tasks = new List&lt;Task&gt;();
      for (int i = 0; i &lt; 1000; i++)
      {
          tasks.Add(MakeHttpClientRequest());
      }
      Task.WaitAll(tasks.ToArray());
  
  }
  
  private async static Task MakeHttpClientRequest()
  {            
      var httpClient = new HttpClient { Timeout = TimeSpan.FromMilliseconds(1) };
      var request = "whatever";
      try
      {
          HttpResponseMessage result =
              await httpClient.PostAsync("http://www.flickr.com/services/rest/?method=flickr.test.echo&amp;format=json&amp;api_key=766c0ac7802d55314fa980727f747710",
                                   new StringContent(request));             
          await result.Content.ReadAsStringAsync();                
      }
      catch (Exception x)
      {
          Console.WriteLine("Error occurred but it is swallowed: " + x);
      }
  }
  </code></pre>
  
  <p>Running this will crash the process with the following exception:</p>
  
  <pre><code>Unhandled Exception: System.AggregateException: One or more errors occurred. ---&gt; System.Net.WebException: The request was canceled
     at System.Net.ServicePointManager.FindServicePoint(Uri address, IWebProxy proxy, ProxyChain&amp; chain, HttpAbortDelegate&amp; abortDelegate, Int32&amp; abortState)
     at System.Net.HttpWebRequest.FindServicePoint(Boolean forceFind)
     at System.Net.HttpWebRequest.get_ServicePoint()
     at System.Net.AuthenticationState.PrepareState(HttpWebRequest httpWebRequest)
     at System.Net.AuthenticationState.ClearSession(HttpWebRequest httpWebRequest)
     at System.Net.HttpWebRequest.ClearAuthenticatedConnectionResources()
     at System.Net.HttpWebRequest.Abort(Exception exception, Int32 abortState)
     at System.Net.HttpWebRequest.Abort()
     at System.Net.Http.HttpClientHandler.OnCancel(Object state)
     at System.Threading.CancellationCallbackInfo.ExecutionContextCallback(Object obj)
     at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
     at System.Threading.CancellationCallbackInfo.ExecuteCallback()
     at System.Threading.CancellationTokenSource.CancellationCallbackCoreWork(CancellationCallbackCoreWorkArguments args)
     at System.Threading.CancellationTokenSource.ExecuteCallbackHandlers(Boolean throwOnFirstException)
     --- End of inner exception stack trace ---
     at System.Threading.CancellationTokenSource.ExecuteCallbackHandlers(Boolean throwOnFirstException)
     at System.Threading.CancellationTokenSource.NotifyCancellation(Boolean throwOnFirstException)
     at System.Threading.CancellationTokenSource.TimerCallbackLogic(Object obj)
     at System.Threading.TimerQueueTimer.CallCallbackInContext(Object state)
     at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
     at System.Threading.TimerQueueTimer.CallCallback()
     at System.Threading.TimerQueueTimer.Fire()
     at System.Threading.TimerQueue.FireNextTimers()
     at System.Threading.TimerQueue.AppDomainTimerCallback()
  </code></pre>
  
  <p>Digging in a little, it seems that when <code>HttpClient</code> aborts the request before a relevant <code>ServicePoint</code> is created, <code>HttpWebRequest</code> attempts to create the <code>ServicePoint</code>, via <code>ServicePointManager.FindServicePoint</code>, which throws a RequestCanceled. Since this exception is thrown in the thread that attempts to cancel the request, it is not caught, and the process dies.</p>
  
  <p>Am I missing something? Have you run into this issue? </p>