The code for the deadlock example has a bug, the threads initiated are running the same function:
t1 = Thread(target=thread_one, args=(lock1, lock2))
t2 = Thread(target=thread_one, args=(lock1, lock2))
The reason there is an execution timeout is because the first thread which acquires the lock never releases it for the second thread to acquire. This is in opposition to the reason stated:
The above example demonstrates that a thread can’t release a lock it has not locked. Furthermore, trying to release an unacquired lock will result in an exception.
When fixed by having the second thread run the thread_two
function, there are no errors and no deadlock.