Since this is still an issue and there are not many solutions out there, I am gonna post an answer here.
This is a known compatibility issue between google-cloud-logging and Python 3.11. The CloudLoggingHandler creates background daemon threads that don't shut down gracefully when GAE terminates instances in Python 3.11, due to stricter thread lifecycle management.
Replace your current logging configuration with StructuredLogHandler, which writes to stdout instead of using background threads:
# In Django settings.py (or equivalent configuration)
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'structured': {
'class': 'google.cloud.logging.handlers.StructuredLogHandler',
}
},
'loggers': {
'': {
'handlers': ['structured'],
'level': 'INFO',
}
},
}
Remove the problematic setup:
# Remove these lines:
logging_client = logging.Client()
logging_client.setup_logging()
Benefits:
Change your app.yaml:
runtime: python310 # instead of python311
Benefits: Confirmed to resolve the issue immediately
Drawbacks: Delays Python 3.11 adoption
Update to the latest version in requirements.txt:
google-cloud-logging>=3.10.0
Benefits: May include Python 3.11 compatibility fixes
Drawbacks: Not guaranteed to resolve the issue
The StructuredLogHandler approach is recommended as it's the most future-proof solution and completely avoids the threading architecture that causes these shutdown errors.