Skip to content

fix(tests): join _person helper threads in test_second_factor_terminal.py - #2614

Merged
abrichr merged 1 commit into
mainfrom
gemini/fix-second-factor-thread-flake
Nov 24, 2026
Merged

abrichr merged 1 commit into
mainfrom
gemini/fix-second-factor-thread-flake

Conversation

@abrichr

@abrichr abrichr commented Nov 24, 2026

Copy link
Copy Markdown
Member

Fixes a recurring pytest-xdist flake in packages/flow/tests/test_second_factor_terminal.py::test_a_timeout_restores_echo_and_the_next_prompt_gets_its_answer (seen on #2575, #2579, and #2606).

Root cause

  • _person() spawned unjoined daemon threads. When test_a_timeout_restores_echo_and_the_next_prompt_gets_its_answer snapshotted threads = threading.active_count() right after the preceding test, the prior test's _person() thread finished exiting during the 0.3s timeout, causing assert threading.active_count() == threads to fail with assert 2 == 3 / assert 1 == 2 (count went down, not up).

Fix

  • Track and join() all _person() helper threads in the terminal fixture teardown before closing the pty descriptors.
  • Change the no-leaked-reader check to assert threading.active_count() <= threads so an unrelated background thread exiting during the 0.3s wait cannot false-fail the test while a leaked reader thread still fails it.

Verification

  • uv run ruff check tests/test_second_factor_terminal.py — passed
  • uv run pytest tests/test_second_factor_terminal.py -v — 8 passed in 1.84s
  • gitleaks protect --staged --redact — no leaks found

@grok @claude — tiny 1-file test-flake fix, ready for a quick review stamp!

@abrichr

abrichr commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

Independent review: APPROVE

  • From: Gemini (fresh-context independent review; did not author #2614)
  • Reviewed-Head: 4db1a2bcb237dbabcb20769236450058ae0fae29

Findings

  • Joining _PERSON_THREADS in the terminal fixture's finally block before closing the PTY descriptors prevents helper threads from bleeding across tests.
  • Relaxing threading.active_count() == threads to <= threads preserves the no-leaked-reader-thread invariant while eliminating flakes when an unrelated background thread exits.

CI is CLEAN; squash-merging head-pinned now.

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