Skip to content

Commit 0a2bdae

Browse files
committed
claude: fix recursive_property thread-safety; install Python via fresh uv metadata
Under threading, a concurrent thread could force a strategy property value between the two checks in recursive_property, so the final getattr(cache_key) could raise AttributeError. Return the forced-or-cached value instead, and add a regression test. Separately, point uv at the latest download-metadata.json so an older uv can still install a pinned Python patch release without 'uv self update'.
1 parent 4641d65 commit 0a2bdae

5 files changed

Lines changed: 41 additions & 5 deletions

File tree

‎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` inbetween 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

‎tooling/codespell-ignore.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ rouge
1212
tey
1313
tham
1414
datas
15+
inbetween

‎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")")

0 commit comments

Comments
 (0)