|
| 1 | +"""Pin: ``YamlNodeStore.set_nodes``'s shield-cancel-drain loop bounds |
| 2 | +the number of absorbed cancels at ``_MAX_CANCEL_DRAIN_ITERS``. |
| 3 | +
|
| 4 | +Without the bound, a persistent cancel-storm + a wedged worker |
| 5 | +thread (kernel I/O hang, NFS mount lost, encrypted volume sealed) |
| 6 | +would spin here forever holding ``self._lock`` — wedging every |
| 7 | +other ``set_nodes`` caller on the same store process-wide. The |
| 8 | +cap surfaces a clear ``RuntimeError`` and releases the lock so |
| 9 | +the wedge is visible rather than silent. |
| 10 | +""" |
| 11 | + |
| 12 | +from __future__ import annotations |
| 13 | + |
| 14 | +import asyncio |
| 15 | +import os |
| 16 | +import threading |
| 17 | +from pathlib import Path |
| 18 | + |
| 19 | +import pytest |
| 20 | + |
| 21 | +from dqliteclient.node_store import ( |
| 22 | + _MAX_CANCEL_DRAIN_ITERS, |
| 23 | + NodeInfo, |
| 24 | + YamlNodeStore, |
| 25 | +) |
| 26 | +from dqlitewire import NodeRole |
| 27 | + |
| 28 | + |
| 29 | +@pytest.mark.asyncio |
| 30 | +async def test_cancel_drain_loop_caps_at_max_iters_and_releases_lock( |
| 31 | + tmp_path: Path, |
| 32 | + monkeypatch: pytest.MonkeyPatch, |
| 33 | +) -> None: |
| 34 | + """Drive a persistent cancel-storm against a parked worker. After |
| 35 | + ``_MAX_CANCEL_DRAIN_ITERS`` cancels are absorbed, the loop must |
| 36 | + raise ``RuntimeError`` with a stuck-worker diagnostic AND release |
| 37 | + the lock so a subsequent ``set_nodes`` is not wedged. |
| 38 | + """ |
| 39 | + store = YamlNodeStore(tmp_path / "nodes.yaml") |
| 40 | + new_nodes = [ |
| 41 | + NodeInfo(node_id=1, address="127.0.0.1:9001", role=NodeRole.VOTER), |
| 42 | + ] |
| 43 | + |
| 44 | + started = threading.Event() |
| 45 | + can_finish = threading.Event() |
| 46 | + real_replace = os.replace |
| 47 | + |
| 48 | + def parked_replace( |
| 49 | + src: str | os.PathLike[str], |
| 50 | + dst: str | os.PathLike[str], |
| 51 | + ) -> None: |
| 52 | + started.set() |
| 53 | + # Park until the test explicitly releases — this stands in for |
| 54 | + # a wedged worker thread (NAS pause, NFS mount lost, etc.). |
| 55 | + can_finish.wait(timeout=30.0) |
| 56 | + real_replace(src, dst) |
| 57 | + |
| 58 | + monkeypatch.setattr("dqliteclient.node_store.os.replace", parked_replace) |
| 59 | + |
| 60 | + task = asyncio.create_task(store.set_nodes(new_nodes)) |
| 61 | + |
| 62 | + # Wait for the worker thread to enter the parked replace. |
| 63 | + for _ in range(200): |
| 64 | + if started.is_set(): |
| 65 | + break |
| 66 | + await asyncio.sleep(0.01) |
| 67 | + assert started.is_set(), "worker did not enter parked_replace" |
| 68 | + assert store._lock.locked(), "lock must be held while inner runs" |
| 69 | + |
| 70 | + # Cancel-storm: well past the cap. |
| 71 | + for _ in range(_MAX_CANCEL_DRAIN_ITERS + 5): |
| 72 | + task.cancel() |
| 73 | + # Give the loop a chance to absorb each cancel. |
| 74 | + await asyncio.sleep(0) |
| 75 | + await asyncio.sleep(0) |
| 76 | + |
| 77 | + # The cap fires: RuntimeError surfaces with the stuck-worker |
| 78 | + # diagnostic. The pre-fix behaviour would have spun absorbing |
| 79 | + # cancels forever. |
| 80 | + with pytest.raises(RuntimeError, match="cancel-drain budget exceeded"): |
| 81 | + await task |
| 82 | + |
| 83 | + # Lock released so a subsequent set_nodes is not wedged. |
| 84 | + assert not store._lock.locked(), ( |
| 85 | + "lock must be released after the cap fires — otherwise every " |
| 86 | + "subsequent set_nodes is wedged process-wide" |
| 87 | + ) |
| 88 | + |
| 89 | + # Release the parked worker so the orphaned to_thread future |
| 90 | + # finishes cleanly and the test does not hang the runner shutdown. |
| 91 | + can_finish.set() |
| 92 | + |
| 93 | + |
| 94 | +@pytest.mark.asyncio |
| 95 | +async def test_modest_cancel_burst_below_cap_still_completes_normally( |
| 96 | + tmp_path: Path, |
| 97 | + monkeypatch: pytest.MonkeyPatch, |
| 98 | +) -> None: |
| 99 | + """A handful of cancels (well below the cap) must NOT trip the |
| 100 | + RuntimeError arm — the existing drain-loop semantics (wait for |
| 101 | + inner to finish, then re-raise the cancel) are preserved.""" |
| 102 | + store = YamlNodeStore(tmp_path / "nodes.yaml") |
| 103 | + new_nodes = [ |
| 104 | + NodeInfo(node_id=1, address="127.0.0.1:9001", role=NodeRole.VOTER), |
| 105 | + ] |
| 106 | + |
| 107 | + started = threading.Event() |
| 108 | + can_finish = threading.Event() |
| 109 | + real_replace = os.replace |
| 110 | + |
| 111 | + def slow_replace( |
| 112 | + src: str | os.PathLike[str], |
| 113 | + dst: str | os.PathLike[str], |
| 114 | + ) -> None: |
| 115 | + started.set() |
| 116 | + can_finish.wait(timeout=5.0) |
| 117 | + real_replace(src, dst) |
| 118 | + |
| 119 | + monkeypatch.setattr("dqliteclient.node_store.os.replace", slow_replace) |
| 120 | + |
| 121 | + task = asyncio.create_task(store.set_nodes(new_nodes)) |
| 122 | + |
| 123 | + for _ in range(200): |
| 124 | + if started.is_set(): |
| 125 | + break |
| 126 | + await asyncio.sleep(0.01) |
| 127 | + assert started.is_set() |
| 128 | + |
| 129 | + # Modest burst, well below the cap. |
| 130 | + for _ in range(3): |
| 131 | + task.cancel() |
| 132 | + await asyncio.sleep(0) |
| 133 | + |
| 134 | + # Release the worker — the drain loop completes normally and |
| 135 | + # re-raises CancelledError (no RuntimeError, no cap trip). |
| 136 | + can_finish.set() |
| 137 | + with pytest.raises(asyncio.CancelledError): |
| 138 | + await task |
| 139 | + |
| 140 | + assert not store._lock.locked() |
0 commit comments