codex icon indicating copy to clipboard operation
codex copied to clipboard

Code mode: MCP call left in flight by a failed or terminated exec script never gets item.completed

Open grey-tsosie opened this issue 1 month ago • 0 comments

What version of Codex CLI is running?

codex-cli 0.163.0

What subscription do you have?

ChatGPT sign-in

Which model were you using?

gpt-6.1-sol (low)

What platform is your computer?

Linux 7.2.11-arch1-1 x86_64

What issue are you seeing?

An MCP tool call started inside a code-mode exec script gets item.started but never item.completed when the script ends before the call returns. The turn still completes. Clients therefore show the call as in progress until they give up on it themselves.

In a T3 Code client (which uses app-server), eight such calls stayed "running" for 10 to 41 minutes each before the turn ended, although the scripts that started them had ended within 0 to 237 seconds. In each case, one of the following ended the script:

  • the agent's wait with terminate: true;
  • a throw in the script;
  • code-mode host exited with status exit status: 0.

The rollout files have no item_completed for any of them, while the same rollouts record hundreds of normally finished McpToolCall items.

The MCP server is also never told: it receives no notifications/cancelled before it is shut down at the end of the run.

What steps can reproduce the bug?

  1. Save this stdio MCP server as server.mjs. It has one tool that answers after 30 s.
import { createInterface } from "node:readline";
const send = (m) => process.stdout.write(JSON.stringify(m) + "\n");
createInterface({ input: process.stdin }).on("line", (line) => {
  const m = JSON.parse(line);
  if (m.method === "initialize")
    send({ jsonrpc: "2.0", id: m.id, result: { protocolVersion: m.params.protocolVersion, capabilities: { tools: {} }, serverInfo: { name: "slowprobe", version: "0" } } });
  else if (m.method === "tools/list")
    send({ jsonrpc: "2.0", id: m.id, result: { tools: [{ name: "slow_echo", description: "Echo text back after 30 seconds.", inputSchema: { type: "object", properties: { text: { type: "string" } }, required: ["text"] } }] } });
  else if (m.method === "tools/call")
    setTimeout(() => send({ jsonrpc: "2.0", id: m.id, result: { content: [{ type: "text", text: String(m.params.arguments?.text) }] } }), 30_000);
  else if (m.id !== undefined) send({ jsonrpc: "2.0", id: m.id, result: {} });
});
  1. Run:
codex exec --json --skip-git-repo-check -s read-only \
  -c 'mcp_servers.slowprobe.command="node"' -c 'mcp_servers.slowprobe.args=["/path/to/server.mjs"]' \
  "Run exactly one code-mode exec script that starts the slowprobe slow_echo tool with text 'hi' WITHOUT awaiting it, then waits 2 seconds (await new Promise(r => setTimeout(r, 2000))), then throws new Error('stop'). Do not retry or call slow_echo any other way. Then reply 'done'." </dev/null

Non-message events:

thread.started
turn.started
item.started   mcp_tool_call item_1 in_progress slowprobe slow_echo
turn.completed

As a control, the same prompt with the call awaited gives item.started, then item.completed mcp_tool_call item_1 completed 30 s later.

When the call was started with no wait before the throw, exec --json showed no item.started either. The log printed ERROR codex_core::tools::router: error=tool call cancelled, and the rollout had no McpToolCall item.

What is the expected behavior?

When a code-mode script ends with nested tool calls still in flight, Codex should finish each started call item with a terminal status (failed or cancelled) before the turn completes. It should also send notifications/cancelled to the MCP server for a request it will no longer read.

Additional information

The same pattern is probably not limited to MCP calls (any nested call a script leaves in flight), but I only tested MCP.

Assisted by: Claude (Claude Code)

grey-tsosie avatar Oct 21 '26 05:10 grey-tsosie