Re: simple threading.Thread and Semaphores hang



lnenov <lnenov@xxxxxxxxxx> writes:

My application hangs on exit.
I have isoleted this piece of code that reproduces the error: (the
time module is extra and not needed to reproduce)

import threading
import time

def func():
b = threading.Semaphore(value=0)
b.acquire()

This waits for the semaphore to have a positive value. This thread is
blocked (apparently forever, since no other thread has access to the
semaphore).

a = threading.Thread(target=func)
a.start()

time.sleep(2)
quit()

When SystemExit is raised, nothing happens. Python hangs.

Your blocked thread is still around, still waiting for the semaphore.
Your program won't exit while there are non-daemon threads alive.

-- Alain.
.