feat: Enhance config loading by applying GUNICORN_CMD_ARGS before programmatic options#12313
Conversation
…grammatic options
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Pull request overview
Updates Langflow’s programmatic Gunicorn integration to respect Gunicorn CLI-style configuration supplied via the GUNICORN_CMD_ARGS environment variable before applying Langflow’s programmatic overrides, enabling operational tuning like worker recycling.
Changes:
- Parse
GUNICORN_CMD_ARGSusing Gunicorn’s parser and apply resulting config values before applyingLangflowApplicationoptions. - Document
GUNICORN_CMD_ARGSusage for Gunicorn worker lifecycle controls (e.g.,--max-requests,--max-requests-jitter).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/backend/base/langflow/server.py |
Applies env-provided Gunicorn args before programmatic Gunicorn settings in LangflowApplication.load_config. |
docs/docs/Develop/environment-variables.mdx |
Adds documentation describing GUNICORN_CMD_ARGS for production Gunicorn deployments. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
|
@ogabrielluiz does it look good? what is probability of including it to 1.9? thanks! |
ogabrielluiz
left a comment
There was a problem hiding this comment.
Hey @severfire! LGTM. Thanks again for looking into this.
|
@ogabrielluiz Hi, could this be released into 1.9.1? Thanks! |
Merge commits are not allowed on this repository
9d14d3b
into
langflow-ai:release-1.9.1
…grammatic options (langflow-ai#12313) * feat: Enhance config loading by applying GUNICORN_CMD_ARGS before programmatic options * docs: Add Gunicorn configuration details to environment variables documentation * Update src/backend/base/langflow/server.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update src/backend/base/langflow/server.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * add unit test --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Gabriel Luiz Freitas Almeida <gabriel@logspace.ai> Co-authored-by: Jordan Frazier <122494242+jordanrfrazier@users.noreply.github.com> Co-authored-by: Jordan Frazier <jordan.frazier@datastax.com>
* fix: enable preload_app option in LangflowApplication configuration * feat: Integrate Sentry and Prometheus support in worker lifespan - Initialize Sentry SDK in the worker lifespan if a DSN is provided, allowing for better error tracking. - Start Prometheus HTTP server if enabled in settings, enhancing monitoring capabilities. - Added logging for both Sentry initialization and Prometheus server startup to aid in debugging and monitoring. Refactor existing code to defer Sentry initialization to avoid issues with process forking. This change improves the overall observability of the application. * fix: Correct Prometheus port validation logic in create_app function - Updated the condition for validating the Prometheus port to use 'and' instead of 'or', ensuring that the port is both greater than 0 and less than MAX_PORT. This change enhances the reliability of the Prometheus server configuration. * test: Add unit tests for LangflowApplication.pre_fork method - Introduced a new test suite for the pre_fork method in LangflowApplication, covering various scenarios including warnings for non-main threads and non-LISTEN TCP connections. - Implemented tests to ensure proper handling of psutil import errors and unexpected exceptions. - Verified that garbage collection is always executed during the pre_fork process. - Added a fake server mock to facilitate testing without requiring a real server instance. * refactor: Move telemetry service initialization to lifespan context - Removed the initialization of the telemetry service from the beginning of the `get_lifespan` function and added it within the lifespan context. This change ensures that the telemetry service is only initialized when needed, improving resource management and application performance. * fix: Enhance Sentry integration with error handling and import checks - Added error handling for Sentry SDK initialization to log warnings if the SDK is not installed or if initialization fails. - Updated the `setup_sentry` function to conditionally import `SentryAsgiMiddleware`, logging a warning if the import fails. - This improves the robustness of the application by preventing crashes related to missing Sentry dependencies and providing clearer logging for debugging. * fix: Improve Prometheus server error handling in lifespan context - Enhanced error handling for starting the Prometheus server by adding specific checks for ImportError and OSError. - Added logging for cases where the Prometheus client is not installed or when the port is already in use, improving clarity for debugging and operational monitoring. - This change aims to provide more informative feedback during server startup, enhancing the overall robustness of the application. * refactor: Rename and enhance Sentry middleware integration - Changed the function name from `setup_sentry` to `add_sentry_middleware` for clarity. - Updated the middleware setup to ensure Sentry is attached at request time, deferring SDK initialization to the worker lifespan to avoid ghost transactions. - Adjusted unit tests to mock the new middleware function name, ensuring continued test coverage and functionality. * feat: Enhance pre_fork method to identify benign threads before forking - Introduced a new class-level constant `_BENIGN_THREAD_PREFIXES` to define known benign thread prefixes. - Added a class method `_is_benign_thread` to check if a thread is benign based on its name. - Updated the `pre_fork` method to log warnings for non-benign threads, improving thread management during the forking process. - Added a unit test to ensure no warnings are emitted for benign threads, enhancing test coverage for the `pre_fork` method. * fix: Improve logging for psutil import error handling - Added a debug log statement to indicate when the psutil library is not installed, enhancing visibility into the application's behavior during TCP connection checks. - This change aims to provide clearer feedback for debugging and operational monitoring when the psutil dependency is missing. * fix: Enhance garbage collection handling in pre_fork method - Wrapped the gc.collect() call in a try-except block to prevent crashes if an exception is raised during garbage collection. - Added logging to capture any warnings when gc.collect() fails, improving error visibility during the pre-fork process. - Introduced a new unit test to ensure that the pre_fork method handles gc.collect() exceptions gracefully while still calling gc.freeze(). * docs: LANGFLOW_GUNICORN_PRELOAD environment variable introduced * refactor: Update application setup for Windows and non-Windows environments - Introduced conditional logic to handle application setup differently based on the operating system. - Added a factory pattern for creating the FastAPI application, improving flexibility for non-Windows systems. - Enhanced error handling to provide clear runtime messages when the application cannot be initialized correctly. * refactor: move test_server.py to correct unit/base location The test file exercises langflow.server which belongs to the base package (src/backend/base/). Moving it to src/backend/tests/unit/base/ follows the project convention and aligns with the path expected by CI ruff checks. Made-with: Cursor * feat: Enhance config loading by applying GUNICORN_CMD_ARGS before programmatic options (#12313) * feat: Enhance config loading by applying GUNICORN_CMD_ARGS before programmatic options * docs: Add Gunicorn configuration details to environment variables documentation * Update src/backend/base/langflow/server.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update src/backend/base/langflow/server.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * add unit test --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Gabriel Luiz Freitas Almeida <gabriel@logspace.ai> Co-authored-by: Jordan Frazier <122494242+jordanrfrazier@users.noreply.github.com> Co-authored-by: Jordan Frazier <jordan.frazier@datastax.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Gabriel Luiz Freitas Almeida <gabriel@logspace.ai> Co-authored-by: Jordan Frazier <122494242+jordanrfrazier@users.noreply.github.com> Co-authored-by: Jordan Frazier <jordan.frazier@datastax.com>
…grammatic options (#12313) * feat: Enhance config loading by applying GUNICORN_CMD_ARGS before programmatic options * docs: Add Gunicorn configuration details to environment variables documentation * Update src/backend/base/langflow/server.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update src/backend/base/langflow/server.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * add unit test --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Gabriel Luiz Freitas Almeida <gabriel@logspace.ai> Co-authored-by: Jordan Frazier <122494242+jordanrfrazier@users.noreply.github.com> Co-authored-by: Jordan Frazier <jordan.frazier@datastax.com>
…grammatic options (#12313) * feat: Enhance config loading by applying GUNICORN_CMD_ARGS before programmatic options * docs: Add Gunicorn configuration details to environment variables documentation * Update src/backend/base/langflow/server.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update src/backend/base/langflow/server.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * add unit test --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Gabriel Luiz Freitas Almeida <gabriel@logspace.ai> Co-authored-by: Jordan Frazier <122494242+jordanrfrazier@users.noreply.github.com> Co-authored-by: Jordan Frazier <jordan.frazier@datastax.com> (cherry picked from commit 9d14d3b)
* fix: enable preload_app option in LangflowApplication configuration * feat: Integrate Sentry and Prometheus support in worker lifespan - Initialize Sentry SDK in the worker lifespan if a DSN is provided, allowing for better error tracking. - Start Prometheus HTTP server if enabled in settings, enhancing monitoring capabilities. - Added logging for both Sentry initialization and Prometheus server startup to aid in debugging and monitoring. Refactor existing code to defer Sentry initialization to avoid issues with process forking. This change improves the overall observability of the application. * fix: Correct Prometheus port validation logic in create_app function - Updated the condition for validating the Prometheus port to use 'and' instead of 'or', ensuring that the port is both greater than 0 and less than MAX_PORT. This change enhances the reliability of the Prometheus server configuration. * test: Add unit tests for LangflowApplication.pre_fork method - Introduced a new test suite for the pre_fork method in LangflowApplication, covering various scenarios including warnings for non-main threads and non-LISTEN TCP connections. - Implemented tests to ensure proper handling of psutil import errors and unexpected exceptions. - Verified that garbage collection is always executed during the pre_fork process. - Added a fake server mock to facilitate testing without requiring a real server instance. * refactor: Move telemetry service initialization to lifespan context - Removed the initialization of the telemetry service from the beginning of the `get_lifespan` function and added it within the lifespan context. This change ensures that the telemetry service is only initialized when needed, improving resource management and application performance. * fix: Enhance Sentry integration with error handling and import checks - Added error handling for Sentry SDK initialization to log warnings if the SDK is not installed or if initialization fails. - Updated the `setup_sentry` function to conditionally import `SentryAsgiMiddleware`, logging a warning if the import fails. - This improves the robustness of the application by preventing crashes related to missing Sentry dependencies and providing clearer logging for debugging. * fix: Improve Prometheus server error handling in lifespan context - Enhanced error handling for starting the Prometheus server by adding specific checks for ImportError and OSError. - Added logging for cases where the Prometheus client is not installed or when the port is already in use, improving clarity for debugging and operational monitoring. - This change aims to provide more informative feedback during server startup, enhancing the overall robustness of the application. * refactor: Rename and enhance Sentry middleware integration - Changed the function name from `setup_sentry` to `add_sentry_middleware` for clarity. - Updated the middleware setup to ensure Sentry is attached at request time, deferring SDK initialization to the worker lifespan to avoid ghost transactions. - Adjusted unit tests to mock the new middleware function name, ensuring continued test coverage and functionality. * feat: Enhance pre_fork method to identify benign threads before forking - Introduced a new class-level constant `_BENIGN_THREAD_PREFIXES` to define known benign thread prefixes. - Added a class method `_is_benign_thread` to check if a thread is benign based on its name. - Updated the `pre_fork` method to log warnings for non-benign threads, improving thread management during the forking process. - Added a unit test to ensure no warnings are emitted for benign threads, enhancing test coverage for the `pre_fork` method. * fix: Improve logging for psutil import error handling - Added a debug log statement to indicate when the psutil library is not installed, enhancing visibility into the application's behavior during TCP connection checks. - This change aims to provide clearer feedback for debugging and operational monitoring when the psutil dependency is missing. * fix: Enhance garbage collection handling in pre_fork method - Wrapped the gc.collect() call in a try-except block to prevent crashes if an exception is raised during garbage collection. - Added logging to capture any warnings when gc.collect() fails, improving error visibility during the pre-fork process. - Introduced a new unit test to ensure that the pre_fork method handles gc.collect() exceptions gracefully while still calling gc.freeze(). * docs: LANGFLOW_GUNICORN_PRELOAD environment variable introduced * refactor: Update application setup for Windows and non-Windows environments - Introduced conditional logic to handle application setup differently based on the operating system. - Added a factory pattern for creating the FastAPI application, improving flexibility for non-Windows systems. - Enhanced error handling to provide clear runtime messages when the application cannot be initialized correctly. * refactor: move test_server.py to correct unit/base location The test file exercises langflow.server which belongs to the base package (src/backend/base/). Moving it to src/backend/tests/unit/base/ follows the project convention and aligns with the path expected by CI ruff checks. Made-with: Cursor * feat: Enhance config loading by applying GUNICORN_CMD_ARGS before programmatic options (#12313) * feat: Enhance config loading by applying GUNICORN_CMD_ARGS before programmatic options * docs: Add Gunicorn configuration details to environment variables documentation * Update src/backend/base/langflow/server.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update src/backend/base/langflow/server.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * add unit test --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Gabriel Luiz Freitas Almeida <gabriel@logspace.ai> Co-authored-by: Jordan Frazier <122494242+jordanrfrazier@users.noreply.github.com> Co-authored-by: Jordan Frazier <jordan.frazier@datastax.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Gabriel Luiz Freitas Almeida <gabriel@logspace.ai> Co-authored-by: Jordan Frazier <122494242+jordanrfrazier@users.noreply.github.com> Co-authored-by: Jordan Frazier <jordan.frazier@datastax.com>
This change updates the LangflowApplication class in server.py to correctly parse and respect the GUNICORN_CMD_ARGS environment variable.
Changes:
Modified load_config to use the Gunicorn parser to fetch arguments from the environment before applying programmatic overrides. Allows users to set parameters like --max-requests and --max-requests-jitter via .env or shell exports.
Context:
Previously, Langflow was not respecting Gunicorn-specific environment variables, making it difficult to manage worker lifecycles. This update specifically addresses memory leak issues under heavy loads by allowing workers to be recycled after a specified number of requests.