Skip to content

Commit

Permalink
Avoid incrementing time in GC callback
Browse files Browse the repository at this point in the history
  • Loading branch information
jobh committed May 13, 2024
1 parent 05d8a51 commit 3f79ceb
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion hypothesis-python/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from hypothesis._settings import is_in_ci
from hypothesis.errors import NonInteractiveExampleWarning
from hypothesis.internal.compat import add_note
from hypothesis.internal.conjecture import junkdrawer
from hypothesis.internal.detection import is_hypothesis_test

from tests.common import TIME_INCREMENT
Expand Down Expand Up @@ -74,7 +75,7 @@ def _consistently_increment_time(monkeypatch):
Replacing time with a fake version under our control avoids this problem.
"""
frozen = [False]
frozen = {0: False} # dict to make it a valid monkeypatch target

current_time = [time_module.time()]

Expand All @@ -98,6 +99,20 @@ def _patch(name, fn):
_patch("sleep", sleep)
monkeypatch.setattr(time_module, "freeze", freeze, raising=False)

# In the patched time regime, observing it causes it to increment. To avoid reintroducing
# non-determinism due to GC running at arbitrary times, we patch the GC observer
# to NOT increment time.

orig_callback = junkdrawer._gc_callback

def patched_callback(*args):
with monkeypatch.context() as mp:
mp.setitem(frozen, 0, True)
orig_callback(*args)

monkeypatch.setattr(junkdrawer, "_gc_callback", patched_callback)
monkeypatch.setattr(gc, "callbacks", [cb for cb in gc.callbacks if cb != orig_callback])


random_states_after_tests = {}
independent_random = random.Random()
Expand Down

0 comments on commit 3f79ceb

Please sign in to comment.