A possible bug in python threading/time module?

Tim Peters tim.one at comcast.net
Fri Jul 4 06:17:40 CEST 2003

> ...
> I expect there may be a bug *somewhere* in Python because a few
> thousand threads is really far fewer than I expect to create trouble
> on Windows.  The evidence I saw suggested the program actually
> finished all its "useful" work, and got hung in threading.py's
> _MainThread.__exitfunc(), waiting to join one of the worker threads
> at Python shutdown time.

Turns out that the Windows implementation of the Python C API function
PyThread_start_new_thread() contained several "laziness" errors, most

1. Not arranging to raise a proper exception if the MS
   _beginthread() function fails (which it can do if many
   threads are created quickly, as the test program does).

2. Not even considering that the Win32 CreateSemaphore() can fail.

3. Not accounting for that, if a great many threads are created
   very quickly, it may in fact take more than 5 seconds for any
   particular bootstrap function to finish.

I'm testing putative fixes on a Win98 box and don't see any hangs any more.
However, with more than about 3000 threads,

    thread.error: can't start new thread

gets raised because MS's _beginthread() fails (with errno == EAGAIN ==
"there are too many threads").

More information about the Python-list mailing list