How to resolve thread deadlocks in real-time?
<p>If a deadlock between python threads is suspected in run-time, is there any way to resolve it without killing the entire process?</p>

<p>For example, if a few threads take far longer than they should, a resource manager might suspect that some of them are deadlocked. While of course it should be debugged fixed in the code in the future, is there a clean solution that can be used immediately (in run-time) to perhaps kill specific threads so that the others can resume?</p>

<p>Edit: I was thinking to add some "deadlock detection" loop (in its own thread) that sleeps for a bit, then checks all running threads, and if a few of them look suspiciously slow, it kills the least important one among them. At what point the thread is suspected of deadlocking, and which is the least important of them, is of course defined by the deadlock detection loop programmer.</p>

<p>Clearly, it won't catch all problems (most obviously if the deadlock detection thread itself is deadlocked). The idea is not to find a mathematically perfect solution (which is, of course, not to write code that can deadlock). Rather, I wanted to partially solve the problem in some realistic cases.</p>

<p>You may try using <a href="http://code.activestate.com/recipes/577334-how-to-debug-deadlocked-multi-threaded-programs/" rel="nofollow">this</a> code snippet ahead of time but during execution the program is stuck and you can't do much about it.</p>

<p>Debuggers like WinDbg or strace might help but as Python is an interpreted language I doubt they'll be realistic to use.</p>

python python-3.x python-multithreading