Commit ed35e87
test(config): fix the concurrent-writers test hang/flake (test-only, no prod change) (#223)
## Problem
`tests/test_concurrency.py::test_config_concurrent_writers_always_leave_a_valid_file`
intermittently **hangs ~20 minutes on Windows CI then gets cancelled**
at the job timeout (it was blocking #221, but it's a pre-existing flake
on `main`). The root causes are **in the test, not production** — two
bugs:
### 1. The 20-minute hang
A dedicated reader busy-looped `while not stop.is_set()`. A writer's
`f.result()` could re-raise **before** `stop.set()` ran, so the reader
was never released and the pool's `shutdown(wait=True)` on `__exit__`
blocked forever.
### 2. Why a writer raised (the flake)
That reader was a **zero-gap busy-spin**, holding `config.toml` open
continuously. On Windows (no atomic replace-over-open) this made nearly
every concurrent `os.replace` fail the transient sharing-violation and
exhaust its retry — a synthetic contention level no single-user CLI ever
produces.
## Approach — fix the test, leave production alone
An earlier draft of this PR hardened production's
`_retry_on_sharing_violation` (exponential jitter, bigger budget, an
`S311` ruff-ignore) so it could survive that synthetic hammer. That was
the wrong layer — gold-plating prod to satisfy an unrealistic test.
This version is **test-only**:
- Replaces the dedicated-reader + `stop` Event shape with **each thread
doing a few bounded write+read cycles**. Reads still race other threads'
writes, so the real invariant is still tested — `os.replace` atomicity,
no torn/`invalid_config` reads — but file access is **paced by the write
work** (no zero-gap pin) and **bounded** (no perpetual reader). It can
therefore neither hang nor manufacture the Windows contention storm.
- Production's existing modest retry
(`config._retry_on_sharing_violation`) is **untouched** and remains
directly covered by the three `test_retry_on_sharing_violation_*` unit
tests.
Net diff: **`tests/test_concurrency.py` only** — no `config.py` or
`pyproject.toml` change.
## Verification
Full `scripts/check.sh` gate passes locally (`All checks passed.`). The
Windows-only behavior can't be reproduced from the Linux gate, but the
new shape is bounded (hang is structurally impossible) and paced (no
continuous file-open), so the sharing-violation storm that exhausted the
retry can't occur. This PR's own Windows CI is the live confirmation.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
https://claude.ai/code/session_01F24PozqxFy2sCAApA1Ne1b
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 3ae8404 commit ed35e87
1 file changed
Lines changed: 5 additions & 38 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
9 | | - | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
10 | 13 | | |
11 | 14 | | |
12 | 15 | | |
| |||
17 | 20 | | |
18 | 21 | | |
19 | 22 | | |
20 | | - | |
21 | 23 | | |
22 | 24 | | |
23 | 25 | | |
| |||
76 | 78 | | |
77 | 79 | | |
78 | 80 | | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | 81 | | |
115 | 82 | | |
116 | 83 | | |
| |||
0 commit comments