Skip to content

Commit a896a01

Browse files
committed
shutdown all profile threads before running test_weakref_cache
1 parent 66c4a38 commit a896a01

File tree

1 file changed

+45
-36
lines changed

1 file changed

+45
-36
lines changed

distributed/tests/test_spill.py

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ def _print_chain(o_wr):
332332
return o
333333

334334

335+
@pytest.mark.parametrize("attempt", range(1000))
335336
@pytest.mark.parametrize(
336337
"cls,expect_cached",
337338
[
@@ -340,39 +341,47 @@ def _print_chain(o_wr):
340341
],
341342
)
342343
@pytest.mark.parametrize("size", [60, 110])
343-
def test_weakref_cache(tmpdir, cls, expect_cached, size):
344-
buf = SpillBuffer(str(tmpdir), target=100)
345-
346-
# Run this test twice:
347-
# - x is smaller than target and is evicted by y;
348-
# - x is individually larger than target and it never touches fast
349-
x = cls(size)
350-
canary = weakref.ref(x.canary)
351-
buf["x"] = x
352-
if size < 100:
353-
buf["y"] = cls(60) # spill x
354-
assert "x" in buf.slow
355-
356-
# Test that we update the weakref cache on setitem
357-
assert (buf["x"] is x) == expect_cached
358-
359-
# Do not use id_x = id(x), as in CPython id's are C memory addresses and are reused
360-
# by PyMalloc when you descope objects, so a brand new object might end up having
361-
# the same id as a deleted one
362-
id_x = x.id
363-
del x
364-
365-
if size < 100:
366-
buf["y"]
367-
368-
assert _print_chain(canary) is None
369-
assert "x" in buf.slow
370-
371-
x2 = buf["x"]
372-
assert x2.id != id_x
373-
if size < 100:
374-
buf["y"]
375-
assert "x" in buf.slow
376-
377-
# Test that we update the weakref cache on getitem
378-
assert (buf["x"] is x2) == expect_cached
344+
def test_weakref_cache(tmpdir, cls, expect_cached, size, attempt):
345+
from distributed.profile import shutdown_profile_threads
346+
347+
shutdown_profile_threads()
348+
349+
gc.disable()
350+
try:
351+
buf = SpillBuffer(str(tmpdir), target=100)
352+
353+
# Run this test twice:
354+
# - x is smaller than target and is evicted by y;
355+
# - x is individually larger than target and it never touches fast
356+
x = cls(size)
357+
canary = weakref.ref(x.canary)
358+
buf["x"] = x
359+
if size < 100:
360+
buf["y"] = cls(60) # spill x
361+
assert "x" in buf.slow
362+
363+
# Test that we update the weakref cache on setitem
364+
assert (buf["x"] is x) == expect_cached
365+
366+
# Do not use id_x = id(x), as in CPython id's are C memory addresses and are reused
367+
# by PyMalloc when you descope objects, so a brand new object might end up having
368+
# the same id as a deleted one
369+
id_x = x.id
370+
del x
371+
372+
if size < 100:
373+
buf["y"]
374+
375+
assert canary() is None
376+
assert "x" in buf.slow
377+
378+
x2 = buf["x"]
379+
assert x2.id != id_x
380+
if size < 100:
381+
buf["y"]
382+
assert "x" in buf.slow
383+
384+
# Test that we update the weakref cache on getitem
385+
assert (buf["x"] is x2) == expect_cached
386+
finally:
387+
gc.enable()

0 commit comments

Comments
 (0)