google-cloud-go: PubSub client Receive method never returns when context is cancelled

Bug

Client

PubSub current

Describe Your Environment

Docker on GKE

Expected Behavior

When the context in the PubSub reader’s Receive method is cancelled, the receive method will return and stop receiving messages.

Actual Behavior

In the iterator when the stop() method is called, a it.stopped channel is closed https://github.com/GoogleCloudPlatform/google-cloud-go/blob/master/pubsub/iterator.go#L118 and the comments in the receive() method would suggest that this channel should be checked before trying to retrieve messages for the iterator https://github.com/GoogleCloudPlatform/google-cloud-go/blob/master/pubsub/iterator.go#L175 but as far as I can tell the stopped channel is never checked for closed(...) anywhere in the code.

As the code behaves today, if I cancel the context, I need to have a MaxOutstandingMessages or MaxOutstandingBytes set in order for the Acquire to eventually fail for the Receive method to actually return. This is undesirable because it requires that the MaxOutstandingMessages (or Bytes) is hit before the function return because the Acquire function in the flow controller also only checks the context when acquiring messages if the maximum is reached.

This also has the effect that if you choose to set MaxOutstanding settings to 0 and manage those things on the consumer side, the iterator and flow_controller will never return and you’ll be stuck in a forever loop. Most people won’t see these issues unless they are using high values of MaxOutstanding settings.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 1
  • Comments: 15 (8 by maintainers)

Most upvoted comments

@ajroetker Sorry, my colleague (and primary reviewer) is on leave. Given the subtle nature of this bug it’s good to get a solid review before we push anything through. 😃 We should have an update soon - please feel free to add yourself as cc on the aforementioned code review!