Skip to content

Commit b80a9bd

Browse files
committed
Force per-test GC so leaked-stream warnings stay inside the scoped filters
The module-level filterwarnings marks only apply while a test in this file is the active warning context, but v1's leaked memory streams sit in reference cycles and were garbage-collected later: at pytest's session-unconfigure unraisable sweep (every test in this file passed but pytest exited 1 when run without xdist, e.g. -n 0 for --pdb debugging), or during an unrelated test on the same worker, where the global filterwarnings = ["error"] turned the deallocator warning into a failure of that innocent test. An autouse fixture now runs gc.collect() in each test's teardown, so the deallocator warnings fire where the scoped ignores apply. Verified: every previously failing single-test and full-file -n 0 invocation now exits 0, and cross-file runs no longer misattribute the warnings.
1 parent 43cf9ec commit b80a9bd

1 file changed

Lines changed: 19 additions & 1 deletion

File tree

tests/shared/test_streamable_http.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
entirely in process.
55
"""
66

7+
import gc
78
import json
89
import time
9-
from collections.abc import AsyncIterator
10+
from collections.abc import AsyncIterator, Iterator
1011
from contextlib import asynccontextmanager
1112
from dataclasses import dataclass, field
1213
from datetime import timedelta
@@ -70,6 +71,23 @@
7071
pytest.mark.filterwarnings("ignore:.*MemoryObject(Send|Receive)Stream:ResourceWarning"),
7172
]
7273

74+
75+
@pytest.fixture(autouse=True)
76+
def _collect_leaked_streams() -> Iterator[None]:
77+
"""Garbage-collect each test's leaked memory streams inside its own teardown.
78+
79+
The filterwarnings marks above only apply while a test in this file is the
80+
active warning context. The leaked streams sit in reference cycles, so without
81+
a forced collection their deallocator warnings fire wherever the garbage
82+
collector happens to run next: during an unrelated test (failing it, since the
83+
global ``filterwarnings = ["error"]`` has no ignore there) or at pytest's
84+
session-unconfigure unraisable sweep (exit code 1 after all tests passed when
85+
running without xdist, e.g. ``-n 0`` for ``--pdb`` debugging).
86+
"""
87+
yield
88+
gc.collect()
89+
90+
7391
# Test constants
7492
SERVER_NAME = "test_streamable_http_server"
7593
INIT_REQUEST = {

0 commit comments

Comments
 (0)