Skip to content

feat: Enhance config loading by applying GUNICORN_CMD_ARGS before programmatic options#12313

Merged
jordanrfrazier merged 8 commits into
langflow-ai:release-1.9.1from
severfire:gunicorn-env-args-precedence-r190
Apr 15, 2026
Merged

feat: Enhance config loading by applying GUNICORN_CMD_ARGS before programmatic options#12313
jordanrfrazier merged 8 commits into
langflow-ai:release-1.9.1from
severfire:gunicorn-env-args-precedence-r190

Conversation

@severfire
Copy link
Copy Markdown
Contributor

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.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Mar 25, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: c514f1e1-5b46-40f0-b3ae-f35a8c26d9a4

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions github-actions Bot added the community Pull Request from an external contributor label Mar 25, 2026
@severfire severfire changed the title Enhance config loading by applying GUNICORN_CMD_ARGS before programmatic options [feat] Enhance config loading by applying GUNICORN_CMD_ARGS before programmatic options Mar 25, 2026
@severfire severfire changed the title [feat] Enhance config loading by applying GUNICORN_CMD_ARGS before programmatic options feat: Enhance config loading by applying GUNICORN_CMD_ARGS before programmatic options Mar 25, 2026
@github-actions github-actions Bot added the enhancement New feature or request label Mar 25, 2026
@github-actions github-actions Bot added enhancement New feature or request and removed enhancement New feature or request labels Mar 25, 2026
@ogabrielluiz ogabrielluiz requested review from Copilot and jordanrfrazier and removed request for jordanrfrazier March 26, 2026 22:08
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_ARGS using Gunicorn’s parser and apply resulting config values before applying LangflowApplication options.
  • Document GUNICORN_CMD_ARGS usage 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.

Comment thread src/backend/base/langflow/server.py Outdated
Comment thread src/backend/base/langflow/server.py Outdated
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@github-actions github-actions Bot added enhancement New feature or request and removed enhancement New feature or request labels Mar 26, 2026
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@github-actions github-actions Bot added enhancement New feature or request and removed enhancement New feature or request labels Mar 26, 2026
@severfire
Copy link
Copy Markdown
Contributor Author

@ogabrielluiz does it look good? what is probability of including it to 1.9? thanks!

Copy link
Copy Markdown
Contributor

@ogabrielluiz ogabrielluiz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @severfire! LGTM. Thanks again for looking into this.

@ogabrielluiz ogabrielluiz enabled auto-merge April 8, 2026 14:32
@ogabrielluiz ogabrielluiz added the lgtm This PR has been approved by a maintainer label Apr 8, 2026
@github-actions github-actions Bot added enhancement New feature or request and removed enhancement New feature or request labels Apr 8, 2026
@github-actions github-actions Bot removed the enhancement New feature or request label Apr 9, 2026
@github-actions github-actions Bot added the enhancement New feature or request label Apr 9, 2026
@severfire
Copy link
Copy Markdown
Contributor Author

@ogabrielluiz Hi, could this be released into 1.9.1? Thanks!

@jordanrfrazier jordanrfrazier changed the base branch from release-1.9.0 to release-1.9.1 April 15, 2026 13:59
auto-merge was automatically disabled April 15, 2026 13:59

Merge commits are not allowed on this repository

@github-actions github-actions Bot added enhancement New feature or request and removed enhancement New feature or request labels Apr 15, 2026
@github-actions github-actions Bot added enhancement New feature or request and removed enhancement New feature or request labels Apr 15, 2026
@jordanrfrazier jordanrfrazier merged commit 9d14d3b into langflow-ai:release-1.9.1 Apr 15, 2026
8 of 11 checks passed
@github-actions github-actions Bot added enhancement New feature or request and removed enhancement New feature or request labels Apr 15, 2026
erichare pushed a commit to severfire/langflow that referenced this pull request Apr 17, 2026
…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>
github-merge-queue Bot pushed a commit that referenced this pull request Apr 17, 2026
* 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>
erichare pushed a commit that referenced this pull request Apr 18, 2026
…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>
erichare pushed a commit that referenced this pull request Apr 24, 2026
…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)
erichare pushed a commit that referenced this pull request Apr 24, 2026
* 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community Pull Request from an external contributor enhancement New feature or request lgtm This PR has been approved by a maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants