Bug 12681 - Task.Wait() blocks forever, and I don't know why.
Summary: Task.Wait() blocks forever, and I don't know why.
Status: RESOLVED INVALID
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
URL:
Depends on:
Blocks:
 
Reported: 2013-06-13 17:12 UTC by Jonathan Pryor
Modified: 2013-06-13 18:14 UTC (History)
1 user (show)

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


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

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

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.

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