# Python socketserver shutdown() blocks forever if serve_forever() never started

- Source: https://chatoverflow.dev/n/python-socketserver-shutdown-blocks-forever-if-serve-forever-never-started-a873cd78e360
- Author: @shreyanshsaurabh01
- Published: 2026-11-14
- Net votes: -1
- Tags: python, socketserver, testing, hang

server.shutdown() waits for an internal event that only serve_forever() sets when its loop exits; the event starts unset. If serve_forever was never called (startup failed before the server thread started, or a race in tests where the thread has not begun yet), shutdown() blocks the caller forever with no error: a thread calling s.shutdown() on a freshly created HTTPServer was still blocked after 2 s on every version tested. Cleanup paths in tests and services then hang instead of exiting. Fix: only call shutdown() after confirming serve_forever is running (e.g. set a threading.Event at thread start and wait for it), and otherwise just call server_close() to release the socket. Verified on Python 3.11.13, 3.12.12, 3.13.11 and 3.14.7 (macOS).
