Skip to content

Commit 86a377f

Browse files
committed
add reset_sse_app_status workaround for sse_starlette quirk
1 parent 8188bf2 commit 86a377f

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

tests/shared/test_sse.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from anyio.abc import TaskGroup
99
from inline_snapshot import snapshot
1010
from pydantic import AnyUrl
11+
from sse_starlette.sse import AppStatus
1112
from starlette.applications import Starlette
1213
from starlette.requests import Request
1314
from starlette.responses import Response
@@ -32,6 +33,19 @@
3233
Tool,
3334
)
3435

36+
37+
@pytest.fixture(autouse=True)
38+
def reset_sse_app_status():
39+
"""Reset sse-starlette's global AppStatus singleton before each test.
40+
41+
This is necessary because AppStatus.should_exit_event (a global anyio.Event) gets bound
42+
to one event loop but accessed from others during parallel test execution (xdist workers),
43+
causing RuntimeError("bound to a different event loop"), which prevents the SSE server
44+
from responding (leaving status at 499) and causes ClosedResourceError during teardown.
45+
"""
46+
AppStatus.should_exit_event = anyio.Event()
47+
48+
3549
SERVER_NAME = "test_server_for_SSE"
3650
TEST_SERVER_HOST = "testserver"
3751
TEST_SERVER_BASE_URL = f"http://{TEST_SERVER_HOST}"
@@ -106,8 +120,6 @@ async def handle_sse(request: Request) -> Response:
106120

107121

108122
# Test fixtures
109-
110-
111123
@pytest.fixture()
112124
def server_app() -> Starlette:
113125
"""Create test Starlette app with SSE transport"""
@@ -243,7 +255,6 @@ async def sse_client_mounted_server_app_session(
243255

244256
async with sse_client(
245257
f"{TEST_SERVER_BASE_URL}/mounted_app/sse",
246-
sse_read_timeout=0.5,
247258
httpx_client_factory=asgi_client_factory,
248259
) as streams:
249260
async with ClientSession(*streams) as session:
@@ -329,7 +340,6 @@ async def test_request_context_propagation(tg: TaskGroup, context_server_app: St
329340
f"{TEST_SERVER_BASE_URL}/sse",
330341
headers=custom_headers,
331342
httpx_client_factory=asgi_client_factory,
332-
sse_read_timeout=0.5,
333343
) as streams:
334344
async with ClientSession(*streams) as session:
335345
# Initialize the session

0 commit comments

Comments
 (0)