From c4b75c6f342f69df3689831cbc769098c8831bec Mon Sep 17 00:00:00 2001 From: Leander Beernaert Date: Fri, 28 Jul 2023 14:52:46 +0200 Subject: [PATCH] fix(GODT-2802): Ensure cpc reply does not block if never read --- pkg/cpc/cpc.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pkg/cpc/cpc.go b/pkg/cpc/cpc.go index 3d153e1f..231f8b9a 100644 --- a/pkg/cpc/cpc.go +++ b/pkg/cpc/cpc.go @@ -20,6 +20,7 @@ package cpc import ( "context" "errors" + "time" ) var ErrInvalidReplyType = errors.New("reply type does not match") @@ -41,6 +42,12 @@ func (r *Request) Value() any { func (r *Request) Reply(ctx context.Context, value any, err error) { defer close(r.reply) + // There is a chance when `A` sends a request to `B` that the `A`'s context is cancelled before `B` is able to reply. + // This can cause `B` to wait forever. To avoid that situation we use a context with a deadline. This is safe + // since `A` is blocked waiting on our reply, and if the above happens, there's nothing left to do. + ctx, cancel := context.WithDeadline(ctx, time.Now().Add(10*time.Second)) + defer cancel() + select { case <-ctx.Done(): case r.reply <- reply{