79742815

Date: 2025-08-21 22:41:05
Score: 0.5
Natty:
Report link

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.

Solutions (in order of preference for me)

1. Switch to StructuredLogHandler (Recommended)

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:

2. Downgrade to Python 3.10

Change your app.yaml:
runtime: python310 # instead of python311

Benefits: Confirmed to resolve the issue immediately
Drawbacks: Delays Python 3.11 adoption

3. Upgrade google-cloud-logging

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

References

The StructuredLogHandler approach is recommended as it's the most future-proof solution and completely avoids the threading architecture that causes these shutdown errors.

Reasons:
  • Blacklisted phrase (1.5): any solution
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: Amin Gheibi