Skip to content

Commit

Permalink
Unblock connection receive when context is done
Browse files Browse the repository at this point in the history
When context is canceled we don't read anymore from `t.incoming` channel
because the select clause exists on `ctx.Done`. This change adds the
same behavior to the point where `t.incoming` channel is written.
Without this the call to `conn.Receive` could block forever given a
race condition between a context cancellation and a new incoming
message.

Furthermore, is always a good practice to listen to `context.Done` on
blocking points where a context variable is available.

Signed-off-by: Iñigo Garcia Olaizola <11333576+igolaizola@users.noreply.github.com>
  • Loading branch information
igolaizola committed Jul 7, 2022
1 parent 2d3bb5b commit 4cec1cf
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion protocol/pubsub/v2/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,10 @@ func (t *Protocol) startSubscriber(ctx context.Context, sub subscriptionWithTopi
}
// Ok, ready to start pulling.
return conn.Receive(ctx, func(ctx context.Context, m *pubsub.Message) {
t.incoming <- *m
select {
case t.incoming <- *m:
case <-ctx.Done():
}
})
}

Expand Down

0 comments on commit 4cec1cf

Please sign in to comment.