Skip to content

Test lifespan fires once per process over HTTP - #4480

Merged
jlowin merged 4 commits into
mainfrom
feature/lifespan-through-sdk-hook
Jul 17, 2026
Merged

Test lifespan fires once per process over HTTP#4480
jlowin merged 4 commits into
mainfrom
feature/lifespan-through-sdk-hook

Conversation

@jlowin

@jlowin jlowin commented Jul 9, 2026

Copy link
Copy Markdown
Member

#4446 drove the FastMCP lifespan through the SDK session manager, making the user lifespan fire once per process instead of once per session — but the only test asserting once-per-process semantics ran over the in-memory transport, not the HTTP path where the SDK manager is actually in the loop. This adds a regression test that runs a real HTTP server and asserts, with an enter/exit counter, that the user lifespan enters exactly once and stays open across three sequential and two overlapping client sessions, exiting once at shutdown. If anyone ever reworks the _lifespan_proxy/session-manager wiring, this is the test that catches a silent regression to per-session semantics.

Label: chores

@jlowin jlowin added the v4 label Jul 9, 2026
@marvin-context-protocol marvin-context-protocol Bot added http Related to HTTP transport, networking, or web server functionality. tests enhancements labels Jul 9, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 991336be6f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

def ping() -> str:
return "pong"

async with run_server_async(server, transport="http") as mcp_url:

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.

P2 Badge Exercise the session manager without pre-entering lifespan

This test is meant to catch _lifespan_proxy / StreamableHTTPSessionManager reverting to per-session lifespan behavior, but run_server_async goes through server.run_http_async(), which already wraps uvicorn in async with self._lifespan_manager() (fastmcp_slim/fastmcp/server/mixins/transport.py:322-324). Since _lifespan_proxy is ref-counted and explicitly reuses an existing outer lifespan, a regression where the HTTP session manager enters the proxy once per client session would still keep enter_count == 1 and exit_count == 0 here, so the new regression test can pass while the behavior it claims to guard is broken; serving server.http_app() without the outer manager would exercise the intended path.

Useful? React with 👍 / 👎.

# The session manager entered the lifespan exactly once across every
# session, and the user lifespan was entered once and exited once at
# process shutdown.
assert proxy_enter_count == 1
@jlowin
jlowin merged commit 266c129 into main Jul 17, 2026
15 checks passed
@jlowin
jlowin deleted the feature/lifespan-through-sdk-hook branch July 17, 2026 21:02
@jlowin jlowin added the enhancement Improvement to existing functionality. For issues and smaller PR improvements. label Jul 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Improvement to existing functionality. For issues and smaller PR improvements. http Related to HTTP transport, networking, or web server functionality. tests v4

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant