Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I terminate a .NET thread that has had its call stack corrupted?

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?

like image 239
Emil D Avatar asked Mar 15 '10 19:03

Emil D


People also ask

Can you catch stack overflow C#?

A stack overflow is a fatal error and can't be recovered from.

When can you catch a stack overflow exception?

Remarks. StackOverflowException is thrown for execution stack overflow errors, typically in case of a very deep or unbounded recursion. So make sure your code doesn't have an infinite loop or infinite recursion.

What is Stackoverflow C#?

A stack overflow is a type of buffer overflow error that occurs when a computer program tries to use more memory space in the call stack than has been allocated to that stack.


2 Answers

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.

like image 67
Mark Byers Avatar answered Nov 04 '22 07:11

Mark Byers


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.

like image 26
SLaks Avatar answered Nov 04 '22 09:11

SLaks