fix(tests): stabilize backfill-dedup race in test_readiness.py - #17
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
排查 PR #16(一个纯文档 PR)的 CI 失败时发现,
main分支自己合并 PR #15 之后的 CI 也是红的,同一个测试、同一个失败特征,重跑一次还是同样失败:跟 PR #15/#16 的改动内容都无关(一个是 UI,一个是纯文档),所以单独开一个 PR 修。
根因
kick_backfill只在第一个线程is_alive()时才去重,_run_backfill在ensure_range()返回的瞬间就把自己从_backfill_threads里弹出——这是正确行为,不是实现 bug。问题在测试本身:它对
TEST_AAPL(无网络、同步、跑得很快的 fixture)kick 一次后立刻读线程字典当作first,再 kick 一次读second,断言两者相同。如果第一个线程在测试代码执行到第二次 kick 之前就已经跑完并自我清理,second自然是一个新线程——这是对"用真实时钟运气来保证还在 in-flight"的隐式依赖,CI 跑得快一点就会稳定失败。修复
用
monkeypatch把ensure_range换成一个卡在threading.Event上的版本,让"第一个线程还在跑"变成确定性状态而不是看运气。没有改动任何生产代码——kick_backfill的去重逻辑(只在存活时去重)本来就是对的。验证
tests/test_readiness.py全量:全绿pytest -m "not network"全量(304 项):全绿ruff check/ruff format --check:干净🤖 Generated with Claude Code