feat: add python-disable-gunicorn flag#10708
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a --python-disable-gunicorn option to the Firebase emulators, allowing Python Functions to run using the Flask server in non-debug mode instead of gunicorn to prevent fork-safety crashes on macOS. This is implemented by dynamically injecting a sitecustomize.py shim via PYTHONPATH to intercept and block gunicorn imports. The feedback recommends improving the Python import hook's robustness by using *args and **kwargs to support varying signatures, and registering a process exit hook to clean up the temporary directory created for the shim to prevent resource leaks.
| def _firebase_import_without_gunicorn(name, globals=None, locals=None, fromlist=(), level=0): | ||
| if name == "functions_framework._http.gunicorn": | ||
| raise ImportError("Gunicorn disabled for local Firebase Functions emulator") | ||
| return _original_import(name, globals, locals, fromlist, level) |
There was a problem hiding this comment.
The _firebase_import_without_gunicorn hook currently uses a fixed signature (name, globals=None, locals=None, fromlist=(), level=0). To ensure maximum compatibility across different Python versions, alternative interpreters, or third-party packages that might call __import__ with different positional/keyword arguments, it is more robust to use *args and **kwargs and extract the name argument dynamically.
| def _firebase_import_without_gunicorn(name, globals=None, locals=None, fromlist=(), level=0): | |
| if name == "functions_framework._http.gunicorn": | |
| raise ImportError("Gunicorn disabled for local Firebase Functions emulator") | |
| return _original_import(name, globals, locals, fromlist, level) | |
| def _firebase_import_without_gunicorn(*args, **kwargs): | |
| name = args[0] if args else kwargs.get("name") | |
| if name == "functions_framework._http.gunicorn": | |
| raise ImportError("Gunicorn disabled for local Firebase Functions emulator") | |
| return _original_import(*args, **kwargs) |
Fixes #6628