Skip to content

fix(tests): stabilize backfill-dedup race in test_readiness.py - #17

Merged
HRLoveFun merged 1 commit into
mainfrom
worktree-fix-backfill-dedup-test-race
Sep 11, 2026
Merged

HRLoveFun merged 1 commit into
mainfrom
worktree-fix-backfill-dedup-test-race

Conversation

@HRLoveFun

Copy link
Copy Markdown
Owner

背景

排查 PR #16(一个纯文档 PR)的 CI 失败时发现,main 分支自己合并 PR #15 之后的 CI 也是红的,同一个测试、同一个失败特征,重跑一次还是同样失败:

tests/test_readiness.py::test_kick_backfill_dedupes_an_in_flight_range
assert <Thread(Thread-81 (_run_backfill), stopped ...)> is <Thread(Thread-82 (_run_backfill), started ...)>

跟 PR #15/#16 的改动内容都无关(一个是 UI,一个是纯文档),所以单独开一个 PR 修。

根因

kick_backfill 只在第一个线程 is_alive() 时才去重,_run_backfillensure_range() 返回的瞬间就把自己从 _backfill_threads 里弹出——这是正确行为,不是实现 bug。

问题在测试本身:它对 TEST_AAPL(无网络、同步、跑得很快的 fixture)kick 一次后立刻读线程字典当作 first,再 kick 一次读 second,断言两者相同。如果第一个线程在测试代码执行到第二次 kick 之前就已经跑完并自我清理,second 自然是一个新线程——这是对"用真实时钟运气来保证还在 in-flight"的隐式依赖,CI 跑得快一点就会稳定失败。

修复

monkeypatchensure_range 换成一个卡在 threading.Event 上的版本,让"第一个线程还在跑"变成确定性状态而不是看运气。没有改动任何生产代码——kick_backfill 的去重逻辑(只在存活时去重)本来就是对的。

验证

  • 单独跑 5 次:全绿
  • tests/test_readiness.py 全量:全绿
  • pytest -m "not network" 全量(304 项):全绿
  • ruff check / ruff format --check:干净

🤖 Generated with Claude Code

The test asserted the first kick_backfill's thread was still tracked when
the second kick_backfill ran, but relied on real-clock luck: kick_backfill
only dedupes while the first thread is_alive(), and _run_backfill pops
itself from _backfill_threads the instant ensure_range() returns. Against
the real TEST_* fixture path (no network, synchronous, fast) that return
can beat the test back to its second call on a fast CI runner — main's own
post-merge CI just failed here deterministically (unrelated commit,
confirming this isn't a one-off flake).

Block ensure_range behind a threading.Event via monkeypatch so "still in
flight" is deterministic instead of timing-dependent. No production code
changed; kick_backfill's dedup behaviour (only while alive) is correct.

Verified: 5x isolated runs green, full test_readiness.py green, full
`pytest -m "not network"` suite green, ruff check/format clean.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@HRLoveFun
HRLoveFun merged commit 2726bd7 into main Sep 11, 2026
3 checks passed
@HRLoveFun
HRLoveFun deleted the worktree-fix-backfill-dedup-test-race branch September 11, 2026 13:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant