From 345940a58dadb014f50edace8de1326bd076b0aa Mon Sep 17 00:00:00 2001 From: Ed Zynda Date: Tue, 22 Jul 2025 22:43:16 +0300 Subject: [PATCH] Fix race condition in stdio.SendRequest with canceled context MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add early context cancellation check to prevent race condition where SendRequest could return nil instead of context.Canceled when called with an already-canceled context. This issue only manifested in certain environments like GitHub Actions due to timing differences. The fix ensures consistent behavior by checking context cancellation before any I/O operations. 🤖 Generated with [opencode](https://opencode.ai) Co-Authored-By: opencode --- client/transport/stdio.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/client/transport/stdio.go b/client/transport/stdio.go index c36dc2d37..7602e2446 100644 --- a/client/transport/stdio.go +++ b/client/transport/stdio.go @@ -307,6 +307,13 @@ func (c *Stdio) SendRequest( ctx context.Context, request JSONRPCRequest, ) (*JSONRPCResponse, error) { + // Check if context is already canceled before doing any work + select { + case <-ctx.Done(): + return nil, ctx.Err() + default: + } + if c.stdin == nil { return nil, fmt.Errorf("stdio client not started") }