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
11 changes: 8 additions & 3 deletions docs/SERVICE.md
Original file line number Diff line number Diff line change
Expand Up @@ -550,9 +550,14 @@ can be lost:
would write for the phases it finished can be lost. The stop that ran out logs an ERROR saying
so; a second stop call that joined it and ran out of its own time, cancelled or not, logs a
WARNING. A cancel of the engine's stop during this wait is held and raised once the store has
closed, except at least when a stop step that runs before the store's guarded teardown fails
first: the stop then ends on that failure without closing the store, as it does when no cancel
came.
closed, and the message graph's stop still runs first. The engine's other stop steps are
guarded the same way. With a message graph, at least these leave the store open: a cancel,
`KeyboardInterrupt` or `SystemExit` that leaves a step before the graph's stop, except a cancel
during this wait; a cancel requested before the stop, unless this wait is the first step; a
failed or cancelled graph stop, when a listener may still be live. The first two never begin the
graph's stop, but the remaining earlier steps run and can hold the stop. After a swallowed cancel
the graph's stop and the close run, unless a later step also ends cancelled. An inbound that will
not stop holds the graph's stop.
- **A CRL reload pass in flight** gets at most about 1.8 s at the 3 s minimum, or about 0.8 s
while a config write is still running, and less when an earlier stop step was slow, down to the
same 0.1 s. When the budget cuts the pass, it is abandoned with a WARNING; a cancel of the stop
Expand Down
407 changes: 303 additions & 104 deletions messagefoundry/pipeline/engine.py

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion messagefoundry/pipeline/remote_debug_watch.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def _bound(kept: float) -> float:
if task is None:
return
# ``wait``, not ``wait_for``: a loop task that something else cancelled must not make
# this call look cancelled, which would end the engine's stop before it closed the store.
# this call look cancelled, which the engine's stop would hold as a failed step.
try:
wait = _bound(STOP_FLOOR_SECONDS)
done, _pending = await asyncio.wait({task}, timeout=wait)
Expand Down
37 changes: 32 additions & 5 deletions tests/test_3929_transit_bulk_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,11 +418,20 @@ async def test_a_queued_call_is_cancelled_at_shutdown_and_never_runs() -> None:
assert ran == []


async def _cancel_this_stop() -> None:
"""A cancel of the stopping task itself. A bare ``raise CancelledError`` with no cancel of the
task is a leaked cancel, which Engine.stop reads as a step failure (BACKLOG #8190)."""
task = asyncio.current_task()
assert task is not None
task.cancel()
await asyncio.sleep(0)


class _CancelledDrain:
"""An alert sink whose state drain is cancelled, so the stop runs on as a cancelled stop."""

async def drain_state(self, timeout: float) -> None:
raise asyncio.CancelledError
await _cancel_this_stop()


async def test_a_cancelled_stop_logs_no_still_running_warning(
Expand Down Expand Up @@ -451,6 +460,8 @@ async def test_the_lease_is_released_when_the_store_close_raises(

async def close() -> None:
await real_close()
if raised is asyncio.CancelledError:
await _cancel_this_stop()
raise raised

monkeypatch.setattr(eng.store, "close", close)
Expand Down Expand Up @@ -524,7 +535,7 @@ def shutdown(self, wait: bool = True, *, cancel_futures: bool = False) -> None:
assert await asyncio.wait_for(shutdown_transit_pools(None), 3.0) is False


class _FailingBackupRunner:
class _FailingStop:
async def stop(self) -> None:
raise RuntimeError("synthetic stop step failure")

Expand All @@ -533,20 +544,36 @@ async def test_a_stop_that_raises_before_the_close_keeps_the_lease(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Item 1: a stop that raises before it reaches the store's close keeps the lease, since the
store and the graph may still use the pools; a later stop that reaches the close releases it."""
store and the graph may still use the pools; a later stop that reaches the close releases it.
Since BACKLOG #8190 the cases that end the stop before the close include at least a failed
graph stop, the one this test builds; Engine.stop's docstring lists the others."""
monkeypatch.setattr(row_seal, "_leases", 0)
eng = await _engine(tmp_path / "e.db")
pool = row_seal._transit_pool()
eng._backup_runner = _FailingBackupRunner()
eng._registry_runner = _FailingStop()
with pytest.raises(RuntimeError, match="synthetic stop step failure"):
await eng.stop()
assert row_seal._leases == 1
assert row_seal._transit_pool() is pool, "the early raise detached pools still in use"
eng._backup_runner = None
eng._registry_runner = None
await eng.stop()
assert row_seal._leases == 0


async def test_a_failed_step_before_the_graph_stop_still_releases_the_lease(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""BACKLOG #8190: a step that fails without leaving the graph live no longer ends the stop,
so the store closes and the lease is released."""
monkeypatch.setattr(row_seal, "_leases", 0)
eng = await _engine(tmp_path / "b.db")
row_seal._transit_pool()
eng._backup_runner = _FailingStop()
with pytest.raises(RuntimeError, match="synthetic stop step failure"):
await eng.stop()
assert row_seal._leases == 0


async def test_an_engine_constructor_that_raises_takes_no_lease(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
Expand Down
12 changes: 7 additions & 5 deletions tests/test_engine_stop_budget.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,12 +379,14 @@ async def test_a_cancel_during_the_crl_runner_first_wait_propagates_as_on_main(
await asyncio.gather(pass_task, return_exceptions=True)


async def test_a_cancel_of_engine_stop_during_the_crl_first_wait_leaves_as_on_main(
async def test_a_cancel_of_engine_stop_during_the_crl_first_wait_still_closes_the_store(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""As on main, the CRL stop comes before engine.stop()'s guarded teardown, so a cancel there
leaves engine.stop() at once and the store is not closed (measured on main 72ac872c9cd). This
pins main's behaviour, which this PR keeps; it is not a property to preserve."""
"""The CRL stop cancels its pass and passes the cancel on, as on main. engine.stop() holds that
cancel, closes the store, then raises it: every stop step is guarded (BACKLOG #8190). Before
that, a cancel here left engine.stop() at once with the store still open. With a registry
runner wired, which this test has not, it still ends the stop before the graph stop and
leaves the store open."""
eng = await _engine(tmp_path)
runner = CrlReloadRunner(stop_timeout_seconds=30.0)
pass_task = asyncio.ensure_future(_hangs())
Expand All @@ -407,7 +409,7 @@ async def _close() -> None:
await stopping
await asyncio.sleep(0)
assert pass_task.cancelled()
assert closed == []
assert closed == [True]
finally:
pass_task.cancel()
await asyncio.gather(pass_task, return_exceptions=True)
Expand Down
Loading
Loading