From 3617964a850e952c1fd80e96af705597fd65121d Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Wed, 2 Dec 2026 07:33:58 -0600 Subject: [PATCH 1/4] tests: sandbox cut-off test re-dispatches until a lookup was served test_cut_off_waiters_never_start_a_second_call_on_a_hung_connection assumed each of its three dispatches reaches its lookup inside the 4.0s wall cap. On a loaded windows-2025 runner (CI run 46863103581) the first dispatch's cap fired before its worker sent the lookup (kill cause wall_cap, not wall_cap_lookups), so dispatch B became the hung call and only C refused: `assert 1 == 2` on the refusing helpers. A dispatch that served nothing proves nothing either way, so the test now sends it again until the visible effect it is there for appears: a runner call for A, one more refusing helper for B and for C, within a 30s budget inside the watchdog. Each dispatch must still hit the cap, and the guarded property is checked after every dispatch: no second runner call on the hung connection. A temporary mutation that skips the per-connection record (B calls the runner) fails the test. Verified-by: tests --- tests/test_sandbox_isolated.py | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/tests/test_sandbox_isolated.py b/tests/test_sandbox_isolated.py index bc3ab5144e..ecace7e76d 100644 --- a/tests/test_sandbox_isolated.py +++ b/tests/test_sandbox_isolated.py @@ -23,6 +23,7 @@ import os import threading import time +from collections.abc import Callable from decimal import Decimal from pathlib import Path from typing import Any @@ -1017,14 +1018,32 @@ def slow_refusal(lookup: codec.LookupRequest) -> str: env=None, graph=None, ) + + retry_until = time.monotonic() + 30.0 # inside the 60 s per-test watchdog + + def dispatch_until(reached: Callable[[], bool], what: str) -> None: + # On a loaded runner a worker can send its lookup only after its cap fired, or its helper + # can start only then (CI run 46863103581). That dispatch served nothing: it neither calls + # the runner nor builds a refusal, so it is sent again. Every dispatch still hits the cap. + while True: + with pytest.raises(SandboxError, match="exceeded the 4.0s wall cap"): + _bodies(registry, "h_db", session) + assert len(calls) <= 1, "a second runner call started on the hung connection" + if reached(): + return + if time.monotonic() > retry_until: + pytest.fail(f"no dispatch {what} before the retry budget ran out") + try: with db_lookup_mod.activated(hung): - for _ in range(3): # A hangs; B and C each find the connection busy - with pytest.raises(SandboxError, match="exceeded the 4.0s wall cap"): - _bodies(registry, "h_db", session) - # B's and C's helpers must be over before the count is read, or a late call could land - # after it. Each named itself in the refusal it was building when its cap fired. - assert len(refusing_helpers) == 2 + dispatch_until(lambda: bool(calls), "reached the runner") # A hangs + # B and C each find the connection busy. + dispatch_until(lambda: len(refusing_helpers) >= 1, "found the connection busy") + dispatch_until(lambda: len(refusing_helpers) >= 2, "found the connection busy") + # Every refusing helper must be over before the count is read, or a late call could + # land after it. Each named itself in the refusal it was building when its cap fired; + # a retry can add one beyond B's and C's. A helper too late to refuse returned before + # it could register, so it never reaches the runner either. for thread in refusing_helpers: thread.join(10) assert not thread.is_alive() From f1b2df32d792fb29dda9035fd69dde278dcb5516 Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Wed, 2 Dec 2026 07:34:31 -0600 Subject: [PATCH 2/4] tests: durability teardown census starts 3s back, past psutil's btime truncation test_the_TEARDOWN_ends_a_SUSPENDED_process_frozen_before_it_ran failed to arm on ubuntu (CI run 46868850826): "the teardown census did not see the frozen process". The census skips processes created before `since`, set 1.0s below time.time(). On Linux psutil reads a creation time as /proc/stat's btime, truncated by the kernel to whole seconds, plus the start time truncated to clock ticks, so a process can read up to about 1.01s early. One started within a tick of the cut reads as older, and psutil caches that value, so every poll for 15s skipped it. The census, the token fixture's teardown census and the import cut now start CENSUS_MARGIN (3.0s) back. The needles (token, sleeper mark, temporary directory) alone tie a process to its test, so the wider window admits no other test's process, and the test still requires the census to see the frozen process before it asserts the reap. Verified-by: tests Verified-by: code-review xhigh --- tests/test_durability_hook_push_bound.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/tests/test_durability_hook_push_bound.py b/tests/test_durability_hook_push_bound.py index 260eb8273c..5a8cd9dd48 100644 --- a/tests/test_durability_hook_push_bound.py +++ b/tests/test_durability_hook_push_bound.py @@ -102,9 +102,17 @@ ) +#: How far below ``time.time()`` a census starts. On Linux psutil builds a creation time from +#: ``/proc/stat``'s ``btime``, which the kernel truncates to whole seconds, plus the start time +#: truncated to clock ticks, so a process can read up to about 1.01s EARLIER than it started. A 1.0s +#: margin then misses one started within a tick of the cut, and psutil caches the value per process, +#: so every later poll misses it too: CI run 46868850826, "the teardown census did not see the frozen +#: process". A wall-clock step back by more than the margin is not covered. The needles alone tie a +#: process to its test, so a wider margin admits no other test's process. +CENSUS_MARGIN = 3.0 #: Nothing this file starts predates its import, so older processes are skipped unread: see #: ``_ours`` for what a full command-line walk costs on a loaded box. -_IMPORTED = time.time() - 1.0 +_IMPORTED = time.time() - CENSUS_MARGIN def _matching(token: str) -> list[str]: @@ -300,7 +308,7 @@ def token(tmp_path: Path, jobs: list[int]) -> Iterator[str]: outlives it -- on failure as well as on success.""" value = uuid.uuid4().hex[:12] # A margin below now, so a process started in this same instant still counts as this test's. - since = time.time() - 1.0 + since = time.time() - CENSUS_MARGIN try: yield value finally: @@ -497,7 +505,7 @@ def test_the_TEARDOWN_ends_a_SUSPENDED_process_frozen_before_it_ran( job. On Windows it is created suspended and never resumed, which is what a shell killed between ``CreateProcess(CREATE_SUSPENDED)`` and the resume leaves; elsewhere it is stopped on start. The census must SEE it first, or "nothing survived" would only mean nothing was found.""" - since = time.time() - 1.0 + since = time.time() - CENSUS_MARGIN frozen = subprocess.Popen( # noqa: S603 - fixed argv, no shell [sys.executable, "-c", "import time; time.sleep(60)", token], cwd=tmp_path, From 65364036559719db8c8cf2a0afe248c1c886262b Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Wed, 2 Dec 2026 07:35:06 -0600 Subject: [PATCH 3/4] tests: retried-close test waits for the close to forget itself test_a_retried_close_waits_on_the_close_already_in_flight read database._CLOSING right after `await asyncio.wait_for(retry, WAIT)`. The close drops its entry in a done callback queued when wait_closed ends. On a loaded windows-2025 runner (CI run 46876329705) the retry's 0.1s tick timer woke it in the same loop pass, ahead of the close's last step, so the retry ended before that callback ran. The test task, woken by the send in that pass, found `retry` already done, the await returned without yielding, and the read saw the entry: "a finished close is still tracked". The test now polls with the shared wait_until (up to WAIT) for the entry to go. Removing the _forget_close callback in a temporary local mutation still fails the test. Verified-by: tests --- tests/test_backlog2394_database_gate.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/test_backlog2394_database_gate.py b/tests/test_backlog2394_database_gate.py index c96018557e..c274e5b769 100644 --- a/tests/test_backlog2394_database_gate.py +++ b/tests/test_backlog2394_database_gate.py @@ -526,7 +526,15 @@ async def test_a_retried_close_waits_on_the_close_already_in_flight( assert await asyncio.wait_for(sending, WAIT) is None await asyncio.wait_for(retry, WAIT) assert destination._pool is None and rig.pools[0].waits == 1 - assert rig.pools[0] not in database._CLOSING, "a finished close is still tracked" + # The close forgets itself in a done callback, queued when wait_closed ends. On a loaded runner + # the retry's tick timer can wake it in that same pass, ahead of the close's last step, so the + # retry ends before that callback runs. If the send also ended then, this task resumes between + # the two, finds ``retry`` already done, and the await above returns without yielding: read at + # once the entry is still there (CI run 46876329705). A close that never forgets itself still + # fails here, once WAIT runs out. + assert await wait_until(lambda: rig.pools[0] not in database._CLOSING), ( + "a finished close is still tracked" + ) class _FailingClose: From 2358f1496c78c96810c8a7cb9962499b7a8bf50f Mon Sep 17 00:00:00 2001 From: Scott Hall Date: Wed, 2 Dec 2026 07:35:39 -0600 Subject: [PATCH 4/4] tests: SCP stop test ends its SCU with an abort, not a release test_scp_stop_runs_off_loop_and_lets_in_flight_cstore_finish timed out on ubuntu (CI run 46854824199) waiting 15s for its SCU thread. The SCP is stopping when the SCU releases, so it ends that release with an abort at once. pynetdicom 3.0.4's requestor can lose the abort: release() pauses the reactor by trusting _is_paused, which still reads True for the reactor send_c_store has just woken, and that reactor's next pass takes the abort off the queue. release() then waits out the SCU's 30s ACSE timeout. A local induction (requestor reactor 50 ms late to its abort check, release waiter 200 ms late) reproduces the CI TimeoutError with the old helper and passes with this change. The test's SCU is now an inline copy of _scu_cstore that ends with an abort, kept inside this test's body so it merges cleanly with PR 3556's edits to the helper. The C-STORE status is read before the SCU ends, so every claim holds: a stop that aborts the in-flight association (a temporary local mutation calling AE.shutdown) still fails the test. Verified-by: tests --- tests/test_dicom_scp.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/tests/test_dicom_scp.py b/tests/test_dicom_scp.py index 6f499f16c2..1505c789cb 100644 --- a/tests/test_dicom_scp.py +++ b/tests/test_dicom_scp.py @@ -415,11 +415,36 @@ async def gated_handler(data: bytes) -> IngressReceipt: await release_commit.wait() # park mid-commit until the test releases it return IngressReceipt.accepted("mid-gated") # the receipt: committed + def cstore_then_abort(port: int, data: bytes) -> tuple[bool, int | None]: + # _scu_cstore, ended with an A-ABORT instead of a release. The SCP is stopping by then, so + # it ends a release with an abort at once, and pynetdicom 3.0.4's requestor can lose that: + # release() pauses the reactor by trusting _is_paused, which still reads True for the + # reactor send_c_store has just woken. That reactor's next pass takes the abort off the + # queue, and release() waits out the SCU's 30 s ACSE timeout (CI run 46854824199). Nothing + # this test proves rides on how the SCU ends; the C-STORE status is read before it does. + from pydicom import dcmread + from pynetdicom import AE + + ds = dcmread(BytesIO(data)) + ae = AE(ae_title="MODALITY1") + ae.add_requested_context(ds.SOPClassUID, ds.file_meta.TransferSyntaxUID) + assoc = ae.associate("127.0.0.1", port, ae_title=_SCP_AE) + if not assoc.is_established: + return (False, None) + try: + status = assoc.send_c_store(ds) + # No Status when the association ended before the reply, as a stop that aborted would. + return (True, int(status.Status) if "Status" in status else None) + finally: + assoc.abort() + scp = _build_scp(captured) await scp.start(receipt_handler(gated_handler)) try: # Fire the SCU C-STORE off-thread; it parks inside send_c_store awaiting the reply. - cstore = asyncio.create_task(asyncio.to_thread(_scu_cstore, scp.sockport, make_sr_part10())) + cstore = asyncio.create_task( + asyncio.to_thread(cstore_then_abort, scp.sockport, make_sr_part10()) + ) await asyncio.wait_for(commit_begun.wait(), timeout=15) assert len(captured) == 1, "the in-flight commit must have begun before we stop"