Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(api): Fix potential thread leak in MCP `BaseSession` (#22169)

NeatGuyCoding7bf3d2c8bf57
diff --git a/api/core/mcp/session/base_session.py b/api/core/mcp/session/base_session.py
index 1c0f582501d..7734b8fdd99 100644
--- a/api/core/mcp/session/base_session.py
+++ b/api/core/mcp/session/base_session.py
@@ -1,7 +1,7 @@
import logging
import queue
from collections.abc import Callable
-from concurrent.futures import ThreadPoolExecutor
+from concurrent.futures import Future, ThreadPoolExecutor, TimeoutError
from contextlib import ExitStack
from datetime import timedelta
from types import TracebackType
@@ -171,23 +171,41 @@ class BaseSession(
self._session_read_timeout_seconds = read_timeout_seconds
self._in_flight = {}
self._exit_stack = ExitStack()
+ # Initialize executor and future to None for proper cleanup checks
+ self._executor: ThreadPoolExecutor | None = None
+ self._receiver_future: Future | None = None
def __enter__(self) -> Self:
- self._executor = ThreadPoolExecutor()
+ # The thread pool is dedicated to running `_receive_loop`. Setting `max_workers` to 1
+ # ensures no unnecessary threads are created.
+ self._executor = ThreadPoolExecutor(max_workers=1)
self._receiver_future = self._executor.submit(self._receive_loop)
return self
def check_receiver_status(self) -> None:
- if self._receiver_future.done():
+ """`check_receiver_status` ensures that any exceptions raised during the
+ execution of `_receive_loop` are retrieved and propagated."""
+ if self._receiver_future and self._receiver_future.done():
self._receiver_future.result()
def __exit__(
self, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None
) -> None:
- self._exit_stack.close()
self._read_stream.put(None)
self._write_stream.put(None)
+ # Wait for the receiver loop to finish
+ if self._receiver_future:
+ try:
+ self._receiver_future.result(timeout=5.0) # Wait up to 5 seconds
+ except TimeoutError:
+ # If the receiver loop is still running after timeout, we'll force shutdown
+ pass
+
+ # Shutdown the executor
+ if self._executor:
+ self._executor.shutdown(wait=True)
+
def send_request(
self,
request: SendRequestT,