Eric Hare 0217f704df fix: serialize concurrent MCP session access to prevent race conditions (#12761)
* fix: serialize concurrent MCP session access to prevent race conditions

Two MCPTools components pointing at the same SSE URL share a single
MCPSessionManager via the component cache. Under concurrent flow
execution (10+ runs against the same server), ~40-50% of runs failed
with one of:

  Error updating tool list: 'streamable_http_<hash>_0'
  Timeout updating tool list: ...
  Error updating tool list: <connection error>

Three races caused this:

1. get_session() was not serialized per-server. Concurrent callers
   iterating self.sessions_by_server[server_key]["sessions"] could both
   pass the health check, then both invoke _cleanup_session_by_id().
2. _cleanup_session_by_id() used del sessions[session_id] in a finally
   block. Two callers that both passed the `if session_id not in
   sessions` guard would race on the delete — the loser raised
   KeyError: 'streamable_http_<hash>_0', matching the reported symptom.
3. Session ids were generated from len(sessions), so removing and
   re-adding sessions could silently produce colliding ids.

Fixes:
- Per-server asyncio.Lock (guarded by a module-level lock for creation)
  serializes session reuse/creation/cleanup.
- _cleanup_session_by_id() now pops the session entry up front; only
  the winning caller runs teardown, the rest no-op.
- Session ids come from a monotonic per-server counter.

Regression tests cover all three races: 10 concurrent get_session
calls must share a single created session; 10 concurrent cleanups
must not raise; session ids must not recycle "_0" after cleanup.

Fixes langflow-ai/langflow#9860

* fix: extend per-server lock to cleanup paths and reclaim per-key maps

Addresses review findings on the previous commit:

HIGH: cleanup paths bypassed the per-server lock. Both
`_cleanup_session(context_id)` (invoked from client disconnect) and
`_cleanup_idle_sessions()` (invoked from the periodic background task)
still mutated `sessions_by_server` without holding the lock. A
concurrent `get_session()` could finish validating a session while
idle-cleanup popped and cancelled its task; `get_session()` then
returned a dead session and registered a dangling refcount entry.

MEDIUM: per-server maps (`_server_locks`, `_session_id_counters`)
grew unboundedly. The previous patch only reclaimed server entries
from `sessions_by_server`; rotating auth/session headers (which
change `server_key` via `_get_server_key`) leaked entries in the
other two maps forever in long-lived processes.

Changes:
- Replace `_get_server_lock()` with `_server_lock()`, an async
  context manager that pin-counts the entry so reclamation can't
  race a task that holds or is about to acquire the lock.
- Wrap the mutating regions of `_cleanup_session()` and
  `_cleanup_idle_sessions()` in `_server_lock(server_key)`.
- On lock release, reclaim both `_server_locks[server_key]` and
  `_session_id_counters[server_key]` once pins drop to 0, the lock
  is unheld, and the server has no remaining sessions.
- `cleanup_all()` also clears the two maps under `_locks_guard`.

New regression tests:
- `test_cleanup_idle_vs_get_session_are_serialized` — an in-flight
  `get_session` blocks a concurrent idle-cleanup pass; the session is
  returned live, not torn down underneath the caller.
- `test_concurrent_cleanup_session_and_get_session_safe` — refcount
  transitions stay consistent across concurrent connect/disconnect.
- `test_server_lock_and_counter_reclaimed_when_unused` — after all
  sessions for a `server_key` are removed, both `_server_locks` and
  `_session_id_counters` entries go away.

* fix: CAS context mapping pop to preserve cross-server handoffs

Addresses the review finding on cross-server reuse of `context_id`:

`_cleanup_session(context_id)` runs under the per-server lock of the
server the mapping pointed at when cleanup started. That lock does
NOT serialize a concurrent `get_session(context_id, different_server)`
which runs under a *different* per-server lock and atomically
re-points `_context_to_session[context_id]` at the new session.

The old cleanup path then ran
`self._context_to_session.pop(context_id, None)` unconditionally,
wiping out the fresh mapping. The new session on the other server
was left with refcount 1 and no context mapping, so the next
disconnect was a no-op and the session leaked indefinitely.

Fix: CAS the pop. After the refcount decrement / teardown, only drop
the mapping if `_context_to_session[context_id]` still points at
`(server_key, session_id)` — the pair we just cleaned up. The
`get()` and `pop()` run synchronously (no `await` between them) so
asyncio cannot interleave another coroutine between the check and
the mutation.

New regression test `test_cleanup_does_not_wipe_cross_server_handoff`
slows down `_cleanup_session_by_id` for server A while a concurrent
`get_session(ctx, serverB)` re-points the context at server B.
Without the CAS, the assertion that `_context_to_session[ctx] ==
(server_B, ...)` fails. With the CAS, the fresh B mapping survives
and its refcount is intact.

* refactor(mcp): address review findings on concurrent-access fix

IMPORTANT
- Replace `dict[str, Any]` with `_ServerLockEntry` TypedDict so the lock/pins
  shape is visible to static analysis (finding #1).
- Add TODO marker above `MCPSessionManager` flagging the module's file-size
  debt as follow-up work (finding #2).

RECOMMENDED
- Replace timing-based `asyncio.sleep(0.02)` / `asyncio.sleep(0.05)` in
  concurrency regression tests with deterministic Event-based rendezvous
  and pin-count polling (finding #3).
- Introduce `_sessions_for(server_key)` helper and use it in
  `_cleanup_idle_sessions`, `_cleanup_session_by_id`, and `cleanup_all` to
  stop reaching through `sessions_by_server[server_key]["sessions"]` at
  every call site (finding #4).
- Document why `_cleanup_session_by_id` keeps a broad `except Exception`
  (transport-layer teardown raises many different exception hierarchies —
  leak-on-cleanup is worse than a swallowed error) (finding #5).
- Log a warning when pin count goes negative in
  `_release_server_lock_if_idle` so a missing acquire / double release
  surfaces in telemetry instead of being silently swept (finding #6).
- Document the "caller must hold `_server_lock(server_key)`" invariant on
  `_next_session_id` (finding #7).

NICE TO HAVE
- Annotate the `_server_lock` async context manager yield type as
  `AsyncIterator[None]` for IDE support (finding #8).

All 175 tests in `test_mcp_util.py` still pass; ruff clean.
2026-04-23 18:25:12 +00:00
2026-04-15 08:41:09 -07:00
2026-04-13 23:23:37 +00:00
2026-04-15 16:11:11 -04:00
2026-04-15 16:11:11 -04:00

Langflow logo

Release Notes PyPI - License PyPI - Downloads Twitter YouTube Channel Discord Server Ask DeepWiki

Langflow is a powerful platform for building and deploying AI-powered agents and workflows. It provides developers with both a visual authoring experience and built-in API and MCP servers that turn every workflow into a tool that can be integrated into applications built on any framework or stack. Langflow comes with batteries included and supports all major LLMs, vector databases and a growing library of AI tools.

Highlight features

  • Visual builder interface to quickly get started and iterate.
  • Source code access lets you customize any component using Python.
  • Interactive playground to immediately test and refine your flows with step-by-step control.
  • Multi-agent orchestration with conversation management and retrieval.
  • Deploy as an API or export as JSON for Python apps.
  • Deploy as an MCP server and turn your flows into tools for MCP clients.
  • Observability with LangSmith, LangFuse and other integrations.
  • Enterprise-ready security and scalability.

🖥️ Langflow Desktop

Langflow Desktop is the easiest way to get started with Langflow. All dependencies are included, so you don't need to manage Python environments or install packages manually. Available for Windows and macOS.

📥 Download Langflow Desktop

Quickstart

Requires Python 3.103.13 and uv (recommended package manager).

Install

From a fresh directory, run:

uv pip install langflow -U

The latest Langflow package is installed. For more information, see Install and run the Langflow OSS Python package.

Run

To start Langflow, run:

uv run langflow run

Langflow starts at http://127.0.0.1:7860.

That's it! You're ready to build with Langflow! 🎉

📦 Other install options

Run from source

If you've cloned this repository and want to contribute, run this command from the repository root:

make run_cli

For more information, see DEVELOPMENT.md.

Docker

Start a Langflow container with default settings:

docker run -p 7860:7860 langflowai/langflow:latest

Langflow is available at http://localhost:7860/. For configuration options, see the Docker deployment guide.

🛡️ Security

For security information, see our Security Policy.

🚀 Deployment

Langflow is completely open source and you can deploy it to all major deployment clouds. To learn how to deploy Langflow, see our Langflow deployment guides.

Stay up-to-date

Star Langflow on GitHub to be instantly notified of new releases.

Star Langflow

👋 Contribute

We welcome contributions from developers of all levels. If you'd like to contribute, please check our contributing guidelines and help make Langflow more accessible.


Star History Chart

❤️ Contributors

langflow contributors

Description
Langflow is a powerful tool for building and deploying AI-powered agents and workflows.
Readme MIT 2.3 GiB
Languages
Python 64.5%
TypeScript 23.4%
JavaScript 11.4%
CSS 0.3%
Makefile 0.2%
Other 0.1%