Python: http.server.ThreadingHTTPServer fails since update to Python 3.12

370 views Asked by At

I've got a simple Python HTTP server application based on http.server.ThreadingHTTPServer, running in a Docker container. It works fine with Python 3.11, but under Python 3.12 aborts with the following as soon as a HTTP request comes in:

Exception occurred during processing of request from ('127.0.0.1', 50544)
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/socketserver.py", line 318, in _handle_request_noblock
    self.process_request(request, client_address)
  File "/usr/local/lib/python3.12/socketserver.py", line 706, in process_request
    t.start()
  File "/usr/local/lib/python3.12/threading.py", line 971, in start
    _start_new_thread(self._bootstrap, ())
RuntimeError: can't create new thread at interpreter shutdown

The Dockerfile is:

FROM python:alpine
RUN pip3 install requests psycopg2-binary semantic-version
COPY mysource /
ENTRYPOINT /myprogram.py

and the server code in myprogram.py is something like

def runServer():
    server = http.server.ThreadingHTTPServer(('0.0.0.0', 1234), Server)
    server.serve_forever()

...
thread = threading.Thread(target=runServer)
thread.start()
...

Using HTTPServer instead of ThreadingHTTPServer makes the problem go away.

All worked fine until a few days ago when Python 3.12 came out to which the python:alpine image was obviously updated. With FROM python:3.12-alpine or FROM python (which I suppose is also 3.12) in the Dockerfile it fails, with FROM python:3.11-alpine or FROM: python:3-11 it works.

Did anyone else also observe this? Any idea what to, short of pinning the Python version to 3.11 (which I'm reluctant to do)?

0

There are 0 answers