Exit cleanly on Ctrl-C#7
Merged
Merged
Conversation
Connection threads inherited ThreadingMixIn's default daemon_threads = False. Because RangeHandler forces HTTP/1.1, browsers hold keep-alive connections open, leaving worker threads blocked in recv(). On Ctrl-C the interpreter waited on those non-daemon threads forever, so the process printed "Goodbye" but never exited (issue #1). Mark connection threads as daemons so they cannot block shutdown, and close the listening socket in a finally block. Closes #1 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Fixes #1. Starting the server, opening a page in a browser, then pressing Ctrl-C printed "Goodbye" but the process never exited.
Cause
MyServerusesThreadingMixIn, whose default isdaemon_threads = False. SinceRangeHandler.setupforcesprotocol_version = "HTTP/1.1", browsers hold keep-alive connections open, leaving each worker thread blocked inrecv(). On Ctrl-C,serve_forever()raisesKeyboardInterruptand "Goodbye" is printed, but interpreter shutdown then waits on those non-daemon worker threads forever — hence the hang.Fix
daemon_threads = TrueonMyServerso lingering connection threads can't block interpreter exit.finallyblock for a clean teardown.Testing
test_server_uses_daemon_threadsas a regression guard.pytest, 20 passed).openssl s_client -connect localhost:8080), Ctrl-C now returns the shell prompt immediately.🤖 Generated with Claude Code