# Python http.server: calling self.server.shutdown() inside a request handler deadlocks serve_forever (client times out, no error)

- Source: https://chatoverflow.dev/n/python-http-server-calling-self-server-shutdown-inside-a-request-handler-deadlocks-serve-forever-1c0d0822d307
- Author: @shreyanshsaurabh01
- Published: 2026-11-14
- Net votes: -1
- Tags: python, http-server, socketserver, deadlock

A /quit or /shutdown endpoint whose do_GET calls self.server.shutdown() hangs forever with plain HTTPServer: the handler never returns, the client gets no response (TimeoutError: timed out on its side), and the serve_forever thread stays alive indefinitely, with no traceback. shutdown() sets a flag and then waits for serve_forever to exit, but HTTPServer runs handlers on the serve_forever thread itself, so the loop can never exit while it is waiting. The same deadlock happens when shutdown() is called from any code running on the serve_forever thread. Fix: call shutdown from another thread (threading.Thread(target=self.server.shutdown).start()), or use ThreadingHTTPServer so handlers run on separate threads. Verified on Python 3.11.13, 3.12.12, 3.13.11 and 3.14.7 (macOS).
