Skip to content

Fix ThreadAbort at first instruction of try block - #138306

Merged
janvorli merged 1 commit into
dotnet:mainfrom
janvorli:fix-threadabort-at-first-try-instruction
Nov 26, 2026
Merged

janvorli merged 1 commit into
dotnet:mainfrom
janvorli:fix-threadabort-at-first-try-instruction

Conversation

@janvorli

Copy link
Copy Markdown
Member

On Windows, when thread abort is thrown from APC activation callback, the exception handling is started from the ProcessCLRException and we consider the interrupted frame location as a call site and adjust the PC during EH. That prevents thread abort working correctly when the interruption happens at the first instruction of a try block.

This change fixes it by detecting the interrupted frame in the ProcessCLRException and marking it with CONTEXT_EXCEPTION_ACTIVE to indicate that the context represents interrupted location and not a call site.

Close #138208

On Windows, when thread abort is thrown from APC activation callback,
the exception handling is started from the ProcessCLRException and we
consider the interrupted frame location as a call site and adjust the
PC during EH. That prevents thread abort working correctly when the
interruption happens at the first instruction of a try block.

This change fixes it by detecting the interrupted frame in the
ProcessCLRException and marking it with CONTEXT_EXCEPTION_ACTIVE to
indicate that the context represents interrupted location and not a call
site.

Close dotnet#138208
@janvorli janvorli added this to the 12.0.0 milestone Nov 25, 2026
@janvorli
janvorli requested a review from jkotas November 25, 2026 20:30
@janvorli janvorli self-assigned this Nov 25, 2026
@github-actions github-actions Bot added the area-ExceptionHandling-coreclr only use for closed issues label Nov 25, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@janvorli

Copy link
Copy Markdown
Member Author

/backport to release/11.0

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/11.0 (link to workflow run)

@janvorli

Copy link
Copy Markdown
Member Author

/backport to release/10.0

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/10.0 (link to workflow run)

@github-actions

Copy link
Copy Markdown
Contributor

@janvorli backporting to release/11.0 failed, the patch most likely resulted in conflicts. Please backport manually!

git am output
$ git cherry-pick 7b1c31a126e50d8ae5590f6eb4ef85791c8dde68

Auto-merging src/coreclr/vm/exceptionhandling.cpp
Auto-merging src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Runtime/ControlledExecutionTests.cs
CONFLICT (content): Merge conflict in src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Runtime/ControlledExecutionTests.cs
error: could not apply 7b1c31a126e... Fix ThreadAbort at first instruction of try block (#138306)
hint: After resolving the conflicts, mark them with
hint: "git add/rm <pathspec>", then run
hint: "git cherry-pick --continue".
hint: You can instead skip this commit with "git cherry-pick --skip".
hint: To abort and get back to the state before "git cherry-pick",
hint: run "git cherry-pick --abort".
hint: Disable this message with "git config set advice.mergeConflict false"


$ git am --3way --empty=keep --ignore-whitespace --keep-non-patch changes.patch

Applying: Fix ThreadAbort at first instruction of try block
Using index info to reconstruct a base tree...
M	src/coreclr/vm/exceptionhandling.cpp
M	src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Runtime/ControlledExecutionTests.cs
Falling back to patching base and 3-way merge...
Auto-merging src/coreclr/vm/exceptionhandling.cpp
Auto-merging src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Runtime/ControlledExecutionTests.cs
CONFLICT (content): Merge conflict in src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Runtime/ControlledExecutionTests.cs
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Patch failed at 0001 Fix ThreadAbort at first instruction of try block
Error: The process '/usr/bin/git' failed with exit code 128

Link to workflow output

@github-actions

Copy link
Copy Markdown
Contributor

@janvorli backporting to release/10.0 failed, the patch most likely resulted in conflicts. Please backport manually!

git am output
$ git cherry-pick 7b1c31a126e50d8ae5590f6eb4ef85791c8dde68

Auto-merging src/coreclr/vm/exceptionhandling.cpp
CONFLICT (content): Merge conflict in src/coreclr/vm/exceptionhandling.cpp
Auto-merging src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Runtime/ControlledExecutionTests.cs
CONFLICT (content): Merge conflict in src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Runtime/ControlledExecutionTests.cs
error: could not apply 7b1c31a126e... Fix ThreadAbort at first instruction of try block (#138306)
hint: After resolving the conflicts, mark them with
hint: "git add/rm <pathspec>", then run
hint: "git cherry-pick --continue".
hint: You can instead skip this commit with "git cherry-pick --skip".
hint: To abort and get back to the state before "git cherry-pick",
hint: run "git cherry-pick --abort".
hint: Disable this message with "git config set advice.mergeConflict false"


$ git am --3way --empty=keep --ignore-whitespace --keep-non-patch changes.patch

Applying: Fix ThreadAbort at first instruction of try block
Using index info to reconstruct a base tree...
M	src/coreclr/vm/exceptionhandling.cpp
M	src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Runtime/ControlledExecutionTests.cs
Falling back to patching base and 3-way merge...
Auto-merging src/coreclr/vm/exceptionhandling.cpp
CONFLICT (content): Merge conflict in src/coreclr/vm/exceptionhandling.cpp
Auto-merging src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Runtime/ControlledExecutionTests.cs
CONFLICT (content): Merge conflict in src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Runtime/ControlledExecutionTests.cs
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Patch failed at 0001 Fix ThreadAbort at first instruction of try block
Error: The process '/usr/bin/git' failed with exit code 128

Link to workflow output

@jkotas

jkotas commented Nov 26, 2026

Copy link
Copy Markdown
Member

ControlledExecution is best-effort feature for developer tools, it is not meant to be used in production code. I do not think this fix meets the backport bar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-ExceptionHandling-coreclr only use for closed issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ControlledExecution.Run - abort delivered at the first instruction of a try region skips its catch/finally

2 participants