What happened?
Any Dependency subclass that calls ContextVar.set() in __aenter__ and reset() in __aexit__, storing the token on self, raises ValueError when two or more Docket background tasks run concurrently. This affects _CurrentContext and _CurrentAccessToken when auth is configured.
Docket interprets the teardown exception as task failure and requeues tasks that actually completed successfully.
ValueError: <Token var=<ContextVar name='_marker' ...>> was created in a different Context
Example Code
# main.py — run with FASTMCP_DOCKET_URL=redis://localhost:6379/0, FASTMCP_DOCKET_CONCURRENCY=5
import asyncio
from contextvars import ContextVar, Token
from fastmcp import FastMCP
from fastmcp.server.tasks import TaskConfig
from uncalled_for import Dependency
_marker: ContextVar[str] = ContextVar("_marker")
class Marker(Dependency["Marker"]):
_token: Token[str] | None = None
async def __aenter__(self) -> "Marker":
self._token = _marker.set("active")
return self
async def __aexit__(self, *args: object) -> None:
if self._token is not None:
_marker.reset(self._token)
self._token = None
mcp = FastMCP("bug-repro")
@mcp.tool(task=TaskConfig(mode="optional"))
async def my_tool(marker: Marker = Marker()) -> str:
await asyncio.sleep(3)
return "done"
app = mcp.http_app()
# repro.py — submit 4 concurrent background tasks
import asyncio
from fastmcp import Client
from fastmcp.client.transports import StreamableHttpTransport
async def main():
async with Client(StreamableHttpTransport("http://localhost:8080/mcp")) as client:
await asyncio.gather(*[client.call_tool("my_tool", {}, task=True) for _ in range(4)])
asyncio.run(main())
Version Information
FastMCP version: 3.1.1
MCP version: 1.26.0
Python version: 3.14.0
Platform: macOS-26.3.1-arm64-arm-64bit-Mach-O
Also:
pydocket 0.18.2
What happened?
Any Dependency subclass that calls
ContextVar.set()in__aenter__and reset() in__aexit__, storing the token on self, raisesValueErrorwhen two or more Docket background tasks run concurrently. This affects_CurrentContextand_CurrentAccessTokenwhen auth is configured.Docket interprets the teardown exception as task failure and requeues tasks that actually completed successfully.
ValueError: <Token var=<ContextVar name='_marker' ...>>was created in a different ContextExample Code
Version Information