fix(clients/python): concurrency-safe Session.close() caching the real reply (mxaccessgw#202)
ci / nightly-windev (push) Has been skipped
ci / windows-x86 (push) Successful in 1m22s
ci / java (push) Successful in 2m24s
ci / portable (push) Failing after 4m37s

This commit is contained in:
Joseph Doherty
2026-11-09 12:04:13 -05:00
parent 9007966292
commit 58e9b6a05a
3 changed files with 56 additions and 15 deletions
@@ -374,3 +374,34 @@ class FakeStream:
def cancel(self) -> None:
self.cancelled = True
@pytest.mark.asyncio
async def test_session_close_is_concurrency_safe_and_caches_real_reply() -> None:
stub = FakeGatewayStub()
real = stub.close_session.replies[0]
real.final_state = pb.SESSION_STATE_CLOSED
original_call = stub.close_session.__call__
async def slow_close(request: Any, *, metadata: Any) -> Any:
await asyncio.sleep(0.01)
return await original_call(request, metadata=metadata)
class SlowUnary:
requests = stub.close_session.requests
async def __call__(self, request: Any, *, metadata: Any) -> Any:
return await slow_close(request, metadata=metadata)
stub.CloseSession = SlowUnary()
client = await GatewayClient.connect(
ClientOptions(endpoint="fake", api_key="k", plaintext=True),
stub=stub,
)
session = await client.open_session(client_session_name="pytest")
replies = await asyncio.gather(*(session.close() for _ in range(5)))
assert len(stub.close_session.requests) == 1
assert all(r is real for r in replies)
assert (await session.close()) is real