mirror of
https://github.com/ProtonMail/proton-bridge.git
synced 2025-12-10 04:36:43 +00:00
fix(GODT-2802): Ensure cpc reply does not block if never read
This commit is contained in:
@ -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{
|
||||
|
||||
Reference in New Issue
Block a user