From 4e1302e569031789e50cb49235405a6e238b2046 Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Fri, 20 Nov 2026 15:56:27 +0000 Subject: [PATCH 1/3] Cover coordination transport reuse boundaries Specify fresh proof sessions, private transport ownership and failure disposal before implementing bounded reuse. Signed-off-by: zlareb1 --- tests/unit/test_ch_coordination_session.py | 143 ++++++++++++++++++++- 1 file changed, 142 insertions(+), 1 deletion(-) diff --git a/tests/unit/test_ch_coordination_session.py b/tests/unit/test_ch_coordination_session.py index 9f1c9a54df..169b9f20e0 100644 --- a/tests/unit/test_ch_coordination_session.py +++ b/tests/unit/test_ch_coordination_session.py @@ -209,12 +209,15 @@ async def proof() -> None: await asyncio.gather(proof(), proof()) await client.query_one("SELECT 3") - assert len(client.proofs) == 2 and all(driver.closed for driver in client.proofs) + assert len(client.proofs) == 2 + assert sum(not driver.closed for driver in client.proofs) <= 1 sessions = [ settings["session_id"] for driver in client.proofs for _, settings, _ in driver.calls ] assert len(set(sessions)) == 2 and all(sessions.count(value) == 2 for value in set(sessions)) assert len(client.client.calls) == 1 and "session_id" not in client.client.calls[0][1] + await client.disconnect() + assert all(driver.closed for driver in client.proofs) @pytest.mark.asyncio @@ -301,3 +304,141 @@ async def test_unsupported_operations_refuse_before_any_send_and_poison_the_scop with pytest.raises(ch.CoordinationSessionLost): await client.query_one("SELECT 3") assert len(client.proofs[0].calls) == 1 and other.client.calls == [] + + +@pytest.mark.asyncio +async def test_sequential_proofs_reuse_transport_but_bootstrap_distinct_sessions( + client: Any, +) -> None: + for index in range(3): + async with client.coordination_session(): + await client.query_one(f"SELECT {index + 2}") + + assert len(client.proofs) == 1 + driver = client.proofs[0] + assert not driver.closed + assert len(driver.calls) == 6 + sessions = [extra["session_id"] for _, extra, _ in driver.calls] + assert len(set(sessions)) == 3 + assert all(sessions.count(value) == 2 for value in set(sessions)) + assert [extra["session_check"] for _, extra, _ in driver.calls] == [0, 1] * 3 + assert all( + extra["use_query_cache"] == extra["use_query_condition_cache"] == 0 + for _, extra, _ in driver.calls + ) + assert client.client.calls == [] + await client.disconnect() + assert driver.closed + + +@pytest.mark.asyncio +async def test_a_caught_failure_discards_the_reusable_transport(client: Any) -> None: + async with client.coordination_session(): + first_channel = client._statement_client() + await client.query_one("SELECT 2") + first = client.proofs[0] + async with client.coordination_session(): + assert client._statement_client() is first_channel + first.error = ConnectionResetError("connection reset") + with pytest.raises(ConnectionResetError): + await client.query_one("SELECT 3") + assert first.closed + async with client.coordination_session(): + assert client._statement_client() is not first_channel + await client.query_one("SELECT 4") + assert len(client.proofs) == 2 + await client.disconnect() + assert all(driver.closed for driver in client.proofs) + + +@pytest.mark.asyncio +async def test_reusable_transport_is_not_shared_between_clients(client: Any) -> None: + async with client.coordination_session(): + original_channel = client._statement_client() + await client.query_one("SELECT 2") + original = client.proofs[0] + other = ch.CHClient() + other._client = _Driver() + other._connection_config = dict(client._connection_config) + async with other.coordination_session(): + assert other._statement_client() is not original_channel + await other.query_one("SELECT 3") + assert len(client.proofs) == 2 + assert not original.closed + await other.disconnect() + assert not original.closed + await client.disconnect() + assert original.closed + + +@pytest.mark.asyncio +@pytest.mark.parametrize("replacement", ["generation", "driver", "configuration"]) +async def test_idle_transport_cannot_cross_a_connection_replacement( + client: Any, replacement: str +) -> None: + async with client.coordination_session(): + original_channel = client._statement_client() + await client.query_one("SELECT 2") + original = client.proofs[0] + if replacement == "generation": + client._generation += 1 + elif replacement == "driver": + client._client = _Driver() + else: + client._connection_config = dict(client._connection_config, username="different") + async with client.coordination_session(): + assert client._statement_client() is not original_channel + await client.query_one("SELECT 3") + assert len(client.proofs) == 2 and original.closed + await client.disconnect() + assert all(driver.closed for driver in client.proofs) + + +@pytest.mark.asyncio +async def test_owner_exit_discards_a_channel_with_an_inflight_child(client: Any) -> None: + pending = None + try: + async with client.coordination_session(): + original_channel = client._statement_client() + original = client.proofs[0] + original.release = asyncio.Event() + pending = asyncio.create_task(client.query_one("SELECT 2")) + await original.entered.wait() + assert original.closed + async with client.coordination_session(): + assert client._statement_client() is not original_channel + await client.query_one("SELECT 3") + finally: + if pending is not None: + original.release.set() + outcomes = await asyncio.gather(pending, return_exceptions=True) + await client.disconnect() + assert isinstance(outcomes[0], ch.CoordinationSessionLost) + assert all(driver.closed for driver in client.proofs) + + +@pytest.mark.asyncio +async def test_idle_transport_reuse_never_revives_an_old_child_session(client: Any) -> None: + release = asyncio.Event() + + async def later() -> None: + await release.wait() + with pytest.raises(ch.CoordinationSessionLost): + await client.query_one("SELECT 2") + + pending = None + try: + async with client.coordination_session(): + original_channel = client._statement_client() + pending = asyncio.create_task(later()) + async with client.coordination_session(): + assert client._statement_client() is original_channel + release.set() + await pending + await client.query_one("SELECT 3") + assert len(client.proofs) == 1 and len(client.proofs[0].calls) == 3 + finally: + release.set() + if pending is not None: + await asyncio.gather(pending, return_exceptions=True) + await client.disconnect() From 54e6d8e0ab1b1c1b88f4e8ba3206a38544f15ecf Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Fri, 20 Nov 2026 16:17:58 +0000 Subject: [PATCH 2/3] Reuse private transports across fresh coordination proofs Keep one idle channel per client while rebuilding each checked session and discarding failed, replaced or active-child transports. Signed-off-by: zlareb1 --- server/core/ch.py | 102 +++++++++++---- tests/unit/test_ch_coordination_session.py | 125 +++++++++++++++++++ tests/unit/test_ch_coordination_transport.py | 33 +++-- 3 files changed, 229 insertions(+), 31 deletions(-) diff --git a/server/core/ch.py b/server/core/ch.py index 15170e721f..151e46a7b3 100644 --- a/server/core/ch.py +++ b/server/core/ch.py @@ -187,10 +187,12 @@ class _CoordinationSession: channel: AsyncClient | None = None checked: bool = False closed: bool = False + inflight: int = 0 def check(self, client: CHClient | None = None) -> None: if ( self.closed + or self.owner._disconnecting or (client is not None and client is not self.owner) or self.owner.generation != self.generation or self.owner.client is not self.driver @@ -200,6 +202,22 @@ def check(self, client: CHClient | None = None) -> None: raise CoordinationSessionLost() +@dataclass +class _CoordinationTransport: + owner_driver: AsyncClient + generation: int + configuration: dict[str, Any] + channel: AsyncClient + + def current(self, client: CHClient) -> bool: + return ( + not client._disconnecting + and client._client is self.owner_driver + and client.generation == self.generation + and client.__dict__.get("_connection_config") == self.configuration + ) + + _coordination_session: ContextVar[_CoordinationSession | None] = ContextVar( "ch_coordination_session", default=None ) @@ -876,6 +894,8 @@ class CHClient: _slots: asyncio.Semaphore | None _stream_slots: asyncio.Semaphore _reconnect_lock: asyncio.Lock + _coordination_idle: _CoordinationTransport | None + _disconnecting: bool def __init__(self) -> None: self._client: AsyncClient | None = None @@ -897,6 +917,8 @@ def _ensure_state(self) -> None: limit = int(settings.ch_pool_maxsize) d["_slots"] = asyncio.Semaphore(limit) if limit > 0 else None d.setdefault("_stream_slots", asyncio.Semaphore(STREAM_SLOTS)) + d.setdefault("_coordination_idle", None) + d.setdefault("_disconnecting", False) if "_reconnect_lock" not in d: d["_reconnect_lock"] = asyncio.Lock() @@ -906,7 +928,12 @@ async def coordination_session(self) -> AsyncIterator[None]: Every step still attests the backend; a checked session is never recreated. """ - if _coordination_session.get() is not None or _analyst_var.get() is not None: + self._ensure_state() + if ( + _coordination_session.get() is not None + or _analyst_var.get() is not None + or self._disconnecting + ): raise CoordinationSessionLost() session = _CoordinationSession(self, self.client, self.generation, uuid.uuid4().hex) configuration = dict(self._connection_config) @@ -918,6 +945,12 @@ async def coordination_session(self) -> AsyncIterator[None]: async def open_channel() -> None: # Own it before the guarded call's final generation/identity check. + idle, self._coordination_idle = self._coordination_idle, None + if idle is not None: + if idle.current(self): + session.channel = idle.channel + return + idle.channel._force_close() session.channel = await _make_driver( configuration, connector_limit=1, coordination=True ) @@ -928,22 +961,34 @@ async def open_channel() -> None: yield failed = False finally: + reusable = not failed and not session.closed and session.inflight == 0 session.closed = True _coordination_session.reset(token) try: - if session.channel is not None: - if failed: - # A delivered proof timeout cannot bound a fresh cleanup await. - session.channel._force_close() - else: - try: - await session.channel.close() - except BaseException: - session.channel._force_close() - raise + await self._return_coordination_transport(session, configuration, reusable) finally: self._stream_slots.release() + async def _return_coordination_transport( + self, session: _CoordinationSession, configuration: dict[str, Any], reusable: bool + ) -> None: + if session.channel is None: + return + transport = _CoordinationTransport( + session.driver, session.generation, configuration, session.channel + ) + if not reusable or not transport.current(self): + # A failed or still-running proof must never lend its channel to another owner. + session.channel._force_close() + elif self._coordination_idle is None: + self._coordination_idle = transport + else: + try: + await session.channel.close() + except BaseException: + session.channel._force_close() + raise + @property def connected(self) -> bool: return self._client is not None @@ -958,20 +1003,31 @@ async def connect(self) -> None: await _turn_off_unsafe_condition_cache(client) self._client = client self._connection_config = configuration + self._disconnecting = False await _probe_vector_state(self) async def disconnect(self) -> None: self._ensure_state() - retired = list(self._retired.values()) - self._retired.clear() - for old in retired: - try: - await old.close() - except Exception: # noqa: BLE001 — closing is best effort - pass - if self._client is not None: - await self._client.close() - self._client = None + self._disconnecting = True + idle, self._coordination_idle = self._coordination_idle, None + try: + retired = list(self._retired.values()) + self._retired.clear() + for old in retired: + try: + await old.close() + except Exception: # noqa: BLE001 — closing is best effort + pass + if self._client is not None: + await self._client.close() + self._client = None + finally: + if idle is not None: + try: + await idle.channel.close() + except BaseException: + idle.channel._force_close() + raise @property def client(self) -> AsyncClient: @@ -1218,6 +1274,8 @@ async def _with_retry( # type: ignore[no-untyped-def] self._ensure_state() session = _coordination_session.get() took = False + if session is not None: + session.inflight += 1 try: if session is not None: session.check(self) @@ -1233,6 +1291,8 @@ async def _with_retry( # type: ignore[no-untyped-def] session.closed = True raise finally: + if session is not None: + session.inflight -= 1 if took: self._release_slot() diff --git a/tests/unit/test_ch_coordination_session.py b/tests/unit/test_ch_coordination_session.py index 169b9f20e0..d131eee398 100644 --- a/tests/unit/test_ch_coordination_session.py +++ b/tests/unit/test_ch_coordination_session.py @@ -442,3 +442,128 @@ async def later() -> None: if pending is not None: await asyncio.gather(pending, return_exceptions=True) await client.disconnect() + + +@pytest.mark.asyncio +@pytest.mark.parametrize("failure", ["bootstrap", "preflight", "unsupported", "body"]) +async def test_a_refused_reused_transport_is_not_lent_to_the_next_proof( + client: Any, monkeypatch: pytest.MonkeyPatch, failure: str +) -> None: + async with client.coordination_session(): + original_channel = client._statement_client() + original = client.proofs[0] + try: + if failure == "bootstrap": + original.error = ConnectionResetError("connection reset") + with pytest.raises(ConnectionResetError): + async with client.coordination_session(): + pytest.fail("a failed bootstrap admitted a proof") + elif failure == "body": + with pytest.raises(ValueError, match="proof refused"): + async with client.coordination_session(): + raise ValueError("proof refused") + else: + async with client.coordination_session(): + if failure == "preflight": + with monkeypatch.context() as patch: + patch.setattr( + ch, "guard_pending_schema", AsyncMock(side_effect=ValueError()) + ) + with pytest.raises(ValueError): + await client.query_one("SELECT 2") + else: + with pytest.raises(ch.CoordinationSessionLost): + await client.insert("loom._migration_lock", [{"name": "test"}]) + assert original.closed + async with client.coordination_session(): + assert client._statement_client() is not original_channel + await client.query_one("SELECT 3") + assert len(client.proofs) == 2 + finally: + await client.disconnect() + + +@pytest.mark.asyncio +async def test_cancelled_reuse_bootstrap_discards_the_transport_and_returns_capacity( + client: Any, +) -> None: + async with client.coordination_session(): + pass + original = client.proofs[0] + original.release = asyncio.Event() + capacity = client._stream_slots._value + + async def proof() -> None: + async with client.coordination_session(): + pytest.fail("a blocked bootstrap admitted a proof") + + pending = asyncio.create_task(proof()) + try: + await original.entered.wait() + pending.cancel() + with pytest.raises(asyncio.CancelledError): + await pending + assert original.closed and client._stream_slots._value == capacity + async with client.coordination_session(): + await client.query_one("SELECT 2") + assert len(client.proofs) == 2 + finally: + pending.cancel() + await asyncio.gather(pending, return_exceptions=True) + await client.disconnect() + + +@pytest.mark.asyncio +async def test_disconnect_cannot_receive_an_idle_channel_from_an_active_proof( + client: Any, monkeypatch: pytest.MonkeyPatch +) -> None: + entered, release = asyncio.Event(), asyncio.Event() + closing, closed = asyncio.Event(), asyncio.Event() + + async def proof() -> None: + async with client.coordination_session(): + entered.set() + await release.wait() + + async def close_shared() -> None: + closing.set() + await closed.wait() + + monkeypatch.setattr(client.client, "close", close_shared) + pending = asyncio.create_task(proof()) + disconnect = None + try: + await entered.wait() + disconnect = asyncio.create_task(client.disconnect()) + await closing.wait() + release.set() + await pending + assert client.proofs[0].closed + with pytest.raises(ch.CoordinationSessionLost): + async with client.coordination_session(): + pytest.fail("a disconnecting client admitted a proof") + finally: + release.set() + closed.set() + await asyncio.gather(pending, *([disconnect] if disconnect is not None else [])) + assert not client.connected and client._coordination_idle is None + + +@pytest.mark.asyncio +async def test_reused_channel_keeps_the_service_principal_for_each_fresh_session( + client: Any, +) -> None: + try: + with ch.caller_shaped(): + for _ in range(2): + async with client.coordination_session(): + await client.query_one("SELECT 2") + await client.query_one("SELECT 3") + assert len(client.proofs) == 1 + calls = client.proofs[0].calls + assert all(kwargs == {} for _, _, kwargs in calls) + assert [settings["session_check"] for _, settings, _ in calls] == [0, 1, 0, 1] + assert len({settings["session_id"] for _, settings, _ in calls}) == 2 + assert "Authorization" in client.client.calls[0][2]["transport_settings"] + finally: + await client.disconnect() diff --git a/tests/unit/test_ch_coordination_transport.py b/tests/unit/test_ch_coordination_transport.py index de952e86e0..ea2f340d07 100644 --- a/tests/unit/test_ch_coordination_transport.py +++ b/tests/unit/test_ch_coordination_transport.py @@ -85,8 +85,8 @@ async def create(**kwargs: Any) -> _RoutedDriver: else: assert await client.raw_bytes("SELECT 1") == b"1" assert client.client is shared and shared.calls == [] and not shared.closed - assert len(channels) == 1 and channels[0].closed - assert channels[0].close_calls == 1 and channels[0].force_calls == 0 + assert len(channels) == 1 and not channels[0].closed + assert channels[0].close_calls == 0 and channels[0].force_calls == 0 assert [call["session_check"] for call in channels[0].calls] == [0, 1] assert len({call["session_id"] for call in channels[0].calls}) == 1 assert {key: options[0][key] for key in client._connection_config} == client._connection_config @@ -94,6 +94,9 @@ async def create(**kwargs: Any) -> _RoutedDriver: assert options[0]["query_retries"] == 0 assert options[0]["settings"]["use_query_cache"] == 0 assert options[0]["settings"]["use_query_condition_cache"] == 0 + await client.disconnect() + assert channels[0].closed and channels[0].close_calls == 1 + assert channels[0].force_calls == 0 def _client_with_channel( @@ -151,17 +154,19 @@ async def test_a_missing_checked_session_closes_only_its_channel_without_reboots @pytest.mark.asyncio -async def test_cancelled_close_force_closes_the_owned_channel_and_returns_capacity( +async def test_cancelled_disconnect_force_closes_the_idle_channel_and_keeps_capacity( monkeypatch: pytest.MonkeyPatch, ) -> None: channel = _RoutedDriver(pooled=False) monkeypatch.setattr(channel, "close", AsyncMock(side_effect=asyncio.CancelledError())) client, _ = _client_with_channel(monkeypatch, channel) capacity = client._stream_slots._value + async with client.coordination_session(): + pass + assert not channel.closed with pytest.raises(asyncio.CancelledError): - async with client.coordination_session(): - pass - assert channel.closed and channel.force_calls == 1 and not client.client.closed + await client.disconnect() + assert channel.closed and channel.force_calls == 1 and not client.connected assert client._stream_slots._value == capacity and ch._coordination_session.get() is None @@ -221,8 +226,10 @@ async def proof() -> None: client._stream_slots.release() await pending assert pending.done() and client._stream_slots._value == 1 - assert channel.closed and not client.client.closed + assert not channel.closed and not client.client.closed factory.assert_awaited_once() + await client.disconnect() + assert channel.closed @pytest.mark.asyncio @@ -244,7 +251,9 @@ async def ordinary() -> Any: assert len(client.client.calls) == 1 and "session_id" not in client.client.calls[0] assert len(channel.calls) == 1 assert await client.query_one("SELECT 1") == {"value": 1} - assert channel.closed and not client.client.closed + assert not channel.closed and not client.client.closed + await client.disconnect() + assert channel.closed @pytest.mark.asyncio @@ -330,14 +339,18 @@ async def proof(index: int) -> None: assert len(channels) == ch.STREAM_SLOTS and slots._value == 0 release[0].set() await entered[-1].wait() - assert channels[0].closed and len(channels) == ch.STREAM_SLOTS + 1 + assert not channels[0].closed and len(channels) == ch.STREAM_SLOTS + assert len({call["session_id"] for call in channels[0].calls}) == 2 assert sum(not channel.closed for channel in channels) == ch.STREAM_SLOTS finally: for event in release: event.set() await asyncio.gather(*tasks, return_exceptions=True) - assert all(channel.closed for channel in channels) and slots._value == ch.STREAM_SLOTS + assert sum(not channel.closed for channel in channels) == 1 + assert slots._value == ch.STREAM_SLOTS assert not client.client.closed + await client.disconnect() + assert all(channel.closed for channel in channels) @pytest.mark.asyncio From e8fab20b421ca2b05d53a6c2608ade595eed4db7 Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Fri, 20 Nov 2026 16:36:02 +0000 Subject: [PATCH 3/3] Keep final-page focus requests within their DOM commit A pending passive effect can clear a request before its loading state commits. Hand focus over in a layout effect and reproduce the lost request before passive effects flush. Signed-off-by: zlareb1 --- frontend/src/lib/useLastPageFocus.test.tsx | 20 ++++++++++++++++++-- frontend/src/lib/useLastPageFocus.ts | 5 +++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/frontend/src/lib/useLastPageFocus.test.tsx b/frontend/src/lib/useLastPageFocus.test.tsx index 7e14a7e645..2481f15465 100644 --- a/frontend/src/lib/useLastPageFocus.test.tsx +++ b/frontend/src/lib/useLastPageFocus.test.tsx @@ -1,4 +1,4 @@ -import { useRef, useState } from "react"; +import { useLayoutEffect, useRef, useState } from "react"; import userEvent from "@testing-library/user-event"; import { afterEach, describe, expect, it } from "vitest"; import { act, cleanup, render, screen } from "../test/rtl"; @@ -8,12 +8,13 @@ import { useLastPageFocus } from "./useLastPageFocus"; // ends the read with these rows shown and says whether a page is left. let finish: (rows: string[], more: boolean) => void = () => {}; -function List({ first }: { first: string[] }) { +function List({ first, startOnCommit = false }: { first: string[]; startOnCommit?: boolean }) { const [rows, setRows] = useState(first); const [loading, setLoading] = useState(false); const [more, setMore] = useState(true); const box = useRef(null); const search = useRef(null); + const loadMore = useRef(null); const mark = useLastPageFocus({ loading, more, @@ -23,6 +24,13 @@ function List({ first }: { first: string[] }) { ), fallback: () => search.current, }); + useLayoutEffect(() => { + if (!startOnCommit) return; + // Start before the previous render's passive effects flush. + loadMore.current?.focus(); + mark(first); + setLoading(true); + }, []); finish = (next, left) => { setRows(next); setMore(left); @@ -42,6 +50,7 @@ function List({ first }: { first: string[] }) { {more && (