mirror of
https://github.com/ProtonMail/proton-bridge.git
synced 2025-12-11 13:16:53 +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 (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ErrInvalidReplyType = errors.New("reply type does not match")
|
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) {
|
func (r *Request) Reply(ctx context.Context, value any, err error) {
|
||||||
defer close(r.reply)
|
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 {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
case r.reply <- reply{
|
case r.reply <- reply{
|
||||||
|
|||||||
Reference in New Issue
Block a user