diff --git a/codex-rs/mcp-client/src/mcp_client.rs b/codex-rs/mcp-client/src/mcp_client.rs index 084d0bf4ba..c3c26c31d5 100644 --- a/codex-rs/mcp-client/src/mcp_client.rs +++ b/codex-rs/mcp-client/src/mcp_client.rs @@ -365,7 +365,11 @@ impl McpClient { } }; - if let Some(tx) = pending.lock().await.remove(&id) { + let tx_opt = { + let mut guard = pending.lock().await; + guard.remove(&id) + }; + if let Some(tx) = tx_opt { // Ignore send errors – the receiver might have been dropped. let _ = tx.send(JSONRPCMessage::Response(resp)); } else { @@ -383,7 +387,11 @@ impl McpClient { RequestId::String(_) => return, // see comment above }; - if let Some(tx) = pending.lock().await.remove(&id) { + let tx_opt = { + let mut guard = pending.lock().await; + guard.remove(&id) + }; + if let Some(tx) = tx_opt { let _ = tx.send(JSONRPCMessage::Error(err)); } }