Since the upgrade to .NET Core, the application occasionally closes without warning when I click the "Cancel" button while many threads are running. No error message is displayed, so I pulled the source and reproduced locally while running under the Visual Studio debugger, and got this PlatformNotSupportedException
:
Thread abort is not supported on this platform.
At this call stack:
at System.Threading.Thread.Abort()
at SQLQueryStress.LoadEngine.ThreadKiller.KillEm() in C:\Code\SqlQueryStress\src\SQLQueryStress\LoadEngine.cs:line 610
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
The problem is at this line of code, and the reason is documented here:
This method is obsolete. On .NET 5.0 and later versions, calling this method produces a compile-time warning. This method throws a PlatformNotSupportedException at run time on .NET 5.0 and later and .NET Core.
I'd be interested in contributing a fix, but wanted to open the issue before starting, to discuss possible solutions (don't want to overstep my bounds!).
The docs on How to Stop a Thread indicate that CancellationToken
s should be used to cooperatively cancel threads.
However, reading through the source code, I wonder if the Thread.Abort
could just be removed altogether. It looks like you have cooperative cancellation built in, and this Thread.Abort
is kind of a "last resort" if threads aren't ending quickly enough.
Removing Thread.Abort
fixes the problem in my case - all of the threads eventually successfully cancel, and control returns to the UI
Like I said, I'm glad to contribute a fix - but wanted to make sure there's not some context I'm missing, as I'm very new to the codebase 🙂
My repro scenario involve spinning up 200 threads of a query that will all be blocked indefinitely. Once they have all successfully started (I can see all 200 connections on the SQL Server side), I click cancel. If you'd all like the details, let me know!
By the way, thanks for maintaining this excellent tool - it has been a huge help to me as I learn things about SQL Server.