Commit 9128c3d
fix(test): hoist auth patches out of per-thread bodies (mock thread-safety)
`test_isolated_session_separate_threads_get_separate_clients` failed
intermittently on the 3.10 lane (passed 3.11/3.12) with KeyError: 2 —
the assertion `clients[1] is not clients[2]` couldn't find the second
slot because thread 2 had errored mid-flight.
Root cause: each thread independently entered a
`with patch('services.auth.auth_service.get_auth_details', ...)`
block. `unittest.mock.patch` is not thread-safe — its __enter__ saves
the original attribute and __exit__ restores it, with no locking
between threads. When t1's __exit__ races with t2's __enter__ (or
vice versa), the real `get_auth_details` ends up callable for a brief
window, raising MissingCredentialsError on the unauthenticated CI
runner. That exception killed thread 2 silently, leaving the
clients[2] slot unset, which produced the KeyError on the
post-join assertion (instead of surfacing the actual error).
Two changes:
1. Hoist both patches (`get_auth_details` and `refresh_tokens`) out of
the per-thread function so they wrap the entire join window. The
threads see a stable mock auth surface for their full lifetime, no
patch/unpatch racing.
2. Capture per-thread exceptions in an `errors` dict, asserted before
the slot comparison. When this test ever does fail in the future
for an unrelated reason, the failure message will name the real
exception instead of just "KeyError".
Verified locally: passes 5/5 reruns of the targeted test, plus the full
suite stays green (`pytest tests/ -q --ignore=tests/test_live_smoke.py`
→ 100%).
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>1 parent c68f588 commit 9128c3d
1 file changed
Lines changed: 27 additions & 11 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
201 | 201 | | |
202 | 202 | | |
203 | 203 | | |
204 | | - | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
205 | 214 | | |
206 | 215 | | |
207 | 216 | | |
208 | 217 | | |
209 | 218 | | |
210 | 219 | | |
| 220 | + | |
| 221 | + | |
211 | 222 | | |
212 | | - | |
213 | | - | |
214 | | - | |
| 223 | + | |
215 | 224 | | |
| 225 | + | |
| 226 | + | |
216 | 227 | | |
217 | | - | |
218 | | - | |
219 | | - | |
220 | | - | |
221 | | - | |
222 | | - | |
223 | | - | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
224 | 240 | | |
225 | 241 | | |
0 commit comments