RuntimeError: can't create new thread at interpreter shutdown

504 Views Asked by At

This is a forked repository from Github and when I try to deploy it to Heroku it shows the error:

2023-12-31T03:10:27.965787+00:00 app[web.1]: 
2023-12-31T03:10:27.965828+00:00 app[web.1]: RuntimeError: can't create new thread at interpreter shutdown
2023-12-31T03:10:27.965992+00:00 app[web.1]:     self._executor.open()
2023-12-31T03:10:27.966013+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.12/site-packages/pymongo/periodic_executor.py", line 87, in open
2023-12-31T03:10:27.966072+00:00 app[web.1]:     thread.start()
2023-12-31T03:10:27.966088+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.12/threading.py", line 992, in start
2023-12-31T03:10:27.966229+00:00 app[web.1]:     _start_new_thread(self._bootstrap, ())
2023-12-31T03:10:27.966249+00:00 app[web.1]: RuntimeError: can't create new thread at interpreter shutdown
2023-12-31T03:10:28.174616+00:00 heroku[web.1]: Process exited with status 1
2023-12-31T03:10:28.197914+00:00 heroku[web.1]: State changed from starting to crashed

It says:

RuntimeError: can't create new thread at interpreter shutdown"

Help me to solve this.

I'm trying to create a Telegram bot using that Github repo

1

There are 1 best solutions below

0
intgr On

Python 3.12 introduced a change that prevents creating new threads after the main thread has exited. It's not clear yet whether it is considered a bug and will be reverted. See CPython issue #115533 for details.

  • You could simply downgrade to Python 3.11 for now.
  • Change the logic of the main thread such that it doesn't exit prematurely, but waits for all child threads before returning. This is usually done by calling Thread.join() method on any child threads, or converting the thread spawning to use with ThreadPoolExecutor() as executor: ... (see documentation), which automatically waits at the end of the block.