Skills thread-abort-migration
git clone https://github.com/dotnet/skills
T=$(mktemp -d) && git clone --depth=1 https://github.com/dotnet/skills "$T" && mkdir -p ~/.claude/skills && cp -r "$T/plugins/dotnet-upgrade/skills/thread-abort-migration" ~/.claude/skills/dotnet-skills-thread-abort-migration && rm -rf "$T"
plugins/dotnet-upgrade/skills/thread-abort-migration/SKILL.mdThread.Abort Migration
This skill helps an agent migrate .NET Framework code that uses
Thread.Abort to the cooperative cancellation model required by modern .NET (6+). Thread.Abort throws PlatformNotSupportedException in modern .NET — there is no way to forcibly terminate a managed thread. The skill identifies the usage pattern first, then applies the correct replacement strategy.
When to Use
- Migrating a .NET Framework project to .NET 6+ that calls
Thread.Abort - Replacing
catch blocks that use control flow or cleanup logicThreadAbortException - Removing
calls that cancel pending abortsThread.ResetAbort - Replacing
for waking blocked threadsThread.Interrupt - Migrating ASP.NET code that uses
orResponse.End
, which internally callResponse.Redirect(url, true)Thread.Abort - Resolving
orPlatformNotSupportedException
warnings after a target framework changeSYSLIB0006
When Not to Use
- The code only uses
,Thread.Join
, orThread.Sleep
without any abort, interrupt, orThread.Start
catch blocks. These APIs work identically in modern .NET — no migration is needed. Stop here and tell the user no migration is required. If you suggest modernization (e.g.,ThreadAbortException
,Task.Run
), you must explicitly state these are optional improvements unrelated to Thread.Abort migration, and the existing code will compile and run correctly as-is on the target framework.Parallel.ForEach - The project will remain on .NET Framework indefinitely
- The Thread.Abort usage is inside a third-party library you do not control
Inputs
| Input | Required | Description |
|---|---|---|
| Source project or solution | Yes | The .NET Framework project containing Thread.Abort usage |
| Target framework | Yes | The modern .NET version to target (e.g., ) |
| Thread.Abort usage locations | Recommended | Files or classes that reference , , , or |
Workflow
Commit strategy: Commit after each pattern replacement so the migration is reviewable and bisectable. Group related call sites (e.g., all cancellable work loops) into one commit.
Step 1: Inventory all thread termination usage
Search the codebase for all thread-termination-related APIs:
andThread.Abort
(instance calls)thread.Abort()
in catch blocksThreadAbortExceptionThread.ResetAbortThread.Interrupt
(calls Thread.Abort internally in ASP.NET Framework)Response.End()
(theResponse.Redirect(url, true)
parameter triggers Thread.Abort)true
pragma suppressionsSYSLIB0006
Record each usage location and classify the intent behind the abort.
Step 2: Classify each usage pattern
Categorize every usage into one of the following patterns:
| Pattern | Description | Modern replacement |
|---|---|---|
| Cancellable work loop | Thread running a loop that should stop on demand | checked in the loop |
| Timeout enforcement | Aborting a thread that exceeds a time limit | or with a delay |
| Blocking call interruption | Thread blocked on , , or that needs to wake up | with , or async alternatives |
| ASP.NET request termination | or | Return from the action method; use |
| ThreadAbortException as control flow | Catch blocks that inspect to decide cleanup actions | Catch instead, with explicit cleanup |
| Thread.ResetAbort to continue execution | Catching the abort and calling to keep the thread alive | Check and decide whether to continue |
| Uncooperative code termination | Killing a thread running code that cannot be modified to check for cancellation | Move the work to a separate process and use |
Critical: The fundamental paradigm shift is from preemptive cancellation (the runtime forcibly injects an exception) to cooperative cancellation (the code must voluntarily check for and respond to cancellation requests). Every call site must be evaluated for whether the target code can be modified to cooperate.
Step 3: Apply the replacement for each pattern
- Cancellable work loop: Add a
parameter. Replace the loop condition or addCancellationToken
at safe checkpoints. The caller creates atoken.ThrowIfCancellationRequested()
and callsCancellationTokenSource
instead ofCancel()
.Thread.Abort() - Timeout enforcement: Use
ornew CancellationTokenSource(TimeSpan.FromSeconds(n))
. Pass the token to the work. For task-based code, usects.CancelAfter(timeout)
and cancel the source if the delay wins; cancelling also disposes the delay's internal timer.Task.WhenAny(workTask, Task.Delay(timeout, cts.Token)) - Blocking call interruption: Replace
withThread.Sleep(ms)
orTask.Delay(ms, token)
. Replacetoken.WaitHandle.WaitOne(ms)
withManualResetEvent.WaitOne()
.WaitHandle.WaitAny(new[] { event, token.WaitHandle }) - ASP.NET request termination: Remove
entirely — just return from the method. ReplaceResponse.End()
withResponse.Redirect(url, true)
(without theResponse.Redirect(url)
endResponse parameter) or return a redirect result. In ASP.NET Core, usetrue
as the cancellation token for long-running request work.HttpContext.RequestAborted - ThreadAbortException as control flow: Replace
withcatch (ThreadAbortException)
. Move cleanup logic tocatch (OperationCanceledException)
blocks orfinally
callbacks. Do not catchCancellationToken.Register
and swallow it — let it propagate unless you have a specific recovery action.OperationCanceledException - Thread.ResetAbort to continue execution: Break up "abortable" units of work so that cancellation in a processing loop can continue to the next unit instead of relying on
to prevent tearing down the thread. CheckResetAbort
after each unit and decide whether to continue. Create a newtoken.IsCancellationRequested
(optionally linked to a parent token) for each new unit of work rather than trying to reset an existing one.CancellationTokenSource - Uncooperative code termination: If the code cannot be modified to accept a
(e.g., third-party library, native call), move the work to a child process. The host process communicates via stdin/stdout or IPC and callsCancellationToken
if a timeout expires.Process.Kill
Step 4: Clean up removed APIs
After migrating all patterns, remove or replace any remaining references:
| Removed API | Replacement |
|---|---|
| |
catch blocks | catch blocks |
| Check and decide whether to continue |
| Signal via or set a (also obsolete: in .NET 9) |
| Remove the call; return from the method |
| without endResponse, or return a redirect result |
| Remove after replacing the Thread.Abort call |
Step 5: Verify the migration
- Build the project targeting the new framework. Confirm zero
warnings and noSYSLIB0006
-related compile errors.Thread.Abort - Search the codebase for any remaining references to
,Thread.Abort
,ThreadAbortException
, orThread.ResetAbort
.Thread.Interrupt - Run existing tests. If tests relied on
for cleanup or timeout, update them to useThread.Abort
.CancellationToken - For timeout scenarios, verify that work actually stops within a reasonable time after cancellation is requested.
- For blocking call scenarios, verify that blocked threads wake up promptly when the token is cancelled.
Validation
- No references to
remain in the migrated codeThread.Abort - No
catch blocks remainThreadAbortException - No
calls remainThread.ResetAbort - No
pragma suppressions remainSYSLIB0006 - Project builds cleanly against the target framework with no thread-abort-related warnings
- All cancellable work accepts a
parameterCancellationToken - Timeout scenarios use
or equivalentCancellationTokenSource.CancelAfter - Blocking calls use
withWaitHandle.WaitAny
or async alternativestoken.WaitHandle - Existing tests pass or have been updated for cooperative cancellation
Common Pitfalls
| Pitfall | Solution |
|---|---|
Adding parameter but never checking it in long-running code | Insert at regular checkpoints in loops and between expensive operations. Cancellation only works if the code cooperates. |
| Not passing the token through the full call chain | Every async or long-running method in the chain must accept and forward the . If one method in the chain ignores it, cancellation stalls at that point. |
Expecting to interrupt blocking synchronous calls like or | These calls do not check the token. Replace with . Replace synchronous I/O with async overloads that accept a . |
Catching and swallowing it | Let propagate to the caller. Only catch it at the top-level orchestration point where you decide what to do after cancellation (log, clean up, return a result). |
Not disposing | is . Wrap it in a statement or dispose it in a block. Leaking it causes timer and callback leaks. |
| Assuming cancellation is immediate | Cooperative cancellation only takes effect at the next checkpoint. If work items are large or the code has long gaps between checks, cancellation may be delayed. Design checkpoint frequency based on acceptable latency. |
Using as a substitute for | is also not recommended in modern .NET. It only works on threads in state and throws , which is a different exception type. Replace with signaling. |
Removing catch blocks without migrating the cleanup logic | catch blocks often contained critical cleanup (releasing locks, rolling back transactions). Move this logic to blocks or callbacks before removing the catch. |
More Info
- Thread.Abort breaking change in .NET 6+ — why
throwsThread.AbortPlatformNotSupportedException - Cancellation in managed threads — the cooperative cancellation model with
CancellationToken - CancellationTokenSource class — API reference for creating and managing cancellation tokens
- SYSLIB0006 warning —
is obsoleteThread.Abort - SYSLIB0046 warning —
is obsolete (added in .NET 9)Thread.Interrupt
— cancellable waiting for task-based code (.NET 6+)Task.WaitAsync(CancellationToken)