From 53a9a60d88865a0ed01f184e2f3cda009783d445 Mon Sep 17 00:00:00 2001 From: Magnus Kulke Date: Mon, 5 Oct 2026 14:20:12 +0200 Subject: [PATCH] fix: don't discard final message on server-close A ttrpc server can send valid response and then close the connection. In the client there is a race between the handling the cancellation and receiving the final response. In the case of pending msg the client should retrieve this first instead of returning `ErrClosed`. (this surfaced in kata-container runtime/agent comm that terminates a session with `DestroySandbox`. In some cases the client will not receive an acknowledgment of proper termination but an error instead) Signed-off-by: Magnus Kulke --- client.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/client.go b/client.go index d52d0a0ba..65d99ed35 100644 --- a/client.go +++ b/client.go @@ -547,7 +547,12 @@ func (c *Client) dispatch(ctx context.Context, req *Request, resp *Response) err case <-ctx.Done(): return ctx.Err() case <-c.ctx.Done(): - return ErrClosed + // If recv has a pending message, process that first. + select { + case msg = <-s.recv: + default: + return ErrClosed + } case <-s.recvClose: // If recv has a pending message, process that first select {