Re: How do you kill a completely locked up thread?
- From: "Ben Voigt [C++ MVP]" <rbv@xxxxxxxxxxxxx>
- Date: Fri, 18 Jan 2008 07:10:18 -0600
"Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx> wrote in message
news:op.t43lhtvz8jd0ej@xxxxxxxxxxxxxxxxxxxxxxx
On Thu, 17 Jan 2008 14:58:00 -0800, TheSilverHammer
<TheSilverHammer@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
If they can do it with processes, why can't they do it with threads?
Can do what with processes? We've already explained that you can't safely
terminate a process any more than you can safely abort a thread.
Sure you can. Ok, maybe not an arbitrary process, but it's fairly easy
(depending on what resources are required by your requirements) to design a
process that can be terminated at any point in time. It's even easier to
manage exiting your own process, even with hung threads. Theoretically you
can also create a thread that can be safely terminated, but... not with
..NET. .NET holds internal state and accesses it willy-nilly from any
threads in a way that's threadsafe but not abort safe. However, .NET
doesn't implement any external state on its own, only what you ask it to, so
you can manage your external resources in such a way that it's ok for the
process to be interrupted (for example, instead of writing data files that
could be left inconsistent, store your data in an ACID database using
transactions).
I am sure they can't guarantee that everything will be fine if my code
doesn't anticipate a resources disappearing, but if I do, I should be
able to
do it safely.
It's not an issue of resources "disappearing". It's an issue of them
being left in an inconsistent state.
There is no way for the _operating system_ to ensure that things are left
in an inconsistent state. Implementors of various data structures can do
things to make sure they are always in a consistent state (e.g. see
"journaled" or "transaction-based"), but that's up to the implementor.
The OS has no way to do this (though it might provide APIs to help an
implementor do it).
Yup, and the problem is that the .NET implementation uses hidden
process-local resources without doing any of this, so no matter what code
you tag on top, calling TerminateThread is gonna crash the process.
For example:
I have a MyThread and then I have the thread procedure which opens a
bunch
of files, sockets, and all that. If MyThread is killed, the OS can
recover
all that stuff.
No, it can't. All data within a process is owned by the _process_, unless
it's been specifically marked as thread data (*). The OS has no way to
know whether killing a thread allows that data to be cleaned up or not.
(*) (I'm not sure .NET supports this or not, but is supported in the
unmanaged Windows API...I'm seeing a Thread.AllocateDataSlot() method, and
I suspect this addresses the same issue in managed code. In any case,
note that it only addresses specific thread-local storage, not the OS
objects that might be referenced by that storage, as those are still
per-process and cannot be released with the thread terminates).
But even if it did have a way to know what data could be cleaned up,
_that's not the problem_. Cleaning things up is the least of the
worries. It's the fact that software _does_ stuff, and if it's
interrupted in the middle of _doing_ that stuff, whatever data the
software is operating on could be in an inconsistent state.
Most of your rant seems to be about this question of cleaning up, but
that's not the main problem. That's not what makes killing threads or
processes unsafe, and coming up with a paradigm in which you can ensure
things are cleaned up would _not_ make killing threads or processes a safe
operation.
One reasonable approach, as long as this SharpSSH library doesn't use any
external resources except sockets, would be to put that component in a
separate process, communicate back and forth with Remoting, and provide at
least one call that causes said process to free any shared resources and
then call ExitProcess (.NET Application.Exit?) to free the hung thread(s).
.
- Follow-Ups:
- Re: How do you kill a completely locked up thread?
- From: Willy Denoyette [MVP]
- Re: How do you kill a completely locked up thread?
- References:
- Re: How do you kill a completly locked up thread?
- From: Jeroen Mostert
- Re: How do you kill a completely locked up thread?
- From: TheSilverHammer
- Re: How do you kill a completely locked up thread?
- From: Jeroen Mostert
- Re: How do you kill a completely locked up thread?
- From: Jeroen Mostert
- Re: How do you kill a completely locked up thread?
- From: TheSilverHammer
- Re: How do you kill a completely locked up thread?
- From: Peter Duniho
- Re: How do you kill a completly locked up thread?
- Prev by Date: Re: My silliest extention made yet 2008...
- Next by Date: Re: Aspect Oriented Programming
- Previous by thread: Re: How do you kill a completely locked up thread?
- Next by thread: Re: How do you kill a completely locked up thread?
- Index(es):
Relevant Pages
|