Skip to content

Fix unawaited coroutine warning and treat as test error#2806

Merged
jlowin merged 1 commit intomainfrom
fix-unawaited-coroutine-warning
Jan 7, 2026
Merged

Fix unawaited coroutine warning and treat as test error#2806
jlowin merged 1 commit intomainfrom
fix-unawaited-coroutine-warning

Conversation

@jlowin
Copy link
Copy Markdown
Member

@jlowin jlowin commented Jan 7, 2026

When _await_with_session_monitoring detects that the session task is already done, it raises immediately without closing the passed coroutine. This causes "coroutine was never awaited" warnings during test teardown.

Fix: Call coro.close() before raising to properly clean up the coroutine.

Prevention: Add pytest filterwarnings to treat unawaited coroutine warnings as errors so future occurrences will fail tests rather than emit warnings.

# pyproject.toml
filterwarnings = [
    "error:coroutine .* was never awaited:RuntimeWarning",
    "error:Exception ignored in.*coroutine:pytest.PytestUnraisableExceptionWarning",
]

- Add pytest filterwarnings to fail tests on unawaited coroutine warnings
- Fix _await_with_session_monitoring to close coroutine before raising
  when session task is already done
@marvin-context-protocol marvin-context-protocol Bot added bug Something isn't working. Reports of errors, unexpected behavior, or broken functionality. client Related to the FastMCP client SDK or client-side functionality. tests labels Jan 7, 2026
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jan 7, 2026

Walkthrough

This PR modifies the _await_with_session_monitoring method in src/fastmcp/client/client.py to add a coro.close() call when a session task has already completed. This ensures coroutines are properly closed to suppress "was never awaited" warnings that could occur when a session task finishes before its coroutine is awaited.

Possibly related PRs

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: fixing an unawaited coroutine warning and configuring pytest to treat it as a test error.
Description check ✅ Passed The description provides a clear problem statement, the fix applied, and prevention measures with specific code examples. However, it is missing the required Contributors and Review Checklist sections from the template.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

📜 Recent review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between befaad8 and dbcb677.

⛔ Files ignored due to path filters (1)
  • pyproject.toml is excluded by none and included by none
📒 Files selected for processing (1)
  • src/fastmcp/client/client.py
🧰 Additional context used
📓 Path-based instructions (1)
src/fastmcp/**/*.py

📄 CodeRabbit inference engine (AGENTS.md)

src/fastmcp/**/*.py: Python ≥ 3.10 with full type annotations required
Prioritize readable, understandable code - clarity over cleverness. Avoid obfuscated or confusing patterns even if shorter
Follow existing patterns and maintain consistency in code implementation
Be intentional about re-exports - don't blindly re-export everything to parent namespaces. Core types defining a module's purpose should be exported. Specialized features can live in submodules. Only re-export to fastmcp.* for most fundamental types
Never use bare except - be specific with exception types

Files:

  • src/fastmcp/client/client.py
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (5)
  • GitHub Check: Analyze (python)
  • GitHub Check: Run tests: Python 3.10 on windows-latest
  • GitHub Check: Run tests: Python 3.10 on ubuntu-latest
  • GitHub Check: Run tests: Python 3.13 on ubuntu-latest
  • GitHub Check: Run tests with lowest-direct dependencies
🔇 Additional comments (1)
src/fastmcp/client/client.py (1)

689-690: LGTM! Proper coroutine cleanup prevents runtime warnings.

The fix correctly calls coro.close() to clean up the coroutine when the session task has already completed. This prevents the "coroutine was never awaited" warning since the coroutine cannot be awaited after the session task is done. The change is minimal, well-commented, and follows Python's coroutine lifecycle best practices.


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 and usage tips.

@jlowin jlowin merged commit da965db into main Jan 7, 2026
13 checks passed
@jlowin jlowin deleted the fix-unawaited-coroutine-warning branch January 7, 2026 19:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working. Reports of errors, unexpected behavior, or broken functionality. client Related to the FastMCP client SDK or client-side functionality. tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant