Skip to content

Commit 04b5b90

Browse files
committed
work harder to collect garbage during test runs
1 parent 1ab7690 commit 04b5b90

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

trio/_core/tests/test_run.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import pytest
1212
import attr
1313

14-
from .tutil import check_sequence_matches
14+
from .tutil import check_sequence_matches, gc_collect_harder
1515
from ...testing import (
1616
wait_all_tasks_blocked, Sequencer, assert_yields,
1717
)
@@ -41,7 +41,8 @@ def ignore_coroutine_never_awaited_warnings():
4141
finally:
4242
# Make sure to trigger any coroutine __del__ methods now, before
4343
# we leave the context manager.
44-
gc.collect()
44+
gc_collect_harder()
45+
4546

4647
def test_basic():
4748
async def trivial(x):
@@ -883,7 +884,7 @@ async def main():
883884
# Because this crashes, various __del__ methods print complaints on
884885
# stderr. Make sure that they get run now, so the output is attached to
885886
# this test.
886-
gc.collect()
887+
gc_collect_harder()
887888

888889

889890
def test_error_in_run_loop():
@@ -1490,6 +1491,8 @@ async def f(): # pragma: no cover
14901491
bad_call(len, [1, 2, 3])
14911492
assert "appears to be synchronous" in str(excinfo.value)
14921493

1494+
# Make sure no references are kept around to keep anything alive
1495+
del excinfo
14931496

14941497
def test_calling_asyncio_function_gives_nice_error():
14951498
async def misguided():

trio/_core/tests/tutil.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,26 @@
22

33
import pytest
44

5+
import gc
6+
57
# See trio/tests/conftest.py for the other half of this
68
slow = pytest.mark.skipif(
79
not pytest.config.getoption("--run-slow", True),
810
reason="use --run-slow to run slow tests",
911
)
1012

11-
from ... import _core
13+
def gc_collect_harder():
14+
# In the test suite we sometimes want to call gc.collect() to make sure
15+
# that any objects with noisy __del__ methods (e.g. unawaited coroutines)
16+
# get collected before we continue, so their noise doesn't leak into
17+
# unrelated tests.
18+
#
19+
# On PyPy, coroutine objects (for example) can survive at least 1 round of
20+
# garbage collection, because executing their __del__ method to print the
21+
# warning can cause them to be resurrected. So we call collect a few times
22+
# to make sure.
23+
for _ in range(4):
24+
gc.collect()
1225

1326
# template is like:
1427
# [1, {2.1, 2.2}, 3] -> matches [1, 2.1, 3] or [1, 2.2, 3]

0 commit comments

Comments
 (0)