@@ -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
222233class SearchStrategy (Generic [Ex ]):
0 commit comments