1

I'm getting the following error when trying to unsubscribe from a topic in Google Pub/Sub.

self = <google.cloud.pubsub_v1.SubscriberClient object at 0x000002069A31D820>

    def __enter__(self) -> "Client":
        if self._closed:
>           raise RuntimeError("Closed subscriber cannot be used as context manager.")
E           RuntimeError: Closed subscriber cannot be used as context manager.

venv\lib\site-packages\google\cloud\pubsub_v1\subscriber\client.py:285: RuntimeError

Here is the relevant code, which is based on google's own documentation.

    def unsubscribe(self, subscription_id):
        subscriber = self.subscriber

        subscription_path = subscriber.subscription_path(self.project_id, subscription_id)

        with subscriber:
            subscriber.delete_subscription(request={"subscription": subscription_path})

        return True

1 Answer 1

1

Well, Google's own documentation states that the code I was using would automatically close the subscription because of the with block. For some reason I kept overlooking that comment in the code.

This resolved my issue:

    def unsubscribe(self, subscription_id):
        subscriber = self.subscriber

        subscription_path = subscriber.subscription_path(self.project_id, subscription_id)

        subscriber.delete_subscription(request={"subscription": subscription_path})

        return True

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.