Skip to content

fix(witan): turn off fastmcp's listen streams in the image - #719

Merged
blarghmatey merged 1 commit into
mainfrom
fastmcp-listen-off
Nov 23, 2026
Merged

blarghmatey merged 1 commit into
mainfrom
fastmcp-listen-off

Conversation

@blarghmatey

Copy link
Copy Markdown
Member

What are the relevant tickets?

N/A. Unblocks #447 (lock maintenance, brings fastmcp 4.2.5). Follow-up spike on using listen: tk-spike-should-witan-push-change-notifications-ove-e2fb08

Description (What does it do?)

fastmcp 4.2.5 serves the 2026-07-28 subscriptions/listen stream by default, and nothing closes an open one on SIGTERM, so one listening client holds a terminating witan pod for the whole 120s shutdown grace. witan publishes no change notifications, so the stream carries nothing. This sets FASTMCP_ENABLE_SUBSCRIPTIONS_LISTEN=false in the image's runtime env, which makes listen answer "Method not found" and stops advertising listChanged, the same as fastmcp 4.1.2 on main today.

Implementation details
  • ListenHandler.close() (mcp/server/subscriptions.py) ends every stream gracefully, but neither fastmcp nor the SDK calls it.
  • uvicorn's Server.shutdown waits up to timeout_graceful_shutdown for open connections and only then runs lifespan shutdown, so a lifespan hook can't close the streams in time.
  • Idle handshake-era SSE GET streams are not affected: sse-starlette (still in 3.7.0) patches uvicorn.Server.handle_exit to end them on SIGTERM. That patch doesn't reach listen streams.
  • fastmcp reads the flag once, when the server is created. Versions before 4.2.5 don't serve listen at all, so the variable is a no-op on main's current lock.

How can this be tested?

Built this image locally with #447's uv.lock (fastmcp 4.2.5, sse-starlette 3.7.0) and WITAN_MCP_SHUTDOWN_GRACE_SECONDS=30, opened a listen stream with fastmcp's Client.listen(), then timed docker stop:

  • flag forced on (-e FASTMCP_ENABLE_SUBSCRIPTIONS_LISTEN=true): listen accepted, stop took 33.1s, uvicorn logged "Cancel 1 running task(s), timeout graceful shutdown exceeded"
  • image default (flag off): listen refused with "Method not found", stop took 1.6s
  • image default with an idle 2025-06-18 SSE GET open instead: stop took 5.0s, no cancellation

On CI, "the image carries the bundle" builds the image; it doesn't exercise shutdown.

https://claude.ai/code/session_01FGZebCxqhMftSqoiPG9Df5

fastmcp 4.2.5 serves the 2026-07-28 `subscriptions/listen` stream by
default, and nothing closes an open one on SIGTERM: uvicorn waits on open
connections before it runs lifespan shutdown. One listening client then
holds a terminating pod for the whole 120s grace window witan sets for
in-flight writes. witan publishes no change notifications, so the stream
carries nothing.

Measured against this image built with fastmcp 4.2.5 and a 30s grace
window: with listen on, `docker stop` took 33.1s and uvicorn cancelled
the stream at the timeout; with the flag off, listen answered "Method not
found" and the stop took 1.6s.

Claude-Session: https://claude.ai/code/session_01FGZebCxqhMftSqoiPG9Df5
Copilot AI balanced review requested due to automatic review settings November 23, 2026 15:54

@agent-optibot agent-optibot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Summary

Disables FastMCP listen streams in the Witan image.


Summary of 5e47407 · React 👍 or 👎 on my comments to tune what I raise · Commands · Review config

@agent-optibot agent-optibot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Status: Code Looks Good 👍

No blocking issues found.

📌 Reviewed up to the latest commit · ⏱️ Review completed in 1 minute

  • 🔄 re-review this PR

@blarghmatey
blarghmatey merged commit ca2693e into main Nov 23, 2026
19 of 20 checks passed
@blarghmatey
blarghmatey deleted the fastmcp-listen-off branch November 23, 2026 16:09

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

2 open issues
What changed in this PR

Disables fastmcp’s subscriptions/listen stream by default in the witan image to prevent termination delays caused by long-lived open listen connections.

Changes:

  • Add FASTMCP_ENABLE_SUBSCRIPTIONS_LISTEN=false to the image runtime environment.
  • Document why the flag is disabled (shutdown behavior, lack of notifications, and compatibility with pre-4.2.5 behavior).
File Description
docker/​witan.Dockerfile Sets the fastmcp env flag to disable listen streams and documents the rationale.

Review effort: Lite

Comment thread docker/witan.Dockerfile
Comment on lines +190 to +197
# FASTMCP_ENABLE_SUBSCRIPTIONS_LISTEN=false: fastmcp 4.2.5 serves the
# 2026-07-28 `subscriptions/listen` stream by default, and nothing closes an
# open one on SIGTERM (uvicorn waits on connections before lifespan shutdown
# runs). One listening client then holds every terminating pod for the whole
# 120s grace window that exists for in-flight writes (DEFAULT_SHUTDOWN_GRACE_SECONDS
# in witan/cli/__init__.py). witan publishes no change notifications, so the
# stream carries nothing; off, `subscriptions/listen` answers METHOD_NOT_FOUND
# and `listChanged` is not advertised, as before 4.2.5. Read at server creation.
Comment thread docker/witan.Dockerfile
Comment on lines +196 to +197
# stream carries nothing; off, `subscriptions/listen` answers METHOD_NOT_FOUND
# and `listChanged` is not advertised, as before 4.2.5. Read at server creation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants