mirror of
https://github.com/langgenius/dify.git
synced 2025-07-31 21:36:23 +00:00

The `BaseSession` class in the `core/mcp/session` package uses `ThreadPoolExecutor` to run the receive loop but fails to properly clean up the executor and receiver future, leading to potential thread leaks. This PR addresses this issue by: - Initializing `_executor` and `_receiver_future` attributes to `None` for proper cleanup checks - Adding graceful shutdown with a 5-second timeout in the `__exit__` method - Ensuring the ThreadPoolExecutor is properly shut down to prevent resource leaks This fix prevents memory leaks and hanging threads in long-running scenarios where multiple MCP sessions are created and destroyed. Signed-off-by: neatguycoding <15627489+NeatGuyCoding@users.noreply.github.com> Co-authored-by: QuantumGhost <obelisk.reg+git@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>