Skip to content

prevent shutdown issues by making server thread daemonic #411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pytest_httpserver/httpserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,8 @@ def start(self) -> None:
)

self.port = self.server.port # Update port (needed if `port` was set to 0)
self.server_thread = threading.Thread(target=self.thread_target)
# Explicitly make the new thread daemonic to avoid shutdown issues
self.server_thread = threading.Thread(target=self.thread_target, daemon=True)
self.server_thread.start()

def stop(self):
Expand Down
1 change: 1 addition & 0 deletions tests/test_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ def test_sdist_contents(build: Build, version: str):
"test_querystring.py",
"test_release.py",
"test_ssl.py",
"test_thread_type.py",
"test_threaded.py",
"test_urimatch.py",
"test_wait.py",
Expand Down
21 changes: 21 additions & 0 deletions tests/test_thread_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from __future__ import annotations

import threading
from typing import TYPE_CHECKING

import requests
from werkzeug import Response

if TYPE_CHECKING:
from werkzeug import Request

from pytest_httpserver import HTTPServer


def test_server_thread_is_daemon(httpserver: HTTPServer):
def handler(_request: Request):
return Response(f"{threading.current_thread().daemon}")

httpserver.expect_request("/foo").respond_with_handler(handler)

assert requests.get(httpserver.url_for("/foo")).text == "True"