Skip to content
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

Breaking change: Thread.Abort is now obsolete #19625

Closed
GrabYourPitchforks opened this issue Jul 21, 2020 · 0 comments · Fixed by #19867
Closed

Breaking change: Thread.Abort is now obsolete #19625

GrabYourPitchforks opened this issue Jul 21, 2020 · 0 comments · Fixed by #19867
Assignees
Labels
breaking-change Indicates a .NET Core breaking change 🏁 Release: .NET 5 Work items for the .NET 5 release doc-idea Indicates issues that are suggestions for new topics [org][type][category]

Comments

@GrabYourPitchforks
Copy link
Member

Thread.Abort is now obsolete

The Thread.Abort APIs are now obsolete. Code targeting .NET 5.0+ will receive compile-time warnings when these methods are called. If the warning is suppressed and these methods are called, they will throw PlatformNotSupportedException at runtime.

Version introduced

.NET 5.0 RC1

Old behavior

Calls to Thread.Abort would not produce compile-time warnings. However, on all versions of .NET Core, they would throw exceptions at runtime.

void DoAbortThread()
{
    // Below call always throws PlatformNotSupportedException on
    // .NET Core 2.x - 3.x.

    Thread.CurrentThread.Abort();
}

New behavior

Beginning with .NET 5.0, Thread.Abort is marked obsolete as warning. The runtime implementation of the method is unchanged: it continues to throw PlatformNotSupportedException.

void DoAbortThread()
{
    // Below call produces compiler warning SYSLIB0006.

    Thread.CurrentThread.Abort();
}

Only code targeting .NET 5.0 or later will see the compile-time warning. Code targeting earlier versions (like netcoreapp3.1 or netstandard2.0) will not see a compile-time warning.

Reason for change

Thread.Abort always throws PlatformNotSupportedException on all .NET Core and .NET 5.0+ runtimes. We annotated the API with [Obsolete] to help draw attention to this so that developers can remove these call sites from their own code bases.

Recommended action

If you are trying to abort processing a unit of work, consider using CancellationToken, as demonstrated below.

/*
 * Don't do this.
 */

void ProcessPendingWorkItemsOld()
{
    if (QueryIsMoreWorkPending())
    {
        if (QueryTimeoutReached())
        {
            // line below produces warning SYSLIB0006
            Thread.CurrentThread.Abort();
        }

        WorkItem work = DequeueWorkItem();
        ProcessWorkItem(work);
    }
}

/*
 * Do this instead.
 */

void ProcessPendingWorkItemsNew(CancellationToken cancellationToken)
{
    if (QueryIsMoreWorkPending())
    {
        // If the CancellationToken is marked as "needs to cancel",
        // this will throw the appropriate exception.
        cancellationToken.ThrowIfCancellationRequested();

        WorkItem work = DequeueWorkItem();
        ProcessWorkItem(work);
    }
}

For more information on using CancellationToken to support graceful cancellation, see the document "Cancellation in Managed Threads".

If you need to suppress this warning in code, you can do so by suppressing the warning code SYSLIB0006. This warning code is specific to Thread.Abort and will not suppress other obsoletion warnings in your code. We recommend removing such call sites from your own code.

The below samples only suppress the compile-time warning. They do not change the runtime implementation. Thread.Abort will still throw PlatformNotSupportedException at runtime.

void MyMethod()
{
#pragma warning disable SYSLIB0006 // suppress Thread.Abort compiler warnings
    Thread.CurrentThread.Abort();
#pragma warning restore SYSLIB0006
}

This can also be suppressed project-wide via the .csproj.

<PropertyGroup>
  <OutputType>Exe</OutputType>
  <TargetFramework>net5.0</TargetFramework>
  <!-- Disable "Thread.Abort is obsolete" warnings for entire project -->
  <NoWarn>$(NoWarn);SYSLIB0006</NoWarn>
</PropertyGroup>

Category

  • Core .NET libraries

Affected APIs


Issue metadata

  • Issue type: breaking-change
@GrabYourPitchforks GrabYourPitchforks added breaking-change Indicates a .NET Core breaking change 🏁 Release: .NET 5 Work items for the .NET 5 release labels Jul 21, 2020
@dotnet-bot dotnet-bot added the ⌚ Not Triaged Not triaged label Jul 21, 2020
@gewarren gewarren self-assigned this Jul 23, 2020
@gewarren gewarren added doc-idea Indicates issues that are suggestions for new topics [org][type][category] and removed ⌚ Not Triaged Not triaged labels Jul 23, 2020
gewarren added a commit to gewarren/docs that referenced this issue Aug 6, 2020
gewarren added a commit that referenced this issue Sep 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking-change Indicates a .NET Core breaking change 🏁 Release: .NET 5 Work items for the .NET 5 release doc-idea Indicates issues that are suggestions for new topics [org][type][category]
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants