π The fast, Pythonic way to build MCP servers and clients.
Keep a finished task's status when it is cancelled (#5621)
Asem ahmed35eb89bcd4bf
diff --git a/fastmcp_tasks/fastmcp_tasks/handlers.py b/fastmcp_tasks/fastmcp_tasks/handlers.pyindex ae9deb996..d24b8cb90 100644--- a/fastmcp_tasks/fastmcp_tasks/handlers.py+++ b/fastmcp_tasks/fastmcp_tasks/handlers.py@@ -457,7 +457,8 @@ async def tasks_cancel(server: FastMCP, task_id: str) -> CancelTaskResult:concurrently enqueuing the next one: whichever wins the lock runs tocompletion before the other, and the update rechecks the marker under thesame lock. If the lock is wedged past its timeout, cancel proceeds- best-effort rather than hang.+ best-effort rather than hang. A task that already completed or failed is+ acknowledged but left as it is: terminal statuses are final."""docket = server._docketif docket is None:@@ -474,6 +475,18 @@ async def tasks_cancel(server: FastMCP, task_id: str) -> CancelTaskResult:execution, _base_task_key, leg_number, _created_at, _poll = await _lookup_task(docket, task_scope, task_id)+ # A task that already reached a terminal status stays there: the marker+ # below outranks the execution state in tasks/get, so recording it now+ # would replace a finished result or error with `cancelled`. A leg+ # parked on input has a COMPLETED execution too, but it is not finished.+ await execution.sync()+ if execution.state in (ExecutionState.FAILED, ExecutionState.CANCELLED) or (+ execution.state == ExecutionState.COMPLETED+ and not await read_outstanding_inputs(+ docket, task_scope, task_id, leg_number+ )+ ):+ return CancelTaskResult()ttl_seconds = int(docket.execution_ttl.total_seconds())await mark_cancelled(docket, task_scope, task_id, ttl_seconds)await clear_outstanding(docket, task_scope, task_id, leg_number)diff --git a/tests/tasks/server/test_task_methods.py b/tests/tasks/server/test_task_methods.pyindex b8d78b653..8641bfcd3 100644--- a/tests/tasks/server/test_task_methods.py+++ b/tests/tasks/server/test_task_methods.py@@ -113,6 +113,40 @@ async def test_tasks_cancel_transitions_to_cancelled():assert final.status in {"cancelled", "completed"}+async def test_tasks_cancel_leaves_completed_task_completed():+ """Cancelling a finished task keeps its `completed` status and result."""+ mcp = _methods_server()+ async with running_task_server(mcp):+ finished = await run_task(mcp, "quick_tool", {"value": 21})+ assert finished.status == "completed"++ await cancel_task(mcp, finished.task_id)++ after = await get_task(mcp, finished.task_id)+ assert after.status == "completed"+ assert after.result == finished.result+++async def test_tasks_cancel_leaves_failed_task_failed():+ """Cancelling a task that already failed keeps its `failed` status and error."""+ mcp = FastMCP("cancel-failed-test")+ mcp.add_extension(TasksExtension())++ @mcp.tool(task=True)+ async def protocol_error_tool() -> str:+ raise MCPError(code=-32000, message="backend unavailable")++ async with running_task_server(mcp):+ failed = await run_task(mcp, "protocol_error_tool", {})+ assert failed.status == "failed"++ await cancel_task(mcp, failed.task_id)++ after = await get_task(mcp, failed.task_id)+ assert after.status == "failed"+ assert after.error == failed.error++async def test_worker_keeps_running_after_cancel_before_task_starts(monkeypatch: pytest.MonkeyPatch,):