Skip to content

Race conditions in streaming pull samples #423

@plamut

Description

@plamut

The streaming pull future's cancel() is non-blocking. If subscriber client is used as a context manager that closes the underlying channel, but the client does not block when shutting down the stream (e.g. it just calls future.cancel()), the channel can be closed before the subscriber sends the last requests to the server (e.g. auto NACK of any remaining messages).

The block below is common in the existing samples:

with subscriber:
    try:
        # When `timeout` is not set, result() will block indefinitely,
        # unless an exception occurs first.
        streaming_pull_future.result(timeout=timeout)
    except TimeoutError:
        streaming_pull_future.cancel()

The samples should demonstrate how to block before leaving the with block, but they currently do no, and users have observed errors on shutdown (example). This should be fixed.

Metadata

Metadata

Assignees

Labels

api: pubsubIssues related to the googleapis/python-pubsub API.samplesIssues that are directly related to samples.type: docsImprovement to the documentation for an API.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions