Repository navigation
[Interpreter] Raise a pending thread abort again after a catch handler #135520
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+208
−7
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
44f42b4
Raise a pending thread abort again after an interpreted catch handler
pcshrosbree 08e3dfb
Build test135463 at priority 1
pcshrosbree 35f8910
Check for a pending thread abort on the resume path only
pcshrosbree 7e70fe0
Observe the code after the catch in test135463 with a call-free store
pcshrosbree 0125aaa
Gate test135463 with ConditionalFact instead of project properties
pcshrosbree File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
src/tests/Regressions/coreclr/GitHub_135463/test135463.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Runtime; | ||
| using System.Runtime.CompilerServices; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using TestLibrary; | ||
| using Xunit; | ||
|
|
||
| // ControlledExecution.Run is the API that aborts a thread | ||
| #pragma warning disable SYSLIB0046 | ||
|
|
||
| public class Runtime_135463 | ||
| { | ||
| private static volatile bool s_readyForCancellation; | ||
| private static volatile int s_counter; | ||
| private static string s_trace; | ||
| // Set by a call-free store right after a catch while an abort is pending. A call there (like the string | ||
| // concatenation of s_trace) would carry its own safepoint, where the pending abort can be raised even if | ||
| // nothing raised it again at the end of the catch. | ||
| private static volatile bool s_reachedAfterCatch; | ||
|
|
||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| private static void RunInfiniteLoop() | ||
| { | ||
| while (true) | ||
| { | ||
| if ((++s_counter & 0xfffff) == 0) | ||
| { | ||
| Thread.Sleep(0); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static void WaitUntilAbortIsRequested() | ||
| { | ||
| while ((Thread.CurrentThread.ThreadState & ThreadState.AbortRequested) == 0) | ||
| { | ||
| Thread.Sleep(10); | ||
| } | ||
| } | ||
|
|
||
| // The abort is thrown again at the end of the catch block that swallowed it | ||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| private static void CatchAbort() | ||
| { | ||
| try | ||
| { | ||
| s_readyForCancellation = true; | ||
| RunInfiniteLoop(); | ||
| } | ||
| catch (ThreadAbortException) | ||
| { | ||
| s_trace += "catch "; | ||
| } | ||
|
|
||
| s_reachedAfterCatch = true; | ||
| } | ||
|
|
||
| // The caller catches the abort that was thrown again at the end of the catch block in its callee | ||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| private static void CatchAbortInCaller() | ||
| { | ||
| try | ||
| { | ||
| CatchAbort(); | ||
| } | ||
| catch (ThreadAbortException) | ||
| { | ||
| s_trace += "caller catch "; | ||
| } | ||
|
|
||
| s_reachedAfterCatch = true; | ||
| } | ||
|
|
||
| // A thread is not aborted while it runs a finally block, so the abort stays pending at the end of a catch block | ||
| // that is inside of one | ||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| private static void CatchInsideOfFinally() | ||
| { | ||
| try | ||
| { | ||
| try | ||
| { | ||
| throw new InvalidOperationException(); | ||
| } | ||
| finally | ||
| { | ||
| s_readyForCancellation = true; | ||
| WaitUntilAbortIsRequested(); | ||
|
|
||
| try | ||
| { | ||
| throw new ArgumentException(); | ||
| } | ||
| catch (ArgumentException) | ||
| { | ||
| s_trace += "catch in finally "; | ||
| } | ||
|
|
||
| s_trace += "end of finally "; | ||
| } | ||
| } | ||
| catch (InvalidOperationException) | ||
| { | ||
| s_trace += "outer catch "; | ||
| } | ||
|
|
||
| s_reachedAfterCatch = true; | ||
| } | ||
|
|
||
| private static void CancelWhenReady(CancellationTokenSource cts) | ||
| { | ||
| while (!s_readyForCancellation) | ||
| { | ||
| Thread.Sleep(10); | ||
| } | ||
|
|
||
| cts.Cancel(); | ||
| } | ||
|
|
||
| // Runs the action, aborts it once it is ready and checks which of its blocks ran | ||
| private static void RunAndAbort(string name, Action action, string expectedTrace) | ||
| { | ||
| Console.WriteLine(name); | ||
| s_trace = ""; | ||
| s_readyForCancellation = false; | ||
| s_reachedAfterCatch = false; | ||
|
|
||
| var cts = new CancellationTokenSource(); | ||
| Task.Run(() => CancelWhenReady(cts)); | ||
|
|
||
| Exception exception = null; | ||
| try | ||
| { | ||
| ControlledExecution.Run(action, cts.Token); | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| exception = e; | ||
| } | ||
|
|
||
| Assert.IsType<OperationCanceledException>(exception); | ||
| Assert.Equal(expectedTrace, s_trace); | ||
| Assert.False(s_reachedAfterCatch); | ||
| } | ||
|
|
||
| // ControlledExecution is supported on CoreCLR only | ||
| [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsCoreCLR), nameof(PlatformDetection.IsMultithreadingSupported))] | ||
| public static void TestEntryPoint() | ||
| { | ||
| RunAndAbort(nameof(CatchAbort), CatchAbort, "catch "); | ||
| RunAndAbort(nameof(CatchAbortInCaller), CatchAbortInCaller, "catch caller catch "); | ||
| RunAndAbort(nameof(CatchInsideOfFinally), CatchInsideOfFinally, "catch in finally end of finally outer catch "); | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
src/tests/Regressions/coreclr/GitHub_135463/test135463.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <CLRTestPriority>1</CLRTestPriority> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <Compile Include="test135463.cs" /> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <ProjectReference Include="$(TestLibraryProjectPath)" /> | ||
| </ItemGroup> | ||
| </Project> | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.