title: Google Cloud PubSub throws 504 Deadline Exceeded error while listening for messages in asynchronous pull
body: <p>I have a service that subscribes to a PubSub topic and uses the asynchronous pull. After an idle of 10 minutes and not receiving any messages, PubSub throws an 504 Deadline exceeded error.</p>
  <p>Error always occurs after about 10 minutes. Every similar issue I found was related to synchronous pull, not the asynchronous pull I use.</p>
  <p><strong>Error message:</strong></p>
  <pre><code>INFO:google.cloud.pubsub_v1.subscriber._protocol.streaming_pull_manager:Observed non-terminating stream error 504 Deadline Exceeded                                                                                                           
  INFO:google.cloud.pubsub_v1.subscriber._protocol.streaming_pull_manager:Observed recoverable stream error 504 Deadline Exceeded                                                                                                                 
  INFO:google.api_core.bidi:Re-established stream                                                                         
  INFO:google.cloud.pubsub_v1.subscriber._protocol.streaming_pull_manager:Observed non-terminating stream error 504 Deadline Exceeded                                                                                                             
  INFO:google.cloud.pubsub_v1.subscriber._protocol.streaming_pull_manager:Observed recoverable stream error 504 Deadline Exceeded
  </code></pre>
  <p><strong>Subscriber Class:</strong></p>
  <pre><code>from google.cloud import pubsub_v1
  class Subscriber():
  
      def __init__(self):
          self.subscriber = pubsub_v1.SubscriberClient()
          self.project_id = &quot;my-project-id&quot;
          self.subscription_id = &quot;SUBSCRIPTION_MAIL_RECIEVE&quot;
          self.subscription_path = self.subscriber.subscription_path(self.project_id,
                                                                     self.subscription_id)
  
      def subscribe(self, callback):
          &quot;&quot;&quot;
          Parameters:
              callback (callable): function to be called for incoming messages on given topic
          &quot;&quot;&quot;
          streaming_pull_future = self.subscriber.subscribe(self.subscription_path,
                                                            callback=callback)
          return streaming_pull_future
  </code></pre>
  <p><strong>Listening for messages:</strong></p>
  <pre><code>subscriber = Subscriber()
  pull_future = subscriber.subscribe(my_callback_function(message))
  
  with subscriber.subscriber:
          try:
              print(&quot;Waiting for messages&quot;)
              pull_future.result()
          except TimeoutError:
              pull_future.cancel()
  </code></pre>