Background Thread Not Exiting on Pub/Sub Subscriber Shutdown, Causing ValueError: 'Cannot invoke RPC: Channel closed!' #1316
Labels
api: pubsub
Issues related to the googleapis/python-pubsub API.
Environment details
OS type and version
: MacOS 15.1.1Python version
: 3.11.6pip version
: pip 24.0google-cloud-pubsub
: 2.27.1Description
I'm encountering an issue where my FastAPI application does not terminate gracefully when shutting down the Pub/Sub subscriber. This results in lingering background threads and the following error:
This behavior aligns with the GitHub issue #747 related to incomplete shutdown handling in the
google-cloud-pubsub
library.Steps to Reproduce
Set Up a FastAPI Application with Pub/Sub Subscriber:
google-cloud-pubsub
library.Run the Application:
Initiate Shutdown:
Ctrl+C
or another shutdown signal.Verify Shutdown Behavior:
Code Example
Below is a minimalistic example that reproduces the issue. This setup initializes a FastAPI application with a Pub/Sub subscriber and attempts to shut it down gracefully.
Stack Trace
Upon initiating shutdown (e.g., pressing
Ctrl+C
), the following logs and errors are produced:google-cloud-pubsub
library's handling of shutdown sequences, where the background gRPC threads do not terminate as expected, leading toValueError
exceptions and lingering threads that prevent the application from exiting gracefully.Thank you for your time and assistance!
Notes for Maintainer
and
"your-subscription-name"` with your actual Google Cloud project ID and Pub/Sub subscription name in the code example.Thanks!
The text was updated successfully, but these errors were encountered: