From 89081ad0d2a06974c66b68f7b3d5d61780e1464c Mon Sep 17 00:00:00 2001 From: Seongho Bae Date: Sat, 5 Sep 2026 18:19:18 +0900 Subject: [PATCH 1/2] perf(tests): stop paying the 0.5s serve_forever poll on every teardown The suite stands hundreds of throwaway http.server instances in for provider endpoints, all started as threading.Thread(target=server.serve_forever). The stop flag is only checked once per poll_interval, and shutdown() blocks until that check, so every teardown pays up to the 0.5s default. All 326 call sites under tests/ use the default; none passes the argument. Overriding the default in the root conftest takes the full suite from 652.57s to 46.68s with an identical 3395 passed / 1 skipped, measured back to back on one tree. Repeat patched runs land between 47s and 84s depending on machine load, so the honest range is roughly 8-14x. The override goes in conftest rather than the call sites because tests/test_telemetry.py pins production serve() to calling serve_forever() with no arguments, and because it is one file instead of 233. Co-Authored-By: Claude Opus 5 --- conftest.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/conftest.py b/conftest.py index 9d03eec80..e68ce02a2 100644 --- a/conftest.py +++ b/conftest.py @@ -4,6 +4,32 @@ module load time, which is only installed in the dedicated CI job. Ignore that directory during normal collection so the suite runs without the native toolchain. The Hypothesis property tests under ``tests/fuzz/`` are unaffected. + +The suite also stands hundreds of throwaway ``http.server`` instances in for +provider endpoints, every one of them started as +``threading.Thread(target=server.serve_forever, daemon=True)``. +``socketserver.BaseServer.serve_forever`` only checks its stop flag once per +``poll_interval`` seconds, and ``shutdown()`` blocks until that next check, so +each teardown pays up to the 0.5s default. No call site in this repository +passes the argument, so that default is paid several hundred times per run and +dominates the wall clock: shortening it takes the suite from about eleven +minutes to about one, with no change to what is asserted. + +This overrides the default rather than the call sites because a mocked server +in ``tests/test_telemetry.py`` pins production ``serve()`` to invoking +``serve_forever()`` with no arguments. """ +import socketserver + collect_ignore = ["fuzz"] + +_ORIGINAL_SERVE_FOREVER = socketserver.BaseServer.serve_forever + + +def _serve_forever(self, poll_interval: float = 0.01): + """Serve with a short stop-flag poll so ``shutdown()`` returns promptly.""" + return _ORIGINAL_SERVE_FOREVER(self, poll_interval) + + +socketserver.BaseServer.serve_forever = _serve_forever From c045c79b0221d4f58f8231a8be2c35c00762d077 Mon Sep 17 00:00:00 2001 From: Seongho Bae Date: Sat, 5 Sep 2026 19:03:27 +0900 Subject: [PATCH 2/2] test(perf): lock pytest server polling contract --- tests/test_test_server_poll_interval.py | 35 +++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 tests/test_test_server_poll_interval.py diff --git a/tests/test_test_server_poll_interval.py b/tests/test_test_server_poll_interval.py new file mode 100644 index 000000000..8cd87d373 --- /dev/null +++ b/tests/test_test_server_poll_interval.py @@ -0,0 +1,35 @@ +"""Regression contracts for the pytest-only server polling override.""" + +import socketserver + +import conftest + + +def test_default_test_server_poll_interval_is_short(monkeypatch): + calls = [] + sentinel = object() + + def fake_serve_forever(server, poll_interval=0.5): + calls.append((server, poll_interval)) + return sentinel + + monkeypatch.setattr(conftest, "_ORIGINAL_SERVE_FOREVER", fake_serve_forever) + server = object() + + assert conftest._serve_forever(server) is sentinel + assert calls == [(server, 0.01)] + assert socketserver.BaseServer.serve_forever is conftest._serve_forever + + +def test_explicit_test_server_poll_interval_is_preserved(monkeypatch): + calls = [] + + def fake_serve_forever(server, poll_interval=0.5): + calls.append((server, poll_interval)) + + monkeypatch.setattr(conftest, "_ORIGINAL_SERVE_FOREVER", fake_serve_forever) + server = object() + + conftest._serve_forever(server, poll_interval=0.25) + + assert calls == [(server, 0.25)]