Skip to content

fix: mutmut の clean test 失敗を修正(グローバル event loop 汚染の除去)#460

Merged
yusuke0610 merged 1 commit into
mainfrom
claude/mutmut-runtime-error-7mckoh
Jul 2, 2026
Merged

fix: mutmut の clean test 失敗を修正(グローバル event loop 汚染の除去)#460
yusuke0610 merged 1 commit into
mainfrom
claude/mutmut-runtime-error-7mckoh

Conversation

@yusuke0610

@yusuke0610 yusuke0610 commented Jul 2, 2026

Copy link
Copy Markdown
Owner

変更概要

make mutation-backendmutmut run)がミューテーション本体に入る前の clean test フェーズで RuntimeError: There is no current event loop により落ち、Failed to run clean testexit(1) していた問題を修正する。原因は tests/security/test_ssrf_github.py_run() だけがグローバル asyncio event loop を操作していたこと。他 11 個の _run と同じ「ローカル loop を作って閉じるだけ」の分離パターンに統一し、mutmut の同一プロセス反復実行に対して冪等にする。

原因(詳細)

mutmut 3.x は 同一 Python プロセス内で pytest スイートを複数回実行する(stats 収集 → clean test 検証 → ミュータント実行)。test_llm_clients.pyasyncio.run() が終了時にグローバル loop を None へ戻す(_set_called=True のまま)ため、その壊れた状態が 2 周目のスイート実行に持ち越される。2 周目に SSRF テストの _run()asyncio.get_event_loop() を呼ぶと RuntimeError: There is no current event loop を送出し、with pytest.raises(NonRetryableError) の外へ抜けて test_fetch_repos_raw_rejects_bad_username_without_http が失敗していた。

  • 全 12 個の _run ヘルパのうち、グローバル loop を触るのは本ファイルの 1 つのみ(唯一の汚染源かつ被害者)。
  • 再発防止として .claude/rules/backend/test.md のアンチパターンに「テストでグローバル event loop を触らない」を追記。

セルフレビューチェックリスト

必須確認

  • make ci 相当を確認(※本セッション環境に nix が無いため公式 make は未実行。同等の venv で ruff clean / フルスイート 667 passed / 同一プロセス 2 回実行で修正前=2 回目 1 failed→修正後=両方 667 passed を確認。実環境では make ci / make mutation-backend での最終確認を推奨)
  • コメント・ドキュメント・エラーメッセージは日本語で記述した

条件付き確認

  • app/schemas/ または app/routers/ の変更: N/A(テストとルール文書のみ、OpenAPI へ影響なし)
  • ページ・認証・ナビ・レイアウト変更: N/A
  • 新規環境変数: N/A
  • web/src/ の日本語メッセージ: N/A(web 変更なし)

破壊的変更

  • 破壊的変更なし(プロダクトコード app/ 無変更。テストの意図・API 契約・DB スキーマに変更なし)

ADR

  • N/A(新規ライブラリ採用・アーキテクチャ変更なし)

検証

実行形態 修正前 修正後
フルスイート 1 回(別プロセス) 667 passed 667 passed
同一プロセスでフルスイート 2 回(mutmut の stats→clean を再現) 2 回目 1 failed 両方 667 passed
ruff(変更ファイル) All checks passed

🤖 Generated with Claude Code


Generated by Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Improved test stability by isolating async event loop usage, reducing intermittent failures in repeated test runs.
    • Prevented leftover event loop state from causing runtime errors in clean test sessions.
  • Documentation

    • Added guidance for safer async testing practices, including using local event loops and closing them properly.

mutmut 3.x は同一プロセスで pytest スイートを複数回実行する(stats 収集 →
clean test 検証 → ミュータント実行)。tests/security/test_ssrf_github.py の
_run() だけがグローバル asyncio event loop を set/get していたため、
test_llm_clients.py の asyncio.run() が残したグローバル loop の壊れ状態が
2 周目に持ち越され、get_event_loop() が
"RuntimeError: There is no current event loop" を送出。これで
`make mutation-backend` が clean test 失敗(Failed to run clean test)で
exit(1) していた。

_run() を他 11 個の _run と同じ「ローカル loop を作って閉じるだけ」の分離
パターンに統一し、反復実行に対して冪等にする。再発防止として
.claude/rules/backend/test.md のアンチパターンに追記。

- プロダクトコード(app/)は無変更。テストの意図(不正 username を HTTP
  発行前に弾く)も不変。
- 検証: フルスイートを同一プロセスで 2 回実行し、修正前=2 回目 1 failed →
  修正後=両方 667 passed を確認。ruff clean。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SsRavDhjAWMQby1g6gHVyR
@github-actions github-actions Bot added bug バグ修正 documentation Improvements or additions to documentation backend バックエンド test テスト追加・修正 labels Jul 2, 2026
@coderabbitai

coderabbitai Bot commented Jul 2, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: fbf7371d-cf50-4cad-a2ee-3498bfbc0f43

📥 Commits

Reviewing files that changed from the base of the PR and between 00fdaaf and 2f0cb14.

📒 Files selected for processing (2)
  • .claude/rules/backend/test.md
  • backend/tests/security/test_ssrf_github.py

📝 Walkthrough

Walkthrough

A test helper in test_ssrf_github.py was changed to create and close a local asyncio event loop instead of mutating the global event loop. The backend test guidelines document was updated to document this pattern as the recommended anti-pattern avoidance for mutmut-repeated test runs.

Changes

Event Loop Isolation Fix

Layer / File(s) Summary
Local event loop in test helper
backend/tests/security/test_ssrf_github.py
The _run helper now creates a local event loop via asyncio.new_event_loop(), runs the coroutine with run_until_complete, and closes it in a finally block, instead of saving/restoring the global event loop via set_event_loop/get_event_loop.
Test guideline documentation
.claude/rules/backend/test.md
Adds an anti-pattern note warning against touching the global event loop in tests, citing potential RuntimeError failures under repeated mutmut runs, and recommending the local event loop pattern.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Possibly related PRs

  • yusuke0610/devforge#293: Prior change to the same _run helper's custom event-loop handling in test_ssrf_github.py.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: fixing mutmut clean test failures by removing global event loop pollution.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch claude/mutmut-runtime-error-7mckoh

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@yusuke0610
yusuke0610 merged commit 289f664 into main Jul 2, 2026
24 checks passed
@yusuke0610
yusuke0610 deleted the claude/mutmut-runtime-error-7mckoh branch July 20, 2026 12:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend バックエンド bug バグ修正 documentation Improvements or additions to documentation test テスト追加・修正

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants