Skip to content
This repository was archived by the owner on Mar 9, 2026. It is now read-only.
This repository was archived by the owner on Mar 9, 2026. It is now read-only.

asserting throwing for async publish with currently errored batch #48

@camerondavison

Description

@camerondavison

Environment details

  • OS type and version: linux
  • Python version: python --version : Python 3.7.6
  • pip version: pip --version : pip 19.3.1
  • google-cloud-pubsub version: pip show google-cloud-pubsub 1.3.1

Code example

from google.cloud import pubsub_v1
import time


def main():
    publisher = pubsub_v1.PublisherClient()
    topic_path = "projects/*****/topics/*****"

    try:
        publisher.publish(topic_path, data=b'')  # invalid
    except:
        pass

    while True:
        print('publishing')
        publisher.publish(topic_path, data=b'valid')
        time.sleep(1)


if __name__ == '__main__':
    main()

Stack trace

Traceback (most recent call last):
  File "pubsub_error.py", line 21, in <module>
    main()
  File "pubsub_error.py", line 16, in main
    publisher.publish(topic_path, data=b'valid')
  File "/Users/camerondavison/Library/Python/3.7/lib/python/site-packages/google/cloud/pubsub_v1/publisher/client.py", line 364, in publish
    future = sequencer.publish(message)
  File "/Users/camerondavison/Library/Python/3.7/lib/python/site-packages/google/cloud/pubsub_v1/publisher/_sequencer/unordered_sequencer.py", line 113, in publish
    future = batch.publish(message)
  File "/Users/camerondavison/Library/Python/3.7/lib/python/site-packages/google/cloud/pubsub_v1/publisher/_batch/thread.py", line 334, in publish
    ), "Publish after stop() or publish error."
AssertionError: Publish after stop() or publish error.

As I mentioned just now in #26 (comment) it looks like there is an assert that was added that can now trigger for an errored out batch.

batch errors could happen any time so it seems like it would be better to move the assert below the check to see if the batch is still accepting new messages so that a new batch can be created if it errored out.

I validated the same code does not throw an exception on publish in 1.2.0

Metadata

Metadata

Assignees

Labels

api: pubsubIssues related to the googleapis/python-pubsub API.priority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions