-
Notifications
You must be signed in to change notification settings - Fork 423
Resolve cancel scope error in MCP session cleanup with lifetime task #931
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Resolve cancel scope error in MCP session cleanup with lifetime task #931
Conversation
Signed-off-by: Anuradha Karuppiah <[email protected]>
Signed-off-by: Anuradha Karuppiah <[email protected]>
Signed-off-by: Anuradha Karuppiah <[email protected]>
Signed-off-by: Anuradha Karuppiah <[email protected]>
Signed-off-by: Anuradha Karuppiah <[email protected]>
Signed-off-by: Anuradha Karuppiah <[email protected]>
Signed-off-by: Anuradha Karuppiah <[email protected]>
Signed-off-by: Anuradha Karuppiah <[email protected]>
Signed-off-by: Anuradha Karuppiah <[email protected]>
Signed-off-by: Anuradha Karuppiah <[email protected]>
Signed-off-by: Anuradha Karuppiah <[email protected]>
Signed-off-by: Anuradha Karuppiah <[email protected]>
Signed-off-by: Anuradha Karuppiah <[email protected]>
Signed-off-by: Anuradha Karuppiah <[email protected]>
Signed-off-by: Anuradha Karuppiah <[email protected]>
Signed-off-by: Anuradha Karuppiah <[email protected]>
Signed-off-by: Anuradha Karuppiah <[email protected]>
Signed-off-by: Anuradha Karuppiah <[email protected]>
Signed-off-by: Anuradha Karuppiah <[email protected]>
Signed-off-by: Anuradha Karuppiah <[email protected]>
Signed-off-by: Anuradha Karuppiah <[email protected]>
Co-authored-by: Will Killian <[email protected]> Signed-off-by: Anuradha Karuppiah <[email protected]>
Signed-off-by: Anuradha Karuppiah <[email protected]>
Signed-off-by: Anuradha Karuppiah <[email protected]>
Also add per-session locks for ref counting Signed-off-by: Anuradha Karuppiah <[email protected]>
Signed-off-by: Anuradha Karuppiah <[email protected]>
Signed-off-by: Anuradha Karuppiah <[email protected]>
Signed-off-by: Anuradha Karuppiah <[email protected]>
Signed-off-by: Anuradha Karuppiah <[email protected]>
…k pattern Previously, session cleanup was calling client.__aexit__() from a different task context than where __aenter__() was called, violating anyio's CancelScope requirement that enter and exit must happen in the same task. This caused "Attempted to exit cancel scope in a different task" errors during session cleanup. Solution: - Introduce a per-client lifetime task that manages the entire client lifecycle - The lifetime task enters the client context (async with client:) and waits for a stop_event signal before exiting - Session cleanup now signals the stop_event and waits for the lifetime task to complete, ensuring __aexit__ runs in the correct task context - Add SessionData fields: stop_event (asyncio.Event) and lifetime_task (asyncio.Task) - Update _create_session_client to return (client, stop_event, task) tuple This ensures proper cancel scope handling and prevents resource leaks while maintaining thread-safe session management. Signed-off-by: Anuradha Karuppiah <[email protected]>
WalkthroughSession lifecycle expanded: Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant Caller
participant ClientImpl as MCP ClientImpl
participant Factory as _create_session_client
participant Lifetime as lifetime_task
participant MCP as MCPBaseClient
Caller->>ClientImpl: request session client
ClientImpl->>Factory: _create_session_client(session_id)
activate Factory
Factory->>MCP: instantiate client
Factory->>Lifetime: spawn _lifetime(client, stop_event)
Factory-->>ClientImpl: (client, stop_event, lifetime_task)
deactivate Factory
ClientImpl->>ClientImpl: store SessionData(client, stop_event, lifetime_task)
ClientImpl-->>Caller: return client
Note over Lifetime,MCP: _lifetime runs "async with client", awaits `stop_event`
Caller->>ClientImpl: trigger cleanup inactive sessions
ClientImpl->>ClientImpl: collect SessionData to_close
ClientImpl->>Lifetime: stop_event.set()
alt lifetime_task present
ClientImpl->>Lifetime: await lifetime_task
else no lifetime_task
ClientImpl->>MCP: call client._close() (fallback)
end
Lifetime->>MCP: __aexit__() (client closed)
Lifetime-->>ClientImpl: task completed
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Suggested labels
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🧰 Additional context used📓 Path-based instructions (5)**/*.{py,yaml,yml}📄 CodeRabbit inference engine (.cursor/rules/nat-test-llm.mdc)
Files:
**/*.py📄 CodeRabbit inference engine (.cursor/rules/nat-test-llm.mdc)
Files:
tests/**/*.py📄 CodeRabbit inference engine (.cursor/rules/general.mdc)
Files:
⚙️ CodeRabbit configuration file
Files:
{tests/**/*.py,examples/*/tests/**/*.py}📄 CodeRabbit inference engine (.cursor/rules/general.mdc)
Files:
**/*⚙️ CodeRabbit configuration file
Files:
🧬 Code graph analysis (1)tests/nat/mcp/test_mcp_session_management.py (1)
🪛 Ruff (0.13.3)tests/nat/mcp/test_mcp_session_management.py599-599: Unused function argument: (ARG001) 607-607: Pattern passed to (RUF043) 644-644: Unused function argument: (ARG001) 650-650: Unused function argument: (ARG001) ⏰ 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). (1)
🔇 Additional comments (12)
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. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 3
🧹 Nitpick comments (1)
packages/nvidia_nat_mcp/src/nat/plugins/mcp/client_impl.py (1)
47-49: Consider makinglifetime_tasknon-optional for clarity.The type hint
lifetime_task: asyncio.Task | None = Nonesuggests it's optional, but_create_session_clientalways creates and returns a task (line 377-382). In practice,lifetime_taskshould never beNoneafter session creation. The optional type andNonedefault exist only for dataclass initialization, but could confuse readers about the actual invariants.Consider either:
- Removing the
| Nonefrom the type hint and usingfield(init=False)if a default is needed- Adding a comment explaining that
lifetime_taskis always set during session creation and should never beNonein normal operation</review_comment_end>
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
packages/nvidia_nat_mcp/src/nat/plugins/mcp/client_impl.py(7 hunks)
🧰 Additional context used
📓 Path-based instructions (6)
**/*.{py,yaml,yml}
📄 CodeRabbit inference engine (.cursor/rules/nat-test-llm.mdc)
**/*.{py,yaml,yml}: Configure response_seq as a list of strings; values cycle per call, and [] yields an empty string.
Configure delay_ms to inject per-call artificial latency in milliseconds for nat_test_llm.
Files:
packages/nvidia_nat_mcp/src/nat/plugins/mcp/client_impl.py
**/*.py
📄 CodeRabbit inference engine (.cursor/rules/nat-test-llm.mdc)
**/*.py: Programmatic use: create TestLLMConfig(response_seq=[...], delay_ms=...), add with builder.add_llm("", cfg).
When retrieving the test LLM wrapper, use builder.get_llm(name, wrapper_type=LLMFrameworkEnum.) and call the framework’s method (e.g., ainvoke, achat, call).
**/*.py: In code comments/identifiers use NAT abbreviations as specified: nat for API namespace/CLI, nvidia-nat for package name, NAT for env var prefixes; do not use these abbreviations in documentation
Follow PEP 20 and PEP 8; run yapf with column_limit=120; use 4-space indentation; end files with a single trailing newline
Run ruff check --fix as linter (not formatter) using pyproject.toml config; fix warnings unless explicitly ignored
Respect naming: snake_case for functions/variables, PascalCase for classes, UPPER_CASE for constants
Treat pyright warnings as errors during development
Exception handling: use bare raise to re-raise; log with logger.error() when re-raising to avoid duplicate stack traces; use logger.exception() when catching without re-raising
Provide Google-style docstrings for every public module, class, function, and CLI command; first line concise and ending with a period; surround code entities with backticks
Validate and sanitize all user input, especially in web or CLI interfaces
Prefer httpx with SSL verification enabled by default and follow OWASP Top-10 recommendations
Use async/await for I/O-bound work; profile CPU-heavy paths with cProfile or mprof before optimizing; cache expensive computations with functools.lru_cache or external cache; leverage NumPy vectorized operations when beneficial
Files:
packages/nvidia_nat_mcp/src/nat/plugins/mcp/client_impl.py
packages/*/src/**/*.py
📄 CodeRabbit inference engine (.cursor/rules/general.mdc)
Importable Python code inside packages must live under packages//src/
Files:
packages/nvidia_nat_mcp/src/nat/plugins/mcp/client_impl.py
{src/**/*.py,packages/*/src/**/*.py}
📄 CodeRabbit inference engine (.cursor/rules/general.mdc)
All public APIs must have Python 3.11+ type hints on parameters and return values; prefer typing/collections.abc abstractions; use typing.Annotated when useful
Files:
packages/nvidia_nat_mcp/src/nat/plugins/mcp/client_impl.py
**/*
⚙️ CodeRabbit configuration file
**/*: # Code Review Instructions
- Ensure the code follows best practices and coding standards. - For Python code, follow
PEP 20 and
PEP 8 for style guidelines.- Check for security vulnerabilities and potential issues. - Python methods should use type hints for all parameters and return values.
Example:def my_function(param1: int, param2: str) -> bool: pass- For Python exception handling, ensure proper stack trace preservation:
- When re-raising exceptions: use bare
raisestatements to maintain the original stack trace,
and uselogger.error()(notlogger.exception()) to avoid duplicate stack trace output.- When catching and logging exceptions without re-raising: always use
logger.exception()
to capture the full stack trace information.Documentation Review Instructions - Verify that documentation and comments are clear and comprehensive. - Verify that the documentation doesn't contain any TODOs, FIXMEs or placeholder text like "lorem ipsum". - Verify that the documentation doesn't contain any offensive or outdated terms. - Verify that documentation and comments are free of spelling mistakes, ensure the documentation doesn't contain any
words listed in the
ci/vale/styles/config/vocabularies/nat/reject.txtfile, words that might appear to be
spelling mistakes but are listed in theci/vale/styles/config/vocabularies/nat/accept.txtfile are OK.Misc. - All code (except .mdc files that contain Cursor rules) should be licensed under the Apache License 2.0,
and should contain an Apache License 2.0 header comment at the top of each file.
- Confirm that copyright years are up-to date whenever a file is changed.
Files:
packages/nvidia_nat_mcp/src/nat/plugins/mcp/client_impl.py
packages/**/*
⚙️ CodeRabbit configuration file
packages/**/*: - This directory contains optional plugin packages for the toolkit, each should contain apyproject.tomlfile. - Thepyproject.tomlfile should declare a dependency onnvidia-nator another package with a name starting
withnvidia-nat-. This dependency should be declared using~=<version>, and the version should be a two
digit version (ex:~=1.0).
- Not all packages contain Python code, if they do they should also contain their own set of tests, in a
tests/directory at the same level as thepyproject.tomlfile.
Files:
packages/nvidia_nat_mcp/src/nat/plugins/mcp/client_impl.py
🧬 Code graph analysis (1)
packages/nvidia_nat_mcp/src/nat/plugins/mcp/client_impl.py (2)
packages/nvidia_nat_mcp/src/nat/plugins/mcp/utils.py (1)
truncate_session_id(24-37)packages/nvidia_nat_mcp/src/nat/plugins/mcp/client_base.py (1)
MCPBaseClient(128-407)
⏰ 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). (1)
- GitHub Check: CI Pipeline / Check
🔇 Additional comments (2)
packages/nvidia_nat_mcp/src/nat/plugins/mcp/client_impl.py (2)
297-304: LGTM!The unpacking of the triple return value and initialization of
SessionDatacorrectly handles the new session lifecycle management pattern.</review_comment_end>
342-342: LGTM!The function signature correctly reflects the new return type with proper type hints as required by the coding guidelines.
</review_comment_end>
Signed-off-by: Anuradha Karuppiah <[email protected]>
Signed-off-by: Anuradha Karuppiah <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (1)
packages/nvidia_nat_mcp/src/nat/plugins/mcp/client_impl.py (1)
370-413: Excellent implementation of lifetime task pattern!The lifetime task implementation correctly solves the cancel scope violation by ensuring
__aenter__and__aexit__occur in the same task context. Key strengths:
Prevents initialization hang: The try/except block (lines 378-384) ensures
ready.set()is called even if__aenter__fails, preventing indefinite blocking at line 391.Timeout protection: Initialization waits with a timeout (lines 388-401), preventing resource leaks from stuck clients.
Failure detection: Lines 404-409 properly detect and propagate initialization failures by checking if the task completed before entering the wait state.
Session ID privacy: Task name uses
truncate_session_id()(line 386) to avoid exposing sensitive information in debugging tools.Optional refinement: At line 401, consider using
from Noneto clarify the timeout is a new error context unrelated to any caught exception:raise RuntimeError(f"Session client initialization timed out after {timeout}s") + # Or more explicitly: + raise RuntimeError(f"Session client initialization timed out after {timeout}s") from None
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
packages/nvidia_nat_mcp/src/nat/plugins/mcp/client_impl.py(7 hunks)
🧰 Additional context used
📓 Path-based instructions (6)
**/*.{py,yaml,yml}
📄 CodeRabbit inference engine (.cursor/rules/nat-test-llm.mdc)
**/*.{py,yaml,yml}: Configure response_seq as a list of strings; values cycle per call, and [] yields an empty string.
Configure delay_ms to inject per-call artificial latency in milliseconds for nat_test_llm.
Files:
packages/nvidia_nat_mcp/src/nat/plugins/mcp/client_impl.py
**/*.py
📄 CodeRabbit inference engine (.cursor/rules/nat-test-llm.mdc)
**/*.py: Programmatic use: create TestLLMConfig(response_seq=[...], delay_ms=...), add with builder.add_llm("", cfg).
When retrieving the test LLM wrapper, use builder.get_llm(name, wrapper_type=LLMFrameworkEnum.) and call the framework’s method (e.g., ainvoke, achat, call).
**/*.py: In code comments/identifiers use NAT abbreviations as specified: nat for API namespace/CLI, nvidia-nat for package name, NAT for env var prefixes; do not use these abbreviations in documentation
Follow PEP 20 and PEP 8; run yapf with column_limit=120; use 4-space indentation; end files with a single trailing newline
Run ruff check --fix as linter (not formatter) using pyproject.toml config; fix warnings unless explicitly ignored
Respect naming: snake_case for functions/variables, PascalCase for classes, UPPER_CASE for constants
Treat pyright warnings as errors during development
Exception handling: use bare raise to re-raise; log with logger.error() when re-raising to avoid duplicate stack traces; use logger.exception() when catching without re-raising
Provide Google-style docstrings for every public module, class, function, and CLI command; first line concise and ending with a period; surround code entities with backticks
Validate and sanitize all user input, especially in web or CLI interfaces
Prefer httpx with SSL verification enabled by default and follow OWASP Top-10 recommendations
Use async/await for I/O-bound work; profile CPU-heavy paths with cProfile or mprof before optimizing; cache expensive computations with functools.lru_cache or external cache; leverage NumPy vectorized operations when beneficial
Files:
packages/nvidia_nat_mcp/src/nat/plugins/mcp/client_impl.py
packages/*/src/**/*.py
📄 CodeRabbit inference engine (.cursor/rules/general.mdc)
Importable Python code inside packages must live under packages//src/
Files:
packages/nvidia_nat_mcp/src/nat/plugins/mcp/client_impl.py
{src/**/*.py,packages/*/src/**/*.py}
📄 CodeRabbit inference engine (.cursor/rules/general.mdc)
All public APIs must have Python 3.11+ type hints on parameters and return values; prefer typing/collections.abc abstractions; use typing.Annotated when useful
Files:
packages/nvidia_nat_mcp/src/nat/plugins/mcp/client_impl.py
**/*
⚙️ CodeRabbit configuration file
**/*: # Code Review Instructions
- Ensure the code follows best practices and coding standards. - For Python code, follow
PEP 20 and
PEP 8 for style guidelines.- Check for security vulnerabilities and potential issues. - Python methods should use type hints for all parameters and return values.
Example:def my_function(param1: int, param2: str) -> bool: pass- For Python exception handling, ensure proper stack trace preservation:
- When re-raising exceptions: use bare
raisestatements to maintain the original stack trace,
and uselogger.error()(notlogger.exception()) to avoid duplicate stack trace output.- When catching and logging exceptions without re-raising: always use
logger.exception()
to capture the full stack trace information.Documentation Review Instructions - Verify that documentation and comments are clear and comprehensive. - Verify that the documentation doesn't contain any TODOs, FIXMEs or placeholder text like "lorem ipsum". - Verify that the documentation doesn't contain any offensive or outdated terms. - Verify that documentation and comments are free of spelling mistakes, ensure the documentation doesn't contain any
words listed in the
ci/vale/styles/config/vocabularies/nat/reject.txtfile, words that might appear to be
spelling mistakes but are listed in theci/vale/styles/config/vocabularies/nat/accept.txtfile are OK.Misc. - All code (except .mdc files that contain Cursor rules) should be licensed under the Apache License 2.0,
and should contain an Apache License 2.0 header comment at the top of each file.
- Confirm that copyright years are up-to date whenever a file is changed.
Files:
packages/nvidia_nat_mcp/src/nat/plugins/mcp/client_impl.py
packages/**/*
⚙️ CodeRabbit configuration file
packages/**/*: - This directory contains optional plugin packages for the toolkit, each should contain apyproject.tomlfile. - Thepyproject.tomlfile should declare a dependency onnvidia-nator another package with a name starting
withnvidia-nat-. This dependency should be declared using~=<version>, and the version should be a two
digit version (ex:~=1.0).
- Not all packages contain Python code, if they do they should also contain their own set of tests, in a
tests/directory at the same level as thepyproject.tomlfile.
Files:
packages/nvidia_nat_mcp/src/nat/plugins/mcp/client_impl.py
🧬 Code graph analysis (1)
packages/nvidia_nat_mcp/src/nat/plugins/mcp/client_impl.py (2)
packages/nvidia_nat_mcp/src/nat/plugins/mcp/utils.py (1)
truncate_session_id(24-37)packages/nvidia_nat_mcp/src/nat/plugins/mcp/client_base.py (2)
MCPBaseClient(128-407)name(598-600)
🪛 Ruff (0.13.3)
packages/nvidia_nat_mcp/src/nat/plugins/mcp/client_impl.py
398-400: Use logging.exception instead of logging.error
Replace with exception
(TRY400)
401-401: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling
(B904)
401-401: Avoid specifying long messages outside the exception class
(TRY003)
408-408: Use logging.exception instead of logging.error
Replace with exception
(TRY400)
409-409: Avoid specifying long messages outside the exception class
(TRY003)
🔇 Additional comments (3)
packages/nvidia_nat_mcp/src/nat/plugins/mcp/client_impl.py (3)
47-49: LGTM! Lifetime task fields properly integrated.The
stop_eventandlifetime_taskfields are correctly added with appropriate defaults. The Event factory ensures each SessionData instance gets its own event object, andlifetime_taskbeing optional handles cases where it hasn't been created yet.
239-256: LGTM! Cleanup properly respects cancel scope boundaries.The cleanup logic correctly addresses the core issue by:
- Signaling
stop_eventto trigger graceful shutdown in the lifetime task- Awaiting the lifetime task to ensure
__aexit__runs in the same task that called__aenter__- Including fallback handling for edge cases where the lifetime task is missing
This prevents the "Attempted to exit cancel scope in a different task" error while maintaining thread-safe session management.
302-309: LGTM! Session creation properly wired.The unpacking and SessionData initialization correctly handles the new return type from
_create_session_client, storing all lifecycle components needed for proper cleanup.
Signed-off-by: Anuradha Karuppiah <[email protected]>
Signed-off-by: Anuradha Karuppiah <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
tests/nat/mcp/test_mcp_session_management.py(12 hunks)
🧰 Additional context used
📓 Path-based instructions (5)
**/*.{py,yaml,yml}
📄 CodeRabbit inference engine (.cursor/rules/nat-test-llm.mdc)
**/*.{py,yaml,yml}: Configure response_seq as a list of strings; values cycle per call, and [] yields an empty string.
Configure delay_ms to inject per-call artificial latency in milliseconds for nat_test_llm.
Files:
tests/nat/mcp/test_mcp_session_management.py
**/*.py
📄 CodeRabbit inference engine (.cursor/rules/nat-test-llm.mdc)
**/*.py: Programmatic use: create TestLLMConfig(response_seq=[...], delay_ms=...), add with builder.add_llm("", cfg).
When retrieving the test LLM wrapper, use builder.get_llm(name, wrapper_type=LLMFrameworkEnum.) and call the framework’s method (e.g., ainvoke, achat, call).
**/*.py: In code comments/identifiers use NAT abbreviations as specified: nat for API namespace/CLI, nvidia-nat for package name, NAT for env var prefixes; do not use these abbreviations in documentation
Follow PEP 20 and PEP 8; run yapf with column_limit=120; use 4-space indentation; end files with a single trailing newline
Run ruff check --fix as linter (not formatter) using pyproject.toml config; fix warnings unless explicitly ignored
Respect naming: snake_case for functions/variables, PascalCase for classes, UPPER_CASE for constants
Treat pyright warnings as errors during development
Exception handling: use bare raise to re-raise; log with logger.error() when re-raising to avoid duplicate stack traces; use logger.exception() when catching without re-raising
Provide Google-style docstrings for every public module, class, function, and CLI command; first line concise and ending with a period; surround code entities with backticks
Validate and sanitize all user input, especially in web or CLI interfaces
Prefer httpx with SSL verification enabled by default and follow OWASP Top-10 recommendations
Use async/await for I/O-bound work; profile CPU-heavy paths with cProfile or mprof before optimizing; cache expensive computations with functools.lru_cache or external cache; leverage NumPy vectorized operations when beneficial
Files:
tests/nat/mcp/test_mcp_session_management.py
tests/**/*.py
📄 CodeRabbit inference engine (.cursor/rules/general.mdc)
Unit tests reside under tests/ and should use markers defined in pyproject.toml (e.g., integration)
Files:
tests/nat/mcp/test_mcp_session_management.py
⚙️ CodeRabbit configuration file
tests/**/*.py: - Ensure that tests are comprehensive, cover edge cases, and validate the functionality of the code. - Test functions should be named using thetest_prefix, using snake_case. - Any frequently repeated code should be extracted into pytest fixtures. - Pytest fixtures should define the name argument when applying the pytest.fixture decorator. The fixture
function being decorated should be named using thefixture_prefix, using snake_case. Example:
@pytest.fixture(name="my_fixture")
def fixture_my_fixture():
pass
Files:
tests/nat/mcp/test_mcp_session_management.py
{tests/**/*.py,examples/*/tests/**/*.py}
📄 CodeRabbit inference engine (.cursor/rules/general.mdc)
{tests/**/*.py,examples/*/tests/**/*.py}: Use pytest (with pytest-asyncio for async); name test files test_*.py; test functions start with test_; extract repeated code into fixtures; fixtures must set name in decorator and be named with fixture_ prefix
Mock external services with pytest_httpserver or unittest.mock; do not hit live endpoints
Mark expensive tests with @pytest.mark.slow or @pytest.mark.integration
Files:
tests/nat/mcp/test_mcp_session_management.py
**/*
⚙️ CodeRabbit configuration file
**/*: # Code Review Instructions
- Ensure the code follows best practices and coding standards. - For Python code, follow
PEP 20 and
PEP 8 for style guidelines.- Check for security vulnerabilities and potential issues. - Python methods should use type hints for all parameters and return values.
Example:def my_function(param1: int, param2: str) -> bool: pass- For Python exception handling, ensure proper stack trace preservation:
- When re-raising exceptions: use bare
raisestatements to maintain the original stack trace,
and uselogger.error()(notlogger.exception()) to avoid duplicate stack trace output.- When catching and logging exceptions without re-raising: always use
logger.exception()
to capture the full stack trace information.Documentation Review Instructions - Verify that documentation and comments are clear and comprehensive. - Verify that the documentation doesn't contain any TODOs, FIXMEs or placeholder text like "lorem ipsum". - Verify that the documentation doesn't contain any offensive or outdated terms. - Verify that documentation and comments are free of spelling mistakes, ensure the documentation doesn't contain any
words listed in the
ci/vale/styles/config/vocabularies/nat/reject.txtfile, words that might appear to be
spelling mistakes but are listed in theci/vale/styles/config/vocabularies/nat/accept.txtfile are OK.Misc. - All code (except .mdc files that contain Cursor rules) should be licensed under the Apache License 2.0,
and should contain an Apache License 2.0 header comment at the top of each file.
- Confirm that copyright years are up-to date whenever a file is changed.
Files:
tests/nat/mcp/test_mcp_session_management.py
🧬 Code graph analysis (1)
tests/nat/mcp/test_mcp_session_management.py (1)
packages/nvidia_nat_mcp/src/nat/plugins/mcp/client_impl.py (6)
SessionData(40-49)cleanup_sessions(186-199)_create_session_client(347-413)_cleanup_inactive_sessions(201-256)_get_session_client(258-314)_session_usage_context(317-345)
🪛 Ruff (0.13.3)
tests/nat/mcp/test_mcp_session_management.py
599-599: Unused function argument: self
(ARG001)
607-607: Pattern passed to match= contains metacharacters but is neither escaped nor raw
(RUF043)
644-644: Unused function argument: self
(ARG001)
650-650: Unused function argument: self
(ARG001)
⏰ 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). (1)
- GitHub Check: CI Pipeline / Check
🔇 Additional comments (5)
tests/nat/mcp/test_mcp_session_management.py (5)
34-49: LGTM! Well-structured test cleanup helper.The cleanup logic properly handles the lifetime task lifecycle: signals stop_event, waits with timeout, and gracefully handles cancellation. The defensive
hasattrchecks ensure robustness.
126-127: Good test hygiene with cleanup calls.Adding explicit cleanup at the end of tests that create sessions ensures proper resource management and prevents leaks between tests.
Also applies to: 147-148, 173-174, 198-199, 250-251, 280-281, 307-308, 363-364, 413-414, 453-454, 551-552
554-580: LGTM! Comprehensive test of successful initialization.The test properly verifies the lifetime task is created, running, and that
__aenter__is called. The cleanup sequence (set stop_event, await task) is correct.
582-591: LGTM! Proper failure handling test.The test correctly verifies that initialization failures are propagated with appropriate error messages.
610-844: Excellent comprehensive test coverage for lifetime task functionality.These tests thoroughly cover:
- Cleanup on stop_event signal
- Cancel scope task boundaries (verifying enter/exit in same task)
- Cleanup with lifetime tasks
- Preservation of active sessions
- Handling of already-completed tasks
- Complete session lifecycle
- Multiple independent sessions
The logic is correct, assertions are appropriate, and edge cases are well covered.
Note: Static analysis warnings about unused
selfparameters on lines 644 and 650 are false positives—these are intentional mock method signatures that require the parameter to match the async context manager protocol.
Signed-off-by: Anuradha Karuppiah <[email protected]>
|
/merge |
Description
Previously, session cleanup was calling client.aexit() from a different task context than where aenter() was called, violating anyio's CancelScope requirement that enter and exit must happen in the same task. This caused "Attempted to exit cancel scope in a different task" errors during session cleanup.
Solution:
This ensures proper cancel scope handling and prevents resource leaks while maintaining thread-safe session management.
By Submitting this PR I confirm:
Summary by CodeRabbit
Bug Fixes
Refactor
Chores
Tests