Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/google/adk/tools/mcp_tool/mcp_session_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,10 +434,9 @@ async def close(self):
await exit_stack.aclose()
except Exception as e:
# Log the error but don't re-raise to avoid blocking shutdown
print(
'Warning: Error during MCP session cleanup for'
f' {session_key}: {e}',
file=self._errlog,
logger.warning(
f'Error during MCP session cleanup for {session_key}',
exc_info=True,
)
finally:
del self._sessions[session_key]
Expand Down
15 changes: 7 additions & 8 deletions tests/unittests/tools/mcp_tool/test_mcp_session_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,8 @@ async def test_close_success(self):
assert len(manager._sessions) == 0

@pytest.mark.asyncio
async def test_close_with_errors(self):
@patch("google.adk.tools.mcp_tool.mcp_session_manager.logger")
async def test_close_with_errors(self, mock_logger):
"""Test cleanup when some sessions fail to close."""
manager = MCPSessionManager(self.mock_stdio_connection_params)

Expand All @@ -403,20 +404,18 @@ async def test_close_with_errors(self):
manager._sessions["session1"] = (session1, exit_stack1)
manager._sessions["session2"] = (session2, exit_stack2)

custom_errlog = StringIO()
manager._errlog = custom_errlog

# Should not raise exception
await manager.close()

# Good session should still be closed
exit_stack2.aclose.assert_called_once()
assert len(manager._sessions) == 0

# Error should be logged
error_output = custom_errlog.getvalue()
assert "Warning: Error during MCP session cleanup" in error_output
assert "Close error 1" in error_output
# Error should be logged via logger.warning
mock_logger.warning.assert_called_once()
args, kwargs = mock_logger.warning.call_args
assert "Error during MCP session cleanup for session1" in args[0]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For a more precise test, it's better to assert the exact log message instead of using in. This ensures that the log message is exactly as expected and prevents accidental changes from going unnoticed.

Suggested change
assert "Error during MCP session cleanup for session1" in args[0]
assert "Error during MCP session cleanup for session1" == args[0]

assert kwargs.get("exc_info")

@pytest.mark.asyncio
@patch("google.adk.tools.mcp_tool.mcp_session_manager.stdio_client")
Expand Down
Loading