views:

109

answers:

5

I have a thread in my application that is running code that can potentially cause call stack corruption ( my application is a testing tool for dlls ).

Assuming that I have a method of detecting if the child thread is misbehaving, how would I terminate it? From what I read, calling Thread.Abort() on the misbehaving thread would be equivalent to raising an exception inside it.I fear that that not be a good idea, provided the call stack of the thread might be corrupted.Any suggestions?

+3  A: 

If code is misbehaving, it can do anything, and it can affect anything in the entire process, on any thread.

The most reliable solution is to run the untrusted code in a separate process, then terminate the process if it misbehaves.

SLaks
killing always do. Running it on Your own thread would be just risky, and unreliable, thanks to possible untrusted finalizers which can do plainly ANYTHING...
luckyluke
+6  A: 

If you are running untrusted code that could corrupt your process run that code in a separate process and communicate with it using interprocess communication. If you want to terminate the untrusted code early, you can just kill the process.

Mark Byers
Suddenly I have that Monzy song "Kill dash nine" stuck in my head.
Jason Down
http://www.monzy.com/intro/killdashnine_lyrics.html
Jason Down
+2  A: 

This article might interest you, it is from Eric Lippert himself: Careful with that axe, should I specify timeout?

Will Marcouiller
+3  A: 

Load the DLL into a new AppDomain and run the code in the DLL from there using the AppDomain.DoCallBack method.

John Saunders
A: 

In addition to the accept answer, I'd like to add that it is trivially easy to ignore Thread.Abort.

try
{
    ...
}
catch (ThreadAbortException)
{
   Thread.ResetAbort();
}
Robert Davis