Title: Issue: Sticky Session / Session Affinity Not Working with Azure Web App + FastAPI +FastMCP + Gunicorn · Issue #1350 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: Issue: Sticky Session / Session Affinity Not Working with Azure Web App + FastAPI +FastMCP + Gunicorn · Issue #1350 · modelcontextprotocol/python-sdk
X Title: Issue: Sticky Session / Session Affinity Not Working with Azure Web App + FastAPI +FastMCP + Gunicorn · Issue #1350 · modelcontextprotocol/python-sdk
Description: Question Context I’m running a FastAPI application on Azure Web App (Linux, containerized) using Gunicorn + Uvicorn workers. The app is an MCP (Model Context Protocol) server that connects to Rally via pyral. Because Rally connections ar...
Open Graph Description: Question Context I’m running a FastAPI application on Azure Web App (Linux, containerized) using Gunicorn + Uvicorn workers. The app is an MCP (Model Context Protocol) server that connects to Rally...
X Description: Question Context I’m running a FastAPI application on Azure Web App (Linux, containerized) using Gunicorn + Uvicorn workers. The app is an MCP (Model Context Protocol) server that connects to Rally...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/issues/1350
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Issue: Sticky Session / Session Affinity Not Working with Azure Web App + FastAPI +FastMCP + Gunicorn","articleBody":"### Question\n\n### **Context**\n\nI’m running a FastAPI application on Azure Web App (Linux, containerized) using Gunicorn + Uvicorn workers.\n\nThe app is an MCP (Model Context Protocol) server that connects to Rally via pyral.\nBecause Rally connections are cached per worker, all requests from the same client need to be routed to the same worker.\n\nIf a request lands on a different worker, the Rally API responds with a BAD session ID error because the cached connection is no longer valid.\n\n### **What I Tried**\n\n**1. Gunicorn Startup Command**\n\n```\ngunicorn -k uvicorn.workers.UvicornWorker \\\n -w 2 \\\n -b 0.0.0.0:8000 \\\n main:app \\\n --keep-alive 120 \\\n --timeout 240 \\\n --worker-connections 1000 \\\n --max-requests 1000 \\\n --max-requests-jitter 50 \\\n --preload \\\n --log-level info\n\n```\n\n**2. Custom Session Middleware (FastAPI)**\n\n\u003e Validates that a request always maps back to the same worker.\n\n**3. Debugging**\n\n\u003e Logged worker PID + session ID on each request.\n\u003e \n\u003e Observed the same client’s requests hitting different workers intermittently.\n\u003e \n\u003e Example log:\n\u003e \n\u003e Worker 1234 → Session=sess_abc_1234\n\u003e Worker 5678 → Session=sess_def_5678 # Same client, different worker\n\n### **Problem**\n\n\u003e Client requests are not consistently routed to the same Gunicorn worker.\n\u003e \n\u003e Cached Rally sessions exist only in the worker process that created them.\n\u003e \n\u003e When a client hits another worker, Rally rejects the request with BAD session ID.\n\n**Expected Behavior**\n\n\u003e All requests from a given client should always land on the same worker.\n\u003e \n\u003e Session affinity should be preserved consistently until session expiry.\n\n**### Questions for the Community**\n\n1. Is it possible to achieve worker-level sticky sessions with FastAPI + Gunicorn on Azure Web App?\n\n2. Should I run a single worker per instance (e.g., --workers 1) to avoid this problem (I Actually tried and found out the Server crashes a lot )? \n\n2. Should I share Rally session state across workers (e.g., Redis or DB-backed session store) instead of relying on per-worker caching(Tried it but due to mounting with FASTAPI, the session management didn't got intercepted for the main MCP server URL)?\n\n**Note:**\n\nHave to use statefull http as using sampling/elicitation in my server\n\n\n``` \nmcp = FastMCP(\"Rally MCP Server\",stateless_http=True)\n\n```\n\n\n\n### Additional Context\n\nEnvironment\n\nAzure Web App (Linux, Code Deploy)\n\nFastAPI + Gunicorn + Uvicorn workers\n\nPython 3.13\n\npyral (Rally API client)","author":{"url":"https://github.com/swapnlv","@type":"Person","name":"swapnlv"},"datePublished":"2025-09-08T03:21:54.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/1350/python-sdk/issues/1350"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:ed071c65-02a6-b5a2-5462-17e5f3940903 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8F90:23A0E5:280B88:36C5E7:6A5A5E9A |
| html-safe-nonce | 0c3f77bcda61f99c5806878f3e0efd732ab69ab947c47e8d396bc6350eba6f97 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RjkwOjIzQTBFNToyODBCODg6MzZDNUU3OjZBNUE1RTlBIiwidmlzaXRvcl9pZCI6Ijc4Njk3NDUzOTM3MzUxOTYzMTQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 9dafff7c6fc8ad287177e62c7ebf5aa983bc6c094739c4b87df252f0aa3ca116 |
| hovercard-subject-tag | issue:3392435009 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/modelcontextprotocol/python-sdk/1350/issue_layout |
| twitter:image | https://opengraph.githubassets.com/29a9af8ef6a9f58d2f5528d69be3e4e9ad054e255b4244dfeb7158b10e236917/modelcontextprotocol/python-sdk/issues/1350 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/29a9af8ef6a9f58d2f5528d69be3e4e9ad054e255b4244dfeb7158b10e236917/modelcontextprotocol/python-sdk/issues/1350 |
| og:image:alt | Question Context I’m running a FastAPI application on Azure Web App (Linux, containerized) using Gunicorn + Uvicorn workers. The app is an MCP (Model Context Protocol) server that connects to Rally... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | swapnlv |
| hostname | github.com |
| expected-hostname | github.com |
| None | 05b9ddf6a47d2dbe13944873a99f5fb4b83ba4871f9cb8a8e256793a63ca9687 |
| turbo-cache-control | no-preview |
| go-import | github.com/modelcontextprotocol/python-sdk git https://github.com/modelcontextprotocol/python-sdk.git |
| octolytics-dimension-user_id | 182288589 |
| octolytics-dimension-user_login | modelcontextprotocol |
| octolytics-dimension-repository_id | 862584018 |
| octolytics-dimension-repository_nwo | modelcontextprotocol/python-sdk |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 862584018 |
| octolytics-dimension-repository_network_root_nwo | modelcontextprotocol/python-sdk |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | f8d29d1bd03dda2dd14b3f80b8bc27e1111f43bd |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width