Skip to content

Commit

Permalink
avoid blocking on spawned threads upon server shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmicexplorer committed Aug 10, 2024
1 parent 8a84395 commit 0d72998
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1333,13 +1333,20 @@ def html_index_no_metadata(
@pytest.fixture
def html_index_with_range_server(
html_index_no_metadata: Path,
port: int = 8000,
) -> HTMLIndexWithRangeServer:
"""Serve files from a generated pypi index, with support for range requests.
Provide `-i http://localhost:8000` to pip invocations to point them at this server.
Provide `-i http://localhost:<port>` to pip invocations to point them at
this server.
"""

class InDirectoryServer(http.server.ThreadingHTTPServer):
# Avoid blocking on any (non-daemon) spawned threads upon server shutdown. We
# retain the default of daemon_threads=False, which should mean that threads get
# stopped and killed upon server shutdown with .shutdown().
block_on_close = False

def finish_request(self, request: Any, client_address: Any) -> None:
self.RequestHandlerClass(
request, client_address, self, directory=str(html_index_no_metadata) # type: ignore[call-arg,arg-type]
Expand All @@ -1360,7 +1367,7 @@ def range_handler(self) -> RangeHandler:
head_request_paths: ClassVar[Set[str]] = set()
ok_response_counts: ClassVar[Dict[str, int]] = {}

with InDirectoryServer(("", 8000), Handler) as httpd:
with InDirectoryServer(("", port), Handler) as httpd:
server_thread = threading.Thread(target=httpd.serve_forever)
server_thread.start()

Expand Down

0 comments on commit 0d72998

Please sign in to comment.