In .NET when Aborting Thread, can this piece of code get corrupted?

Posted by bosko on Stack Overflow See other posts from Stack Overflow or by bosko
Published on 2010-04-15T14:35:01Z Indexed on 2010/04/19 10:33 UTC
Read the original article Hit count: 337

Little intro:

In complex multithreaded aplication (enterprise service bus EBS), I need to use Thread.Abort, because this EBS accepts user written modules which communicates with hardware security modules. So if this module gets deadlocked or hardware stops responding - i need to just unload this module and rest of this server aplication must keep runnnig.

So there is abort sync mechanism which ensures that code can be aborted only in user section and this section must be marked as AbortAble. If this happen there is possibility that ThreadAbortException will be thrown in this pieace of code:


public void StopAbortSection()
        {
            var id = Thread.CurrentThread.ManagedThreadId;
            lock (threadIdMap[id])
            {
                ....
            }
        }

If module is on AbortSection and Aplication decides to abort module, but after this decision but before actual Thread.Abort, module enters NonAbortableSection by calling this method, but lock is actualy taken on that locking object.

So lock will block until Abort or abort can be executed before reaching this block by this code. But Object with this method is essential and i need to be sure that this pieace of code is safe to abort in any moment.

Probably i have to mention that threadIdMap is Dictionary(int,ManualResetEvent), so locking object is instance of ManualResetEvent.

I hope you now understad my question. Sorry for its largeness.

© Stack Overflow or respective owner

Related posts about .NET

Related posts about thread