Skip to content

Commit 93ee3c9

Browse files
authored
Merge pull request #4772 from Liam-DeVoe/recursive-property-thread-safety
claude: fix recursive_property thread-safety; install Python via fresh uv metadata
2 parents 4641d65 + 0bb0f2f commit 93ee3c9

7 files changed

Lines changed: 44 additions & 10 deletions

File tree

‎.github/workflows/main.yml‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,6 @@ jobs:
8484
- check-py313-cover
8585
- check-py313-nocover
8686
- check-py313-niche
87-
- check-py313t-cover
88-
- check-py313t-nocover
89-
- check-py313t-niche
9087
- check-quality
9188
- check-pytest9
9289
- check-pytest84

‎hypothesis/RELEASE.rst‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
RELEASE_TYPE: patch
2+
3+
This patch fixes a thread-safety bug where concurrent use of the same strategy instance could error in rare cases. (:issue:`4475`).

‎hypothesis/src/hypothesis/strategies/_internal/strategies.py‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,14 @@ def recursive_property(strategy: "SearchStrategy", name: str, default: object) -
111111
calculation = "calc_" + name
112112
force_key = "force_" + name
113113

114-
def forced_value(target: SearchStrategy) -> Any:
114+
def forced_or_cached_value(target: SearchStrategy) -> Any:
115115
try:
116116
return getattr(target, force_key)
117117
except AttributeError:
118118
return getattr(target, cache_key)
119119

120120
try:
121-
return forced_value(strategy)
121+
return forced_or_cached_value(strategy)
122122
except AttributeError:
123123
pass
124124

@@ -133,7 +133,7 @@ def forced_value(target: SearchStrategy) -> Any:
133133
def recur(strat: SearchStrategy) -> Any:
134134
nonlocal hit_recursion
135135
try:
136-
return forced_value(strat)
136+
return forced_or_cached_value(strat)
137137
except AttributeError:
138138
pass
139139
result = mapping.get(strat, sentinel)
@@ -169,7 +169,7 @@ def recur(strat: SearchStrategy) -> Any:
169169
def recur2(strat: SearchStrategy) -> Any:
170170
def recur_inner(other: SearchStrategy) -> Any:
171171
try:
172-
return forced_value(other)
172+
return forced_or_cached_value(other)
173173
except AttributeError:
174174
pass
175175
listeners[other].add(strat)
@@ -216,7 +216,18 @@ def recur_inner(other: SearchStrategy) -> Any:
216216
# them (not just the strategy we started out with).
217217
for k, v in mapping.items():
218218
setattr(k, cache_key, v)
219-
return getattr(strategy, cache_key)
219+
# This used to simply be `getattr(strategy, cache_key)`. That relied on the invariant
220+
# that our loop above has set `strategy.cached_* = v` on `strategy` if we've reached
221+
# here. However, under threading, this is not necessarily true. If a concurrent thread
222+
# sets `strategy.force_* = v` in between the two places we check for `force_*`, we
223+
# will not set `strategy.cached_*`.
224+
#
225+
# There are several places where we might do this. unwrap_strategies sets
226+
# force_has_reusable_values = True. our numpy.py's `arrays` strategy also does.
227+
#
228+
# We guard against this in general by checking the forced and cached values here,
229+
# rather than just the cached value.
230+
return forced_or_cached_value(strategy)
220231

221232

222233
class SearchStrategy(Generic[Ex]):

‎hypothesis/tests/nocover/test_threading.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,22 @@ def slow_test(n):
200200
slow_test()
201201

202202

203+
def test_recursive_property_concurrent():
204+
# Regression test for https://github.com/HypothesisWorks/hypothesis/issues/4475.
205+
# see comment in recursive_property.
206+
counter = 0
207+
for i in range(10):
208+
leaf = st.integers(min_value=i)
209+
targets = (leaf, st.lists(leaf))
210+
211+
def validate(targets=targets):
212+
nonlocal counter
213+
counter += 1
214+
targets[counter % 2].validate()
215+
216+
run_concurrently(validate, n=8)
217+
218+
203219
def test_one_of_branches_lock():
204220
class SlowBranchesStrategy(SearchStrategy):
205221
@property

‎hypothesis/tox.ini‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[tox]
2-
envlist = py{310,py310,311,py311,312,313,313t,314,314t,315,315t}-{brief,full,cover,rest,nocover,niche,custom}
2+
envlist = py{310,py310,311,py311,312,313,314,314t,315,315t}-{brief,full,cover,rest,nocover,niche,custom}
33
toxworkdir={env:TOX_WORK_DIR:.tox}
44
# Create virtualenvs and resolve Python interpreters with uv (via tox-uv).
55
requires = tox-uv>=1.35

‎tooling/scripts/ensure-python.sh‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ REQUEST=$(translate_version "$VERSION")
6868

6969
mkdir -p "$SNAKEPIT" "$UV_PYTHON_INSTALL_DIR"
7070

71+
# uv resolves the set of installable Python downloads from a list baked into the
72+
# binary at build time, so a uv older than a given Python release can't install
73+
# it. Point uv at the latest metadata instead.
74+
export UV_PYTHON_DOWNLOADS_JSON_URL="https://raw.githubusercontent.com/astral-sh/uv/main/crates/uv-python/download-metadata.json"
75+
7176
uv python install "$REQUEST"
7277
PYTHON_BIN=$(uv python find --managed-python "$REQUEST")
7378
INSTALL_ROOT=$(dirname "$(dirname "$PYTHON_BIN")")

‎tooling/src/hypothesistooling/__main__.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,9 @@ def update_python_versions():
391391
continue
392392
if impl == "cpython":
393393
key = f"{major_minor}{'t' if ft else ''}"
394+
# see https://github.com/HypothesisWorks/hypothesis/pull/4772#issuecomment-4760983630
395+
if key == "3.13t":
396+
continue
394397
candidate = f"{ver}{'+freethreaded' if ft else ''}"
395398
else:
396399
assert impl == "pypy"
@@ -662,7 +665,6 @@ def run_tox(task, version, *args):
662665
"3.11": "3.11.15",
663666
"3.12": "3.12.13",
664667
"3.13": "3.13.14",
665-
"3.13t": "3.13.14+freethreaded",
666668
"3.14": "3.14.6",
667669
"3.14t": "3.14.6+freethreaded",
668670
"3.15": "3.15.0b2",

0 commit comments

Comments
 (0)