CodexBloom - Programming Q&A Platform

GCP Pub/Sub Subscription Dead Letter Policy Not Triggering with Python Client

πŸ‘€ Views: 284 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-06
google-cloud-platform pubsub python Python

I'm working through a tutorial and I've spent hours debugging this and I'm sure I'm missing something obvious here, but I'm currently trying to implement a Dead Letter Policy for a Google Cloud Pub/Sub subscription using the Python client library. I've set up my subscription with a dead letter topic, but it seems that messages are not being sent to the dead letter topic even after multiple acknowledgment failures. Here’s the relevant code snippet: ```python from google.cloud import pubsub_v1 project_id = 'your-project-id' dead_letter_topic = 'projects/your-project-id/topics/dead-letter-topic' subscription_name = 'your-subscription-name' subscriber = pubsub_v1.SubscriberClient() subscription_path = subscriber.subscription_path(project_id, subscription_name) # Update subscription with dead letter policy update_mask = {'paths': ['dead_letter_policy']} dead_letter_policy = pubsub_v1.types.DeadLetterPolicy( dead_letter_topic=dead_letter_topic, max_delivery_attempts=5, ) subscription = subscriber.update_subscription( request={ 'subscription': { 'name': subscription_path, 'dead_letter_policy': dead_letter_policy, }, 'update_mask': update_mask, } ) ``` I’m subscribing to messages and failing to acknowledge them intentionally: ```python def callback(message): print(f'Received message: {message.data}') # Simulating an acknowledgment failure raise Exception('Failing acknowledgment intentionally') streaming_pull_future = subscriber.subscribe(subscription_path, callback=callback) try: streaming_pull_future.result() except Exception as e: print(f'behavior occurred: {e}') ``` I expected that after 5 failed delivery attempts, the messages would be routed to the dead letter topic, but they are still being redelivered to the original subscription. I've double-checked the permissions, and the Pub/Sub service account has the `pubsub.topics.publish` permission on the dead letter topic. The Pub/Sub library version I'm using is `google-cloud-pubsub==2.7.0`. Has anyone else faced this scenario or know if there are additional steps required to properly trigger the dead letter policy? This is part of a larger application I'm building. What am I doing wrong? This is my first time working with Python 3.10. What am I doing wrong? How would you solve this? This is part of a larger application I'm building. What am I doing wrong?