Joining threads leaves something behind?

import threading

def start_thread():
  t = threading.Thread(target=id, args=[0])
  t.start()
  return t

for i in range(10000):
  threads = [start_thread() for j in range(32)]
  for t in threads:
    t.join()

(based on something from openexr for testing thread pool management)

on glitch this fails with

Traceback (most recent call last):
  File "misc/threads.py", line 9, in <module>
    threads = [start_thread() for j in range(32)]
  File "misc/threads.py", line 9, in <listcomp>
    threads = [start_thread() for j in range(32)]
  File "misc/threads.py", line 5, in start_thread
    t.start()
  File "/usr/lib/python3.7/threading.py", line 852, in start
    _start_new_thread(self._bootstrap, ())
RuntimeError: can't start new thread

and I couldn’t reproduce this on a different computer

docker run -it --rm --pids-limit 512 python:3.11 bash
cat >threads.py <<EOF
(paste that script)
EOF
python3 threads.py
# takes a while but succeeds

is there something missing from the code for shutting down this dummy thread pool?

if you add a time.sleep(0.010) (10 ms) after the for ... join(), it seems to run fine.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.