Fix memory leak in threading.py