Dispatch MCP requests concurrently per connection

handleConn processed requests serially, so a slow tool (e.g.
wait_for_pattern with a 300s timeout) monopolized the single per-agent
MCP connection and every queued call timed out behind it. Handle each
request in its own goroutine, serialize responses through a per-conn
write mutex (full response written atomically, partial writes handled),
copy the request line before handing it off (bufio reuses its buffer),
and wait on a WaitGroup before closing the conn so in-flight handlers
finish cleanly. Greeting stays sequential; notifications still get no
response.

Resolves the [MCP TIMEOUT] TODO item.
This commit is contained in:
2026-05-25 12:39:31 +01:00
parent 53f06b604f
commit 7b5a22618f
4 changed files with 226 additions and 24 deletions

11
TODO.md
View File

@@ -1,5 +1,4 @@
- [ ] Codex idle detection seems to trigger too soon, see below [CODEX IDLE]
- [ ] Issue with mcp timing out [MCP TIMEOUT]
- [ ] When opening a codex sub agent, the message gets input to the field, but the message is never submitted.
- This appears to be inconsistent. Sometimes it works, sometimes it doesn't. Might be because of popups on codex sub agents?
- Question: when it fails, is a Codex startup popup visible (trust/workspace, auth/model selection, permissions), or is the normal composer focused?
@@ -60,13 +59,3 @@ Crunched for 1m 57s
✔ Set up worktree for issue 136 implementation │
✔ Draft implementation plan │
… +2 completed │
# [MCP TIMEOUT]
⚙ patterm_send_input [key=enter, kind=key, process_id=p_a6726d, submit=false, tail_mode=stream, text=, wait_ms=1000] │
⚙ patterm_wait_for_pattern [pattern=Findings|No findings|No issues|Residual risk, process_id=p_a6726d, scope=scrollback, timeout_seconds=300] │
MCP error -32001: Request timed out │
⚙ patterm_get_process_status [process_id=p_a6726d] │
MCP error -32001: Request timed out │