Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Terminate a hanging thread

, 25 Dec 2011
Rate this:
Please Sign up or sign in to vote.
The right thing to do in this case is attach a debugger to determine the underlying cause of the the deadlock. This solution ignores the fact that the thread being terminated might have an exclusive lock on other critical resources (e.g., the OS loader lock or any application defined...
The right thing to do in this case is attach a debugger to determine the underlying cause of the the deadlock.

This solution ignores the fact that the thread being terminated might have an exclusive lock on other critical resources (e.g., the OS loader lock or any application defined synchronization object). For this reason, terminating a thread can work by happy accident in a lot of cases, but in general terminating a thread is inherently unsafe on the Windows platform.

For example, I encountered a deadlock that occurred due to calling WinVerifyTrust() from DllMain(). An undocumented feature of WinVerifyTrust() is that it can spawn a worker thread, which requires the OS loader lock ... and the OS loader lock was already acquired by the OS loader before the call to DllMain(). That's a classic deadlock. If we terminated the thread, then the underlying call would simply fail, which would ignore the underlying problem (illegal call from DllMain).

I have also encountered deadlocks in interprocess RPC where the lock order of various synchronization objects was random between different processes and threads, which can result in more complex deadlocks. Again, terminating the thread would simply cause the RPC to fail, and not address the real problem (undefined lock order).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

john_meade
Web Developer
United States United States
No Biography provided

Comments and Discussions

 
GeneralRtlFreeUserThreadStack is also available in Server 2003 (NT ... PinmemberN1CKLAD4-Jan-12 16:35 
GeneralThe return type for RtlFreeUserThreadStack is NTSTATUS and n... PinmemberN1CKLAD4-Jan-12 14:28 
GeneralThe return type for RtlFreeUserThreadStack is NTSTATUS and n... PinmemberN1CKLAD4-Jan-12 14:01 
GeneralI half-agree with your statements... fixing a deadlock is gr... PinmemberAlbert Holguin2-Dec-11 7:56 
GeneralHi John, this is not a recommendation to use TerminateThread... Pinmembertrotwa28-Nov-11 13:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 25 Dec 2011
Article Copyright 2011 by john_meade
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid