All previous approaches (plain httptest.Server, raw TCP with io.Copy,
httptest+Hijack) produced a consistent 2-minute timeout in CI.
Analysis of httptest.Server revealed a subtle goroutine ordering
dependency: the server reads the request body into a buffer before
calling the handler, but the client's request-body writer goroutine
waits for response headers before sending the body. The handler must
return (sending headers) before the client's body writer can complete.
This creates a potential race where the connection is closed while the
client is still writing.
The raw TCP approach eliminates all HTTP library goroutines:
- net.Listen("tcp", "127.0.0.1:0") binds an ephemeral port
- Accept in a goroutine, handle one connection
- Read headers using a 2-second deadline (enough for client to send)
- Send response immediately, close connection
- a2aClient DialContext intercepts all dials and redirects to our port
Key insight: set a Read deadline (not ReadAll to EOF) so the server
proceeds to send the response without waiting for the body. The kernel
discards unread buffered body bytes on close — harmless.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>