mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-07-03 01:01:56 +08:00
### What problem does this PR solve? Closes #12962 MCPToolCallSessions created during agent execution (in `Agent.__init__`) are never explicitly closed. Each session starts its own event loop thread and opens an SSE/HTTP connection to the MCP server. When the canvas goes out of scope, these threads and connections remain alive indefinitely, accumulating over time and causing resource exhaustion after prolonged use. ### Solution 1. Add a `Graph.close()` method that iterates all components, finds MCPToolCallSessions held by Agent tools, and calls `close_sync()` on each to properly shut down the event loop, thread, and connection. 2. Call `canvas.close()` in `finally` blocks after `canvas.run()` completes in `canvas_service.py` and `canvas_app.py`. 3. Move MCP session cleanup to `finally` blocks in `test_tool` endpoint (`mcp_server_app.py`) and `get_mcp_tools` (`api_utils.py`) to ensure sessions are closed even on exceptions. ### Type of change - [x] Bug Fix (non-breaking change which fixes an issue) --------- Co-authored-by: conflict-resolver <conflict-resolver@local> Co-authored-by: Zhichang Yu <yuzhichang@gmail.com>