Skip to content

Exit cleanly on Ctrl-C#7

Merged
bboe merged 1 commit into
mainfrom
fix-ctrl-c-exit
Jun 15, 2026
Merged

Exit cleanly on Ctrl-C#7
bboe merged 1 commit into
mainfrom
fix-ctrl-c-exit

Conversation

@bboe

@bboe bboe commented Jun 15, 2026

Copy link
Copy Markdown
Owner

Problem

Fixes #1. Starting the server, opening a page in a browser, then pressing Ctrl-C printed "Goodbye" but the process never exited.

Cause

MyServer uses ThreadingMixIn, whose default is daemon_threads = False. Since RangeHandler.setup forces protocol_version = "HTTP/1.1", browsers hold keep-alive connections open, leaving each worker thread blocked in recv(). On Ctrl-C, serve_forever() raises KeyboardInterrupt and "Goodbye" is printed, but interpreter shutdown then waits on those non-daemon worker threads forever — hence the hang.

Fix

  • Set daemon_threads = True on MyServer so lingering connection threads can't block interpreter exit.
  • Close the listening socket in a finally block for a clean teardown.

Testing

  • Added test_server_uses_daemon_threads as a regression guard.
  • Full suite passes (pytest, 20 passed).
  • Manually verified: with an idle keep-alive connection open (openssl s_client -connect localhost:8080), Ctrl-C now returns the shell prompt immediately.

🤖 Generated with Claude Code

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>
@bboe bboe force-pushed the fix-ctrl-c-exit branch from c8f6cb1 to 20cab38 Compare June 15, 2026 16:01
@bboe bboe merged commit c42c707 into main Jun 15, 2026
6 checks passed
@bboe bboe deleted the fix-ctrl-c-exit branch June 15, 2026 16:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

does not exit on ctrl-c

1 participant