Skip to content
Open
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 src/hypercorn/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ def shutdown(*args: Any) -> None:
for process in processes:
process.terminate()

exitcode = _join_exited(processes) if exitcode != 0 else exitcode
if exitcode != 0:
_join_exited(processes)

for sock in sockets.secure_sockets:
sock.close()
Expand Down
42 changes: 42 additions & 0 deletions tests/test_run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from __future__ import annotations

from unittest.mock import Mock

from _pytest.monkeypatch import MonkeyPatch

import hypercorn.run
from hypercorn.config import Config


def test_run_preserves_reloaded_worker_failure_exitcode(monkeypatch: MonkeyPatch) -> None:
config = Config()
config.application_path = "example:app"
config.use_reloader = True
monkeypatch.setattr(
config,
"create_sockets",
Mock(return_value=Mock(secure_sockets=[], insecure_sockets=[])),
)

initial_process = Mock(exitcode=0, sentinel=object())
failed_process = Mock(exitcode=1, sentinel=object())
new_processes = [initial_process, failed_process]

def populate(processes: list[Mock], *args: object) -> None:
processes.append(new_processes.pop(0))

context = Mock()
context.Event.return_value = Mock()
monkeypatch.setattr(hypercorn.run, "get_context", Mock(return_value=context))
monkeypatch.setattr(hypercorn.run, "_populate", populate)
monkeypatch.setattr(hypercorn.run, "load_application", Mock())
monkeypatch.setattr(hypercorn.run, "files_to_watch", Mock(return_value=set()))
monkeypatch.setattr(
hypercorn.run,
"check_for_updates",
Mock(side_effect=[True, False]),
)
monkeypatch.setattr(hypercorn.run, "wait", Mock(side_effect=[set(), {failed_process.sentinel}]))
monkeypatch.setattr(hypercorn.run.signal, "signal", Mock())

assert hypercorn.run.run(config) == 1