Bug 12681 - Task.Wait() blocks forever, and I don't know why.
Summary: Task.Wait() blocks forever, and I don't know why.
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries (show other bugs)
Version: 4.8.x
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Jonathan Pryor
Depends on:
Reported: 2013-06-13 17:12 UTC by Jonathan Pryor
Modified: 2013-06-13 18:14 UTC (History)
1 user (show)

See Also:
Is this bug a regression?: ---
Last known good build:

Scratch.TaskWaitDeadlock.zip (10.78 KB, application/zip)
2013-06-13 17:12 UTC, Jonathan Pryor

Description Jonathan Pryor 2013-06-13 17:12:30 UTC
Created attachment 4113 [details]

Consider the Scratch.TaskWaitDeadlocks project. The core method is this:

  void DownloadUrl ()
    var cb = ServicePointManager.ServerCertificateValidationCallback;
    ServicePointManager.ServerCertificateValidationCallback = 
        (s, cert, chain, policy) => {
      Console.WriteLine ("# ServerCertificateValidationCallback");
      return true;

    string url = "https://pipeline.internalx.com/";
    TaskStatus status     = 0;
    Exception  exception  = null;
    var downloadTask = new WebClient ().DownloadDataTaskAsync (url);
    var completeTask = downloadTask.ContinueWith (t => {
        Console.WriteLine ("# DownloadDataTaskAsync complete; " +
            "status={0}; exception={1}", t.Status, t.Exception);
        status    = t.Status;
        exception = t.Exception;
    Console.WriteLine ("# downloadTask.Status={0}; completeTask.Status={1}",
        downloadTask.Status, completeTask.Status);
    Console.WriteLine ("# BEFORE Wait()");
    completeTask.Wait ();
    Console.WriteLine ("# AFTER Wait()");
    ServicePointManager.ServerCertificateValidationCallback = cb;
    if (status != TaskStatus.RanToCompletion)
      throw new InvalidOperationException ("Unable to download URL! Status: " +
          status + "; Exception=" + exception);

If I execute that method in a "normal" console-app, it works as expected.

If I execute that method in a Xamarin.Android app, `completeTask.Wait()` never returns.

If I REMOVE `completeTask.Wait()` (and everything after it...), `completeTask` IS executed (as I see the "DownloadDataTaskAsync complete" message in `adb logcat`).

Thus, something specific to the Xamarin.Android environment is preventing Task.Wait() from ever returning.
Comment 1 Jonathan Pryor 2013-06-13 18:14:41 UTC
Not a bug; by design.

WebClient.DownloadDataTaskAsync() is an `async` method, and when started from the UI thread will call back onto the UI thread as part of the semantics. Since DownloadUrl() is executed from the UI thread and is waiting for a callback to the UI thread to complete, we have deadlock, and I'm a sad panda.

Doing the ~same thing in WPF has the same result, so this is intended behavior.

Notice (2018-05-21): bugzilla.xamarin.com will be switching to read-only mode on Thursday, 2018-05-25 22:00 UTC.

Please join us on Visual Studio Developer Community and GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs and copy them to the new locations as needed for follow-up. The See Also field on each Bugzilla bug will be updated with a link to its new location when applicable.

After Bugzilla is read-only, if you have new information to add for a bug that does not yet have a matching issue on Developer Community or GitHub, you can create a follow-up issue in the new location. Copy and paste the title and description from this bug, and then add your new details. You can get a pre-formatted version of the title and description here:

In special cases you might also want the comments:

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Note You need to log in before you can comment on or make changes to this bug.