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.

PubSub: Synchronous pull raises 504 after a few seconds if no messages available #9

@plamut

Description

@plamut

Forked from googleapis/google-cloud-python#7709 (comment).

Environment details

Tested with the following:

  • PubSub v1.0.2
  • Python 3.7
  • Ubuntu 18.04.

It is reported that the issue does not occur with in PubSub version v0.45.0.
Update: However, this could not be confirmed.
Confirmed by @HemangChothani

Steps to reproduce

  1. Create a topic.
  2. Create a subscription.
  3. With no messages published to the topic, perform a synchronous pull.

Actual result:
After a few seconds a 504 DeadlineExceeded exception is raised.

Expected result:
Assuming that the network functions normally, there should be no error, and the subscriber.pull() should simply return an empty list of messages.

Mind that there is no error if the return_immediately=True flag is used, but the latter is set to be deprecated and should thus not be used.

Code sample

The following script might be useful:

reproduce.py
import logging
import os
import threading
import time

from google.cloud import pubsub_v1


script_dir = os.path.dirname(os.path.realpath(__file__))
log_filename = os.path.join(script_dir, "subscriber.log")

log_format = (
    "%(levelname)-8s [%(asctime)s] %(threadName)-33s "
    "[%(name)s] [%(filename)s:%(lineno)d][%(funcName)s] %(message)s"
)

logging.basicConfig(
    level=logging.DEBUG,
    format=log_format,
)

logger = logging.getLogger(__name__)

PROJECT_NAME = "my-project"
TOPIC_NAME = "my-events"
SUBSCRIPTION_NAME = f"subscription_{TOPIC_NAME}"


def main():
    subscriber = pubsub_v1.SubscriberClient()

    subscription_path = subscriber.subscription_path(PROJECT_NAME, SUBSCRIPTION_NAME)
    topic_path = subscriber.topic_path(PROJECT_NAME, TOPIC_NAME)
    # TODO: if topic does not exist, you must first create it
    subscriber.create_subscription(name=subscription_path, topic=topic_path)

    print("Starting a pull...")
    response = subscriber.pull(
        subscription_path, max_messages=3,
        # return_immediately=True
    )

    received_messages = response.received_messages

    if not received_messages:
        print("No messages received")
        return

    print("response >>", response)

    for message in response.received_messages:
        print(message.message.data.decode("utf-8"))
    
    print("Sending ACK...")
    subscriber.acknowledge(
        subscription_path, [msg.ack_id for msg in response.received_messages]
    )
    print("... ACK sent.")


if __name__ == "__main__":
    main()

Metadata

Metadata

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
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions