|
| 1 | +"""Signal-safety tests for ReadBuffer (issues 037, 041, 045). |
| 2 | +
|
| 3 | +These tests use ``sys.settrace`` to inject a ``KeyboardInterrupt`` at a |
| 4 | +specific source line transition, which is the most reliable way to |
| 5 | +exercise CPython's bytecode-boundary async-exception model without |
| 6 | +relying on wallclock timing. Each test verifies that when an async |
| 7 | +exception lands in the middle of a multi-statement mutation, the |
| 8 | +buffer is left in a state that either: |
| 9 | +
|
| 10 | +- is self-consistent (``available()`` is non-negative, reads don't |
| 11 | + return fabricated bytes), OR |
| 12 | +- is poisoned, so the next caller fails fast with ``ProtocolError``. |
| 13 | +
|
| 14 | +"Looks fine but is silently broken" is the failure mode these tests |
| 15 | +guard against. |
| 16 | +""" |
| 17 | + |
| 18 | +from __future__ import annotations |
| 19 | + |
| 20 | +import contextlib |
| 21 | +import sys |
| 22 | +from types import FrameType |
| 23 | +from typing import Any |
| 24 | + |
| 25 | +import pytest |
| 26 | + |
| 27 | +from dqlitewire.buffer import ReadBuffer |
| 28 | +from dqlitewire.exceptions import DecodeError, ProtocolError |
| 29 | + |
| 30 | +_DEFAULT_INJECTED_EXC: BaseException = KeyboardInterrupt("injected") |
| 31 | + |
| 32 | + |
| 33 | +def _raise_on_source_match( |
| 34 | + func_name: str, |
| 35 | + needle: str, |
| 36 | + exc: BaseException = _DEFAULT_INJECTED_EXC, |
| 37 | +) -> Any: |
| 38 | + """Return a settrace-compatible tracer that raises ``exc`` on the |
| 39 | + first line inside ``func_name`` whose source contains ``needle``. |
| 40 | + """ |
| 41 | + state = {"raised": False} |
| 42 | + |
| 43 | + def tracer(frame: FrameType, event: str, arg: object) -> Any: |
| 44 | + if event != "line": |
| 45 | + return tracer |
| 46 | + if frame.f_code.co_name != func_name: |
| 47 | + return tracer |
| 48 | + try: |
| 49 | + with open(frame.f_code.co_filename) as f: |
| 50 | + src_line = f.readlines()[frame.f_lineno - 1] |
| 51 | + except OSError: |
| 52 | + return tracer |
| 53 | + if needle in src_line and not state["raised"]: |
| 54 | + state["raised"] = True |
| 55 | + raise exc |
| 56 | + return tracer |
| 57 | + |
| 58 | + return tracer |
| 59 | + |
| 60 | + |
| 61 | +class TestMaybeCompactSignalSafety: |
| 62 | + """Regression tests for issue 037. |
| 63 | +
|
| 64 | + ``ReadBuffer._maybe_compact`` used to be two separate ``STORE_ATTR`` |
| 65 | + bytecodes: |
| 66 | +
|
| 67 | + self._data = self._data[self._pos :] # (A) |
| 68 | + self._pos = 0 # (B) |
| 69 | +
|
| 70 | + CPython checks for pending signals at bytecode line transitions. A |
| 71 | + ``KeyboardInterrupt`` (or any ``PyErr_SetAsyncExc`` delivery) landing |
| 72 | + between (A) and (B) would leave the buffer with a freshly compacted |
| 73 | + ``_data`` but a stale ``_pos`` still pointing at the old offset. |
| 74 | + ``available()`` would return a negative number, reads would return |
| 75 | + nonsense, and no poison fired because no exception originated |
| 76 | + *inside* the buffer — the interrupt was purely external. A |
| 77 | + single-owner caller who did nothing wrong except press Ctrl-C |
| 78 | + during a busy decode loop would end up with silent message dropout. |
| 79 | +
|
| 80 | + The fix wraps the compaction in ``try/except BaseException`` and |
| 81 | + poisons on any torn state, so the next call raises ``ProtocolError`` |
| 82 | + instead of lying about the buffer's contents. |
| 83 | + """ |
| 84 | + |
| 85 | + def test_torn_compact_leaves_buffer_poisoned_or_consistent(self) -> None: |
| 86 | + buf = ReadBuffer() |
| 87 | + # Set up a pre-compact state: _pos > 4096 and a tail of bytes |
| 88 | + # that would remain after compaction. |
| 89 | + buf._data = bytearray(b"A" * 4096 + b"B" * 100) |
| 90 | + buf._pos = 4097 |
| 91 | + |
| 92 | + tracer = _raise_on_source_match("_maybe_compact", "self._pos = 0") |
| 93 | + |
| 94 | + sys.settrace(tracer) |
| 95 | + try: |
| 96 | + with contextlib.suppress(KeyboardInterrupt): |
| 97 | + buf._maybe_compact() |
| 98 | + finally: |
| 99 | + sys.settrace(None) |
| 100 | + |
| 101 | + # Post-condition: EITHER the buffer is poisoned (preferred), OR |
| 102 | + # it is self-consistent (available() non-negative, reads valid). |
| 103 | + # The old (broken) behaviour returned available() == -3998. |
| 104 | + if buf.is_poisoned: |
| 105 | + # Exact recovery semantics at the public-API layer are |
| 106 | + # covered by issue 038's tests; here we only require that |
| 107 | + # the buffer knows it is torn. Inspect _check_poisoned |
| 108 | + # directly, which is the single source of truth for the |
| 109 | + # poison gate. |
| 110 | + with pytest.raises(ProtocolError, match="poisoned"): |
| 111 | + buf._check_poisoned() |
| 112 | + else: |
| 113 | + assert buf.available() >= 0, ( |
| 114 | + f"torn compact left available()={buf.available()} " |
| 115 | + f"(len={len(buf._data)}, pos={buf._pos})" |
| 116 | + ) |
| 117 | + |
| 118 | + def test_happy_path_compact_still_works(self) -> None: |
| 119 | + """Sanity: without any interrupt, compact behaves normally.""" |
| 120 | + buf = ReadBuffer() |
| 121 | + buf._data = bytearray(b"A" * 4096 + b"B" * 100) |
| 122 | + buf._pos = 4097 |
| 123 | + |
| 124 | + buf._maybe_compact() |
| 125 | + |
| 126 | + assert not buf.is_poisoned |
| 127 | + assert buf._pos == 0 |
| 128 | + assert len(buf._data) == 99 |
| 129 | + assert buf.available() == 99 |
| 130 | + # And reads continue to work. |
| 131 | + tail = buf.read_bytes(5) |
| 132 | + assert tail == b"B" * 5 |
| 133 | + |
| 134 | + def test_compact_below_threshold_is_a_noop(self) -> None: |
| 135 | + """If _pos is below the compact threshold, the method does |
| 136 | + nothing — no state changes, no poison. |
| 137 | + """ |
| 138 | + buf = ReadBuffer() |
| 139 | + buf.feed(b"\x00" * 32) |
| 140 | + buf.read_bytes(8) |
| 141 | + assert buf._pos == 8 |
| 142 | + |
| 143 | + buf._maybe_compact() |
| 144 | + |
| 145 | + assert buf._pos == 8 |
| 146 | + assert not buf.is_poisoned |
| 147 | + |
| 148 | + |
| 149 | +class TestMaybeCompactPoisonIsWellFormed: |
| 150 | + """If the torn-state fix fires and poisons the buffer, the poison |
| 151 | + cause must be a real exception instance we can inspect — not None, |
| 152 | + not a raw BaseException we can't chain through. |
| 153 | + """ |
| 154 | + |
| 155 | + def test_poison_cause_is_recorded(self) -> None: |
| 156 | + buf = ReadBuffer() |
| 157 | + buf._data = bytearray(b"A" * 4096 + b"B" * 100) |
| 158 | + buf._pos = 4097 |
| 159 | + |
| 160 | + sentinel = RuntimeError("injected torn state") |
| 161 | + tracer = _raise_on_source_match("_maybe_compact", "self._pos = 0", exc=sentinel) |
| 162 | + |
| 163 | + sys.settrace(tracer) |
| 164 | + try: |
| 165 | + with contextlib.suppress(RuntimeError): |
| 166 | + buf._maybe_compact() |
| 167 | + finally: |
| 168 | + sys.settrace(None) |
| 169 | + |
| 170 | + if buf.is_poisoned: |
| 171 | + with pytest.raises(ProtocolError) as ei: |
| 172 | + buf._check_poisoned() |
| 173 | + # The original exception should appear in the cause chain. |
| 174 | + cause = ei.value.__cause__ |
| 175 | + assert cause is not None |
| 176 | + # Either directly the sentinel, or a wrapper around it. |
| 177 | + assert cause is sentinel or isinstance(cause, Exception) |
| 178 | + |
| 179 | + def test_non_exception_base_exception_does_not_crash_poison(self) -> None: |
| 180 | + """KeyboardInterrupt is not an Exception subclass. The poison |
| 181 | + path must still accept it (either by widening poison()'s |
| 182 | + parameter type or by wrapping).""" |
| 183 | + # Use the class above's scenario but with KeyboardInterrupt — |
| 184 | + # that is what CPython actually delivers on Ctrl-C. |
| 185 | + buf = ReadBuffer() |
| 186 | + buf._data = bytearray(b"A" * 4096 + b"B" * 100) |
| 187 | + buf._pos = 4097 |
| 188 | + |
| 189 | + tracer = _raise_on_source_match( |
| 190 | + "_maybe_compact", "self._pos = 0", exc=KeyboardInterrupt("ctrl-c") |
| 191 | + ) |
| 192 | + |
| 193 | + sys.settrace(tracer) |
| 194 | + try: |
| 195 | + with contextlib.suppress(KeyboardInterrupt): |
| 196 | + buf._maybe_compact() |
| 197 | + finally: |
| 198 | + sys.settrace(None) |
| 199 | + |
| 200 | + # We don't care whether the buffer is poisoned or consistent; |
| 201 | + # we care that neither path raised TypeError from poison() |
| 202 | + # rejecting a BaseException that isn't an Exception. |
| 203 | + _ = buf.is_poisoned # just reading it must not raise |
| 204 | + _ = buf.available() |
| 205 | + |
| 206 | + |
| 207 | +def test_decode_error_is_still_recoverable_without_poison() -> None: |
| 208 | + """Counter-test: the torn-state poison must not fire on any |
| 209 | + "normal" code path. A legitimate decode on a clean buffer works |
| 210 | + as before.""" |
| 211 | + buf = ReadBuffer() |
| 212 | + buf.feed(b"\x00" * 16) |
| 213 | + assert not buf.is_poisoned |
| 214 | + _ = DecodeError # referenced for completeness |
0 commit comments