From 8054b6c8f685f54bcab7cb6ead601e1f79f438bb Mon Sep 17 00:00:00 2001 From: "gu.martinm@gmail.com" Date: Sun, 15 Jun 2014 19:45:51 +0200 Subject: [PATCH] WARNING!!! Deadlocks when using Task.WaitAll and async methods. --- .../HttpClientsExamples/HttpClientExample.cs | 30 +++++++++++++++++++++- .../HttpClientsExamples/HttpWebRequestExample.cs | 7 +++++ .../HttpClientsExamples/WebClientExample.cs | 7 +++++ .../ProcessLauncher/LocalizedResourceManager.cs | 12 +++++++++ 4 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 Allgemeines/ProcessLauncher/ProcessLauncher/LocalizedResourceManager.cs diff --git a/Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpClientExample.cs b/Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpClientExample.cs index f1a6a84..ca8ecc9 100644 --- a/Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpClientExample.cs +++ b/Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpClientExample.cs @@ -40,6 +40,13 @@ namespace HttpClientsExamples Task task = client.GetAsync (uri, HttpCompletionOption.ResponseContentRead, CancellationToken.None); try { + // DO NOT DO THIS. THERE COULD BE DEADLOCK. ALL DEPENDS ON THE SynchronizationContext + // How may I know what SynchronizationContext is going to be used? :/ + // See: http://msdn.microsoft.com/en-us/magazine/gg598924.aspx + // http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html + // http://stackoverflow.com/questions/22699048/why-does-task-waitall-not-block-or-cause-a-deadlock-here + // AFAIK, I SHOULD USE Task.WhenAll INSTEAD!!!! Because it creates a task (a new thread instead of blocking the current one) + // http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.whenall%28v=vs.110%29.aspx Task.WaitAll (task); } catch (AggregateException ae) { ae.Handle (e => { @@ -69,6 +76,13 @@ namespace HttpClientsExamples // option I do not think content will be null. Task taskStream = content.ReadAsStreamAsync (); try { + // DO NOT DO THIS. THERE COULD BE DEADLOCK. ALL DEPENDS ON THE SynchronizationContext + // How may I know what SynchronizationContext is going to be used? :/ + // See: http://msdn.microsoft.com/en-us/magazine/gg598924.aspx + // http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html + // http://stackoverflow.com/questions/22699048/why-does-task-waitall-not-block-or-cause-a-deadlock-here + // AFAIK, I SHOULD USE Task.WhenAll INSTEAD!!!! Because it creates a task (a new thread instead of blocking the current one) + // http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.whenall%28v=vs.110%29.aspx Task.WaitAll (taskStream); } catch (AggregateException ae) { ae.Handle (e => { @@ -119,6 +133,13 @@ namespace HttpClientsExamples } Task taskHttpClient = this.DoGetAsync (uri); try { + // DO NOT DO THIS. THERE COULD BE DEADLOCK. ALL DEPENDS ON THE SynchronizationContext + // How may I know what SynchronizationContext is going to be used? :/ + // See: http://msdn.microsoft.com/en-us/magazine/gg598924.aspx + // http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html + // http://stackoverflow.com/questions/22699048/why-does-task-waitall-not-block-or-cause-a-deadlock-here + // AFAIK, I SHOULD USE Task.WhenAll INSTEAD!!!! Because it creates a task (a new thread instead of blocking the current one) + // http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.whenall%28v=vs.110%29.aspx Task.WaitAll (taskHttpClient); } catch (AggregateException ae) { ae.Handle (e => { @@ -153,6 +174,13 @@ namespace HttpClientsExamples } taskHttpClient = this.DoGetStringAsync (uri); try { + // DO NOT DO THIS. THERE COULD BE DEADLOCK. ALL DEPENDS ON THE SynchronizationContext + // How may I know what SynchronizationContext is going to be used? :/ + // See: http://msdn.microsoft.com/en-us/magazine/gg598924.aspx + // http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html + // http://stackoverflow.com/questions/22699048/why-does-task-waitall-not-block-or-cause-a-deadlock-here + // AFAIK, I SHOULD USE Task.WhenAll INSTEAD!!!! Because it creates a task (a new thread instead of blocking the current one) + // http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.whenall%28v=vs.110%29.aspx Task.WaitAll (taskHttpClient); } catch (AggregateException ae) { ae.Handle (e => { @@ -213,7 +241,7 @@ namespace HttpClientsExamples } } - async private Task ReadResponseAsync(HttpContent content) + private async Task ReadResponseAsync(HttpContent content) { /** * Taken from HttpContent.cs ReadAsStringAsync() Mono implementation. diff --git a/Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpWebRequestExample.cs b/Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpWebRequestExample.cs index 1421679..3716155 100644 --- a/Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpWebRequestExample.cs +++ b/Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpWebRequestExample.cs @@ -64,6 +64,13 @@ namespace HttpClientsExamples httpWebRequest = (HttpWebRequest) WebRequest.Create(uri); Task task = httpWebRequest.GetResponseAsync (); try { + // DO NOT DO THIS. THERE COULD BE DEADLOCK. ALL DEPENDS ON THE SynchronizationContext + // How may I know what SynchronizationContext is going to be used? :/ + // See: http://msdn.microsoft.com/en-us/magazine/gg598924.aspx + // http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html + // http://stackoverflow.com/questions/22699048/why-does-task-waitall-not-block-or-cause-a-deadlock-here + // AFAIK, I SHOULD USE Task.WhenAll INSTEAD!!!! Because it creates a task (a new thread instead of blocking the current one) + // http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.whenall%28v=vs.110%29.aspx Task.WaitAll (task); } catch (AggregateException ae) { ae.Handle (e => { diff --git a/Allgemeines/HttpClientsExamples/HttpClientsExamples/WebClientExample.cs b/Allgemeines/HttpClientsExamples/HttpClientsExamples/WebClientExample.cs index a7d10ed..1d99623 100644 --- a/Allgemeines/HttpClientsExamples/HttpClientsExamples/WebClientExample.cs +++ b/Allgemeines/HttpClientsExamples/HttpClientsExamples/WebClientExample.cs @@ -173,6 +173,13 @@ namespace HttpClientsExamples // Don't do this. OpenReadTaskAsync is already launching a new Thread (OpenReadTaskAsync is intended to be used with async/await) //task.Start (); try { + // DO NOT DO THIS. THERE COULD BE DEADLOCK. ALL DEPENDS ON THE SynchronizationContext + // How may I know what SynchronizationContext is going to be used? :/ + // See: http://msdn.microsoft.com/en-us/magazine/gg598924.aspx + // http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html + // http://stackoverflow.com/questions/22699048/why-does-task-waitall-not-block-or-cause-a-deadlock-here + // AFAIK, I SHOULD USE Task.WhenAll INSTEAD!!!! Because it creates a task (a new thread instead of blocking the current one) + // http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.whenall%28v=vs.110%29.aspx Task.WaitAll (task); } catch (AggregateException ae) { ae.Handle (e => { diff --git a/Allgemeines/ProcessLauncher/ProcessLauncher/LocalizedResourceManager.cs b/Allgemeines/ProcessLauncher/ProcessLauncher/LocalizedResourceManager.cs new file mode 100644 index 0000000..b3c77bc --- /dev/null +++ b/Allgemeines/ProcessLauncher/ProcessLauncher/LocalizedResourceManager.cs @@ -0,0 +1,12 @@ +using System; + +namespace ProcessLauncher +{ + public class LocalizedResourceManager + { + public LocalizedResourceManager() + { + } + } +} + -- 2.1.4