Skip to content

Commit 25d75bb

Browse files
committed
Use list.append for session_id capture to fix 3.11 coverage gap
On Python 3.11 with coverage 7.10.7 (lowest-direct), plain statements immediately after async with sse_client(...) exits are not traced reliably due to cancellation in __aexit__ interacting with 3.11's zero-cost exception bytecode. Entering a new async with block does generate a line event, so removing the intermediate assert and indexing the list directly inside the httpx block sidesteps the tracer gap without needing a pragma.
1 parent f0057a5 commit 25d75bb

1 file changed

Lines changed: 3 additions & 9 deletions

File tree

tests/shared/test_sse.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -622,24 +622,18 @@ async def test_sse_session_cleanup_on_disconnect(server: None, server_url: str)
622622
POST requests to disconnected sessions return 202 Accepted followed by a
623623
ClosedResourceError when the server tries to write to the dead stream.
624624
"""
625-
captured_session_id: str | None = None
626-
627-
def on_session_created(session_id: str) -> None:
628-
nonlocal captured_session_id
629-
captured_session_id = session_id
625+
captured: list[str] = []
630626

631627
# Connect a client session, then disconnect
632-
async with sse_client(server_url + "/sse", on_session_created=on_session_created) as streams:
628+
async with sse_client(server_url + "/sse", on_session_created=captured.append) as streams:
633629
async with ClientSession(*streams) as session:
634630
await session.initialize()
635631

636-
assert captured_session_id is not None
637-
638632
# After disconnect, POST to the stale session should return 404
639633
# (not 202 as it did before the fix)
640634
async with httpx.AsyncClient() as client:
641635
response = await client.post(
642-
f"{server_url}/messages/?session_id={captured_session_id}",
636+
f"{server_url}/messages/?session_id={captured[0]}",
643637
json={"jsonrpc": "2.0", "method": "ping", "id": 99},
644638
headers={"Content-Type": "application/json"},
645639
)

0 commit comments

Comments
 (0)