-
Thread.Abort is DangerousWe agree and it is dangerous for anyone else to abort the thread one has started without any ownership. So why can't we make Here are some practical use cases.
NodeJS and Chrome does itRunInContext , Consider a web browser, script taking long will be aborted and browser will return an error to user that script has timed out. Whole browser doesn't crash or need to be restarted. How to Sandbox CPU time?Point here is not about sandboxing security or anything else, when we allow some other's code to be executed, I want to put a timeout, and when I am trying to execute it, will be aware of consequences. ASP.NET Had this ability
AbortableThreadAbortableThread can allow registering IDisposable by creator, so creator can register unmanaged resources, locks that will be safely released without ignoring public delegate void AbortableThreadDelegate(ICollection<IDisposable> list, object state);
var thread = new AbortableThread((list, state) => {
// I can safely run a long code...
// register disposables...
});
thread.Start(); When thread exists or aborted, all registered This also leaves all safety issues as it is, we are not allowing all threads to be aborted, only the one who has created the thread can abort it. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 10 replies
-
.Net 7 added |
Beta Was this translation helpful? Give feedback.
-
This is/was true for libraries in the base runtime, at least in the way you're thinking of. Generally the current recommendation for most of your use-cases has been "run it as a separate process", which also means cleanup does happen.
If I provide you truly malicious code, you aborting the thread is going to happen "too late". I have far better ways to mess up your setup than just busy-waiting. |
Beta Was this translation helpful? Give feedback.
-
It has nothing to do with opting into "abortableness", it's that code written in these constructs must always be hardened so that aborts don't corrupt the internal state. .NET has removed most of the code that tries to harden corruption against thread aborts. It's not pay for play. |
Beta Was this translation helpful? Give feedback.
-
The only way to guarantee that arbitrary code behaves nicely - and to have the option of aborting it abruptly under some condition - is to move that code to a secondary process. This is true for Windows and this is true for Linux. (I am disregarding the fact even process isolation would not be sufficient as a security boundary if you were to be considering multi-tenancy, but your original solution was in-proc so that should not be one of your concerns). If your solution involves "run any user-provided code as-a-service", process isolation will be much more robust than anything you could ever achieve in-process, from any possible crash or corruption. Furthermore, offloading to a worker process would allow you to cap resources (Max CPU%, max memory, etc) at the kernel level - using jobobjects if Windows or cgroups if Linux. |
Beta Was this translation helpful? Give feedback.
.Net 7 added
ControlledExecution.Run
, which sounds very close to what you're looking for, except it's not meant for production code.