From a880910182352a0cd196d84e855cd3cd8bd108a8 Mon Sep 17 00:00:00 2001 From: Meri Khamoyan Date: Mon, 12 Feb 2024 11:54:30 +0100 Subject: [PATCH 1/5] Log ManagedThreadId and NativeThreadId for known test failures --- .../Runtime/InteropServices/JavaScript/WebWorkerTest.Http.cs | 1 + .../Runtime/InteropServices/JavaScript/WebWorkerTest.cs | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/WebWorkerTest.Http.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/WebWorkerTest.Http.cs index 8939cca759b19..cccc7e5e20aa0 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/WebWorkerTest.Http.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/WebWorkerTest.Http.cs @@ -82,6 +82,7 @@ await Assert.ThrowsAsync(async () => { CancellationTokenSource cts = new CancellationTokenSource(); var promise = response.Content.ReadAsStringAsync(cts.Token); + Console.WriteLine("HttpClient_CancelInDifferentThread: ManagedThreadId: " + Environment.CurrentManagedThreadId + " NativeThreadId: " + WebWorkerTestHelper.NativeThreadId); cts.Cancel(); await promise; }); diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/WebWorkerTest.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/WebWorkerTest.cs index c88d32277da8b..4c0be502edff9 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/WebWorkerTest.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/WebWorkerTest.cs @@ -368,6 +368,7 @@ await executor.Execute(async () => using var timer = new Timer(_ => { Assert.NotEqual(1, Environment.CurrentManagedThreadId); + Console.WriteLine("ThreadingTimer: ManagedThreadId: " + Environment.CurrentManagedThreadId + " NativeThreadId: " + WebWorkerTestHelper.NativeThreadId); Assert.True(Thread.CurrentThread.IsThreadPoolThread); tcs.SetResult(); hit = true; @@ -376,6 +377,7 @@ await executor.Execute(async () => await tcs.Task; }, cts.Token); + Console.WriteLine("ThreadingTimer: ManagedThreadId: " + Environment.CurrentManagedThreadId + " NativeThreadId: " + WebWorkerTestHelper.NativeThreadId); Assert.True(hit); } @@ -451,7 +453,7 @@ await executor.Execute(Task () => { exception = ex; } - + Console.WriteLine("WaitAssertsOnJSInteropThreads: ManagedThreadId: " + Environment.CurrentManagedThreadId + " NativeThreadId: " + WebWorkerTestHelper.NativeThreadId); executor.AssertBlockingWait(exception); return Task.CompletedTask; From 879ba5b4806476e304f3035e07f5398d99fd4eda Mon Sep 17 00:00:00 2001 From: Meri Khamoyan Date: Wed, 14 Feb 2024 09:02:34 +0100 Subject: [PATCH 2/5] Add more logs --- .../JavaScript/WebWorkerTest.Http.cs | 19 ++++++++++++++++--- .../JavaScript/WebWorkerTest.cs | 8 ++++---- .../JavaScript/WebWorkerTestBase.cs | 10 ++++++++-- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/WebWorkerTest.Http.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/WebWorkerTest.Http.cs index cccc7e5e20aa0..195353b9d8c16 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/WebWorkerTest.Http.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/WebWorkerTest.Http.cs @@ -82,9 +82,22 @@ await Assert.ThrowsAsync(async () => { CancellationTokenSource cts = new CancellationTokenSource(); var promise = response.Content.ReadAsStringAsync(cts.Token); - Console.WriteLine("HttpClient_CancelInDifferentThread: ManagedThreadId: " + Environment.CurrentManagedThreadId + " NativeThreadId: " + WebWorkerTestHelper.NativeThreadId); - cts.Cancel(); - await promise; + try + { + Console.WriteLine("HttpClient_CancelInDifferentThread: ManagedThreadId: " + Environment.CurrentManagedThreadId + " NativeThreadId: " + WebWorkerTestHelper.NativeThreadId); + cts.Cancel(); + await promise; + } + catch (TaskCanceledException ex) + { + Console.WriteLine("HttpClient_CancelInDifferentThread: TaskCanceledException is thrown with message: " + ex.ToString()); + throw; + } + catch (OperationCanceledException ex) + { + Console.WriteLine("HttpClient_CancelInDifferentThread: OperationCanceledException is thrown with message: " + ex.ToString()); + throw; + } }); }); } diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/WebWorkerTest.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/WebWorkerTest.cs index 4c0be502edff9..dfb598419e391 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/WebWorkerTest.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/WebWorkerTest.cs @@ -364,20 +364,20 @@ public async Task ThreadingTimer(Executor executor) await executor.Execute(async () => { TaskCompletionSource tcs = new TaskCompletionSource(); + Console.WriteLine("ThreadingTimer: Start Time: " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " ManagedThreadId: " + Environment.CurrentManagedThreadId + " NativeThreadId: " + WebWorkerTestHelper.NativeThreadId); using var timer = new Timer(_ => { Assert.NotEqual(1, Environment.CurrentManagedThreadId); - Console.WriteLine("ThreadingTimer: ManagedThreadId: " + Environment.CurrentManagedThreadId + " NativeThreadId: " + WebWorkerTestHelper.NativeThreadId); Assert.True(Thread.CurrentThread.IsThreadPoolThread); - tcs.SetResult(); hit = true; + tcs.SetResult(); }, null, 100, Timeout.Infinite); await tcs.Task; }, cts.Token); - Console.WriteLine("ThreadingTimer: ManagedThreadId: " + Environment.CurrentManagedThreadId + " NativeThreadId: " + WebWorkerTestHelper.NativeThreadId); + Console.WriteLine("ThreadingTimer: End Time: " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " ManagedThreadId: " + Environment.CurrentManagedThreadId + " NativeThreadId: " + WebWorkerTestHelper.NativeThreadId); Assert.True(hit); } @@ -453,7 +453,7 @@ await executor.Execute(Task () => { exception = ex; } - Console.WriteLine("WaitAssertsOnJSInteropThreads: ManagedThreadId: " + Environment.CurrentManagedThreadId + " NativeThreadId: " + WebWorkerTestHelper.NativeThreadId); + Console.WriteLine("WaitAssertsOnJSInteropThreads: ExecuterType: " + executor.Type + " ManagedThreadId: " + Environment.CurrentManagedThreadId + " NativeThreadId: " + WebWorkerTestHelper.NativeThreadId); executor.AssertBlockingWait(exception); return Task.CompletedTask; diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/WebWorkerTestBase.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/WebWorkerTestBase.cs index 87f88745377b0..1725ca618749d 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/WebWorkerTestBase.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/WebWorkerTestBase.cs @@ -156,14 +156,20 @@ public class NamedCall using var cts = new CancellationTokenSource(8); try { mr.Wait(cts.Token); - } catch (OperationCanceledException) { /* ignore */ } + } catch (OperationCanceledException) { + Console.WriteLine("ManualResetEventSlim.Wait: OperationCanceledException is ignored " + Environment.CurrentManagedThreadId + " NativeThreadId: " + WebWorkerTestHelper.NativeThreadId); + /* ignore */ + } }}, new NamedCall { Name = "SemaphoreSlim.Wait", Call = delegate (CancellationToken ct) { using var sem = new SemaphoreSlim(2); var cts = new CancellationTokenSource(8); try { sem.Wait(cts.Token); - } catch (OperationCanceledException) { /* ignore */ } + } catch (OperationCanceledException) { + Console.WriteLine("SemaphoreSlim.Wait: OperationCanceledException is ignored " + Environment.CurrentManagedThreadId + " NativeThreadId: " + WebWorkerTestHelper.NativeThreadId); + /* ignore */ + } }}, }; From 52cb799a6089d61187f378d5671d8514c2d61c74 Mon Sep 17 00:00:00 2001 From: Meri Khamoyan Date: Wed, 14 Feb 2024 09:47:20 +0100 Subject: [PATCH 3/5] Move ReadAsStringAsync inside try block --- .../Runtime/InteropServices/JavaScript/WebWorkerTest.Http.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/WebWorkerTest.Http.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/WebWorkerTest.Http.cs index 195353b9d8c16..53d45b99678e9 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/WebWorkerTest.Http.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/WebWorkerTest.Http.cs @@ -81,9 +81,9 @@ await HttpClient_ActionInDifferentThread(url, executor1, executor2, async (HttpR await Assert.ThrowsAsync(async () => { CancellationTokenSource cts = new CancellationTokenSource(); - var promise = response.Content.ReadAsStringAsync(cts.Token); try { + var promise = response.Content.ReadAsStringAsync(cts.Token); Console.WriteLine("HttpClient_CancelInDifferentThread: ManagedThreadId: " + Environment.CurrentManagedThreadId + " NativeThreadId: " + WebWorkerTestHelper.NativeThreadId); cts.Cancel(); await promise; From 039039a5cf9d38ac97bcddddb5d77c7ea7976e04 Mon Sep 17 00:00:00 2001 From: Meri Khamoyan Date: Thu, 22 Feb 2024 10:09:50 +0100 Subject: [PATCH 4/5] Don't swallow OperationCanceledException --- .../JavaScript/WebWorkerTestBase.cs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/WebWorkerTestBase.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/WebWorkerTestBase.cs index 1725ca618749d..3e0132ec19ebf 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/WebWorkerTestBase.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/WebWorkerTestBase.cs @@ -156,19 +156,23 @@ public class NamedCall using var cts = new CancellationTokenSource(8); try { mr.Wait(cts.Token); - } catch (OperationCanceledException) { - Console.WriteLine("ManualResetEventSlim.Wait: OperationCanceledException is ignored " + Environment.CurrentManagedThreadId + " NativeThreadId: " + WebWorkerTestHelper.NativeThreadId); - /* ignore */ - } + } catch (Exception ex) { + if (ex is OperationCanceledException) + { + throw; + } + } }}, new NamedCall { Name = "SemaphoreSlim.Wait", Call = delegate (CancellationToken ct) { using var sem = new SemaphoreSlim(2); var cts = new CancellationTokenSource(8); try { sem.Wait(cts.Token); - } catch (OperationCanceledException) { - Console.WriteLine("SemaphoreSlim.Wait: OperationCanceledException is ignored " + Environment.CurrentManagedThreadId + " NativeThreadId: " + WebWorkerTestHelper.NativeThreadId); - /* ignore */ + } catch (Exception ex) { + if (ex is OperationCanceledException) + { + throw; + } } }}, }; From ff4a396c37029c83791d70660ab7049213a8ccc7 Mon Sep 17 00:00:00 2001 From: Meri Khamoyan Date: Thu, 22 Feb 2024 11:29:01 +0100 Subject: [PATCH 5/5] Revert OperationCanceledException related changes --- .../JavaScript/WebWorkerTestBase.cs | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/WebWorkerTestBase.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/WebWorkerTestBase.cs index 3e0132ec19ebf..87f88745377b0 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/WebWorkerTestBase.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/WebWorkerTestBase.cs @@ -156,24 +156,14 @@ public class NamedCall using var cts = new CancellationTokenSource(8); try { mr.Wait(cts.Token); - } catch (Exception ex) { - if (ex is OperationCanceledException) - { - throw; - } - } + } catch (OperationCanceledException) { /* ignore */ } }}, new NamedCall { Name = "SemaphoreSlim.Wait", Call = delegate (CancellationToken ct) { using var sem = new SemaphoreSlim(2); var cts = new CancellationTokenSource(8); try { sem.Wait(cts.Token); - } catch (Exception ex) { - if (ex is OperationCanceledException) - { - throw; - } - } + } catch (OperationCanceledException) { /* ignore */ } }}, };