Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit cc480e1

Browse files
author
Sean Quah
committed
Fix HomeServers leaking due to #15334
When the gen-0 GC is run at the end of each test, the `TestCase` object usually still holds references to the `HomeServer` used during the test. As a result, the `HomeServer` gets promoted to gen-1 and then never garbage collected. Fix this by periodically running full GCs. Signed-off-by: Sean Quah <seanq@matrix.org>
1 parent 3b305de commit cc480e1

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

tests/unittest.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,13 +229,20 @@ def tearDown(orig: Callable[[], R]) -> R:
229229
#
230230
# The easiest way to do this would be to do a full GC after each test
231231
# run, but that is very expensive. Instead, we disable GC (above) for
232-
# the duration of the test so that we only need to run a gen-0 GC, which
233-
# is a lot quicker.
232+
# the duration of the test and only run a gen-0 GC, which is a lot
233+
# quicker. This doesn't clean up everything, since the TestCase
234+
# instance still holds references to objects created during the test,
235+
# such as HomeServers, so we do a full GC every so often.
234236

235237
@around(self)
236238
def tearDown(orig: Callable[[], R]) -> R:
237239
ret = orig()
238240
gc.collect(0)
241+
# Run a full GC every 50 gen-0 GCs.
242+
gen0_stats = gc.get_stats()[0]
243+
gen0_collections = gen0_stats["collections"]
244+
if gen0_collections % 50 == 0:
245+
gc.collect()
239246
gc.enable()
240247
set_current_context(SENTINEL_CONTEXT)
241248

0 commit comments

Comments
 (0)