From d525d7bb8179244d02d453e5f87d0ef39b761000 Mon Sep 17 00:00:00 2001 From: Jakub Jares Date: Thu, 16 Apr 2026 13:47:47 +0200 Subject: [PATCH 1/2] Catch InvalidOperationException on mutex cleanup for macOS/Linux AssemblyCleanup may run on a different thread than AssemblyInitialize. On macOS/Linux the runtime throws InvalidOperationException (not ApplicationException) when ReleaseMutex is called from a non-owning thread. Widen the catch to handle both platforms. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../IntegrationTestBuild.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/Microsoft.TestPlatform.TestUtilities/IntegrationTestBuild.cs b/test/Microsoft.TestPlatform.TestUtilities/IntegrationTestBuild.cs index f18ecf7f95..1c15a66277 100644 --- a/test/Microsoft.TestPlatform.TestUtilities/IntegrationTestBuild.cs +++ b/test/Microsoft.TestPlatform.TestUtilities/IntegrationTestBuild.cs @@ -179,7 +179,10 @@ public static void CleanupTestAssets() { if (s_isSessionMutexOwner) { - try { s_sessionMutex.ReleaseMutex(); } catch (ApplicationException) { } + // AssemblyCleanup may run on a different thread than AssemblyInitialize. + // On macOS/Linux the runtime throws InvalidOperationException for + // cross-thread release; on Windows it throws ApplicationException. + try { s_sessionMutex.ReleaseMutex(); } catch (Exception) { } } s_sessionMutex.Dispose(); s_sessionMutex = null; From 9c5ae6d1f5b55a866502a7af8b8eae34392f5646 Mon Sep 17 00:00:00 2001 From: Jakub Jares Date: Thu, 16 Apr 2026 13:51:53 +0200 Subject: [PATCH 2/2] Narrow catch to only ApplicationException and InvalidOperationException Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../IntegrationTestBuild.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Microsoft.TestPlatform.TestUtilities/IntegrationTestBuild.cs b/test/Microsoft.TestPlatform.TestUtilities/IntegrationTestBuild.cs index 1c15a66277..8fe9668a80 100644 --- a/test/Microsoft.TestPlatform.TestUtilities/IntegrationTestBuild.cs +++ b/test/Microsoft.TestPlatform.TestUtilities/IntegrationTestBuild.cs @@ -182,7 +182,7 @@ public static void CleanupTestAssets() // AssemblyCleanup may run on a different thread than AssemblyInitialize. // On macOS/Linux the runtime throws InvalidOperationException for // cross-thread release; on Windows it throws ApplicationException. - try { s_sessionMutex.ReleaseMutex(); } catch (Exception) { } + try { s_sessionMutex.ReleaseMutex(); } catch (Exception ex) when (ex is ApplicationException or InvalidOperationException) { } } s_sessionMutex.Dispose(); s_sessionMutex = null;