From b7b24f818b61e96abe64b68d037e3f4962a15ab4 Mon Sep 17 00:00:00 2001 From: nohwnd Date: Fri, 12 Feb 2021 12:18:35 +0100 Subject: [PATCH] Don't swallow stacktrace from adapter exception when running in thread --- .../net451/System/PlatformThread.cs | 4 +++- .../netcore/System/PlatformThread.cs | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.TestPlatform.PlatformAbstractions/net451/System/PlatformThread.cs b/src/Microsoft.TestPlatform.PlatformAbstractions/net451/System/PlatformThread.cs index 6b6f0537fe..f03199043a 100644 --- a/src/Microsoft.TestPlatform.PlatformAbstractions/net451/System/PlatformThread.cs +++ b/src/Microsoft.TestPlatform.PlatformAbstractions/net451/System/PlatformThread.cs @@ -6,6 +6,7 @@ namespace Microsoft.VisualStudio.TestPlatform.PlatformAbstractions { using System; + using System.Runtime.ExceptionServices; using System.Threading; using Microsoft.VisualStudio.TestPlatform.PlatformAbstractions.Interfaces; @@ -42,7 +43,8 @@ public void Run(Action action, PlatformApartmentState apartmentState, bool waitF thread.Join(); if (exThrown != null) { - throw exThrown; + // Preserve the stacktrace when re-throwing the exception. + ExceptionDispatchInfo.Capture(exThrown).Throw(); } } } diff --git a/src/Microsoft.TestPlatform.PlatformAbstractions/netcore/System/PlatformThread.cs b/src/Microsoft.TestPlatform.PlatformAbstractions/netcore/System/PlatformThread.cs index 671f87fff1..69e0416896 100644 --- a/src/Microsoft.TestPlatform.PlatformAbstractions/netcore/System/PlatformThread.cs +++ b/src/Microsoft.TestPlatform.PlatformAbstractions/netcore/System/PlatformThread.cs @@ -6,6 +6,7 @@ namespace Microsoft.VisualStudio.TestPlatform.PlatformAbstractions { using System; + using System.Runtime.ExceptionServices; using System.Threading; using Microsoft.VisualStudio.TestPlatform.PlatformAbstractions.Interfaces; @@ -45,7 +46,8 @@ public void Run(Action action, PlatformApartmentState apartmentState, bool waitF thread.Join(); if (exThrown != null) { - throw exThrown; + // Preserve the stacktrace when re-throwing the exception. + ExceptionDispatchInfo.Capture(exThrown).Throw(); } } }