Locked Unexpected deadlock (partially-async WinRT)

All Replies

  • Wednesday, December 21, 2011 7:48 AM
     
     Answered Has Code

    Hi there,

    You are hitting a known issue in the .NET developer preview version that Microsoft distributed at the BUILD conference.

    The issue is caused by some subtle specifics of the WinRT UI thread. As a result, any blocking WinRT stream-IO from managed code will lead to a deadlock, if performed from the UI thread. Asynchronous IO (e.g. ReadAsync) will work fine. The development team is aware of the problem and is working to fix it.

    Please note, however, that even if and when the issue is fixed, performing blocking IO on the UI thread is not a good idea. Your application will block and be unresponsive for the duration of the operation. Some .NET APIs do not have async equivalents (yet) and even once they do, converting code may require work. If you have to perform a blocking IO operation, make sure to offload it to the thread pool:

    DoUIStuff();
    Int32 x = await Task.Run(() => {
        OpenStream();
        PerformBlockingIO();
        ProcessResults();
        return ComputeIOResults();
    });
    UseIOResults(x);
    

    This way you keep your application responsive at all times. As a side effect, you will also work around the abovementioned bug and avoid the deadlock.

     

    Greg

    (.NET Base Class Libraries Team)

    • Proposed As Answer by macrogreg Wednesday, December 21, 2011 7:49 AM
    • Marked As Answer by Stephen ClearyMVP Wednesday, December 21, 2011 7:40 PM
    •  
  • Friday, June 08, 2012 3:32 PM
     
     

    Note this issue seems to be fixed in the Windows 8 Release Preview.

  • Tuesday, July 17, 2012 2:53 PM
     
      Has Code

    This type of WinRT IO-Stream still lead to deadlocks in the Windows 8 Release Preview.

    string fileContent = await FileIO.ReadTextAsync(storageFile);
    For example, the above code doesn't works for me and cause a deadlock. 

    However, answer given by macrogreg works fine, waiting a better answer.