Skip to content

refactor: reuse single session when getting variables from db#8814

Merged
ogabrielluiz merged 12 commits into
mainfrom
lfoss-1653
Jul 3, 2025
Merged

refactor: reuse single session when getting variables from db#8814
ogabrielluiz merged 12 commits into
mainfrom
lfoss-1653

Conversation

@deon-sanchez
Copy link
Copy Markdown
Collaborator

@deon-sanchez deon-sanchez commented Jul 1, 2025

Issue

POST http://localhost:3000/api/v1/custom_component/update
Error: "QueuePool limit of size 20 overflow 30 reached, connection timed out, timeout 30.00 (Background on this error at: https://sqlalche.me/e/20/3o7r)"

Repo Steps

  • create a flow with MCP Tools
  • toggle tool mode on/off
  • keep in mind this failure is flaky. so sometimes it will fail, sometimes its works.

Description

This pull request introduces improvements to database session management and updates to connection pool settings to enhance performance and prevent connection pool exhaustion. The most important changes include the introduction of session reuse in database operations, updates to method signatures to support session passing, and adjustments to database connection pool configurations.

Database Session Management Enhancements:

  • src/backend/base/langflow/api/v1/endpoints.py: Introduced the use of a single database session within custom_component_update to handle all load_from_db operations, preventing connection pool exhaustion. ([src/backend/base/langflow/api/v1/endpoints.pyL720-R725](https://github.com/langflow-ai/langflow/pull/8814/files#diff-2cc70b3b7a7be90cba6b85c839b082b0cc844c917644fdc2c9fafba56ba8b607L720-R725))
  • src/backend/base/langflow/custom/custom_component/custom_component.py: Updated the get_variables method to optionally accept a session parameter, allowing for session reuse. A fallback mechanism was added to create a new session for backward compatibility. ([[1]](https://github.com/langflow-ai/langflow/pull/8814/files#diff-010449088e429212d46c7455250d37f399c727ed8e5ab77b99cb155242e879f3L407-R407), [[2]](https://github.com/langflow-ai/langflow/pull/8814/files#diff-010449088e429212d46c7455250d37f399c727ed8e5ab77b99cb155242e879f3R428-R433))
  • src/backend/base/langflow/interface/initialize/loading.py: Modified get_instance_results and update_params_with_load_from_db_fields to accept an optional session parameter, enabling session reuse during database operations. ([[1]](https://github.com/langflow-ai/langflow/pull/8814/files#diff-0a870bc1d9300592df1438dde8700c07b2781027dda82cb0dc85372e7365fe09R60-R63), [[2]](https://github.com/langflow-ai/langflow/pull/8814/files#diff-0a870bc1d9300592df1438dde8700c07b2781027dda82cb0dc85372e7365fe09R114-R121))

Database Connection Pool Configuration:

  • src/backend/base/langflow/services/settings/base.py: Increased pool_size from 20 to 30 and max_overflow from 30 to 50 to handle higher concurrent database connections and improve performance under high load scenarios. ([[1]](https://github.com/langflow-ai/langflow/pull/8814/files#diff-c1d45d91203864dca90286f0f322de7fd8f9dd9acadb9c6e0fbb21cc7bf75fc3L85-R88), [[2]](https://github.com/langflow-ai/langflow/pull/8814/files#diff-c1d45d91203864dca90286f0f322de7fd8f9dd9acadb9c6e0fbb21cc7bf75fc3L107-R108))

Summary by CodeRabbit

  • New Features
    • Improved database session management for more efficient operations when loading parameters and variables.
  • Performance
    • Increased default database connection pool size and maximum overflow settings for enhanced scalability and throughput.

- Updated `get_variables` method in `CustomComponent` to accept an optional session parameter, allowing for session reuse and reducing connection pool exhaustion.
- Modified `update_params_with_load_from_db_fields` to pass the session when calling `get_variables`.
- Adjusted `get_instance_results` to support session management for database operations.
- Increased connection pool size and max overflow in settings for improved performance under load.
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jul 1, 2025

Important

Review skipped

Auto incremental reviews are disabled on this repository.

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.

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

Walkthrough

The changes introduce optional session management in several asynchronous functions related to custom component variable loading, allowing shared database sessions to be passed through the call stack. Additionally, default database connection pool sizes are increased in the settings. No changes to the public API signatures except for added optional parameters.

Changes

File(s) Change Summary
.../api/v1/endpoints.py Updated imports and modified an endpoint to use a shared async session for parameter updates.
.../custom/custom_component/custom_component.py Modified get_variables to accept an optional session parameter for external session management.
.../interface/initialize/loading.py Added optional session parameters to relevant async functions and forwarded session through variable loading.
.../services/settings/base.py Increased default pool_size (20→30) and max_overflow (30→50) for DB connection settings.

Sequence Diagram(s)

sequenceDiagram
    participant API as API Endpoint
    participant Session as session_scope()
    participant Loader as update_params_with_load_from_db_fields
    participant Component as CustomComponent.get_variables
    participant DB as Database

    API->>Session: async with session_scope()
    Session->>Loader: update_params_with_load_from_db_fields(..., session)
    Loader->>Component: get_variables(..., session)
    Component->>DB: Fetch variable using session
    DB-->>Component: Return variable
    Component-->>Loader: Return variable
    Loader-->>Session: Return updated params
    Session-->>API: Return response
Loading

Suggested labels

enhancement, size:L, lgtm

Suggested reviewers

  • jordanrfrazier
  • edwinjosechittilappilly
✨ Finishing Touches
🧪 Generate Unit Tests
  • Create PR with Unit Tests
  • Post Copyable Unit Tests in a Comment
  • Commit Unit Tests in branch lfoss-1653

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai auto-generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@dosubot dosubot Bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Jul 1, 2025
@github-actions github-actions Bot added the refactor Maintenance tasks and housekeeping label Jul 1, 2025
@deon-sanchez deon-sanchez changed the title refactor: enhance database session management in custom components refactor: MCP Tools Mode not working as expected Jul 1, 2025
@github-actions github-actions Bot added refactor Maintenance tasks and housekeeping and removed refactor Maintenance tasks and housekeeping labels Jul 1, 2025
@deon-sanchez deon-sanchez self-assigned this Jul 1, 2025
@github-actions github-actions Bot added refactor Maintenance tasks and housekeeping and removed refactor Maintenance tasks and housekeeping labels Jul 1, 2025
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

This PR enhances database session management by enabling session reuse during load operations and updates the connection pool settings to prevent exhaustion and improve performance.

  • Introduces an optional session parameter in get_variables, get_instance_results, and update_params_with_load_from_db_fields to allow reuse of a single database session.
  • Wraps all load_from_db calls in custom_component_update with a single session_scope.
  • Increases pool_size from 20 to 30 and max_overflow from 30 to 50 in the database settings.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
src/backend/base/langflow/services/settings/base.py Updated pool_size and max_overflow values in both class attributes and settings
src/backend/base/langflow/interface/initialize/loading.py Added session parameter to get_instance_results and update_params_with_load_from_db_fields
src/backend/base/langflow/custom/custom_component/custom_component.py Extended get_variables to accept an optional session and added fallback logic
src/backend/base/langflow/api/v1/endpoints.py Wrapped load_from_db operations in a single session_scope context
Comments suppressed due to low confidence (4)

src/backend/base/langflow/services/settings/base.py:90

  • The docstring suggests max_overflow should be 2x pool_size (i.e., 60), but it's set to 50. Consider updating the docstring to reflect the intended ratio or adjusting the value to match the recommendation.
    Should be 2x the pool_size for optimal performance under load."""

src/backend/base/langflow/custom/custom_component/custom_component.py:407

  • [nitpick] Update the function docstring to include a description for the new 'session' parameter, specifying its type and usage.
    async def get_variables(self, name: str, field: str, session=None):

src/backend/base/langflow/interface/initialize/loading.py:60

  • [nitpick] Add a brief docstring or inline comment to describe the purpose and expected type of the optional 'session' parameter.
    session=None,

src/backend/base/langflow/interface/initialize/loading.py:114

  • Consider adding unit tests to cover the new 'session' parameter path in update_params_with_load_from_db_fields to ensure session reuse behaves as expected.
    session=None,

Comment thread src/backend/base/langflow/custom/custom_component/custom_component.py Outdated
@github-actions github-actions Bot added refactor Maintenance tasks and housekeeping and removed refactor Maintenance tasks and housekeeping labels Jul 1, 2025
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (2)
src/backend/base/langflow/api/v1/endpoints.py (1)

720-726: Excellent session management implementation with minor formatting issue.

The use of session_scope() context manager ensures all database operations for loading parameters share a single session, which aligns perfectly with the PR's goal of reducing connection pool exhaustion.

Fix the whitespace on the blank line:

-            
+
src/backend/base/langflow/interface/initialize/loading.py (1)

60-64: Session parameter propagation implemented correctly with minor formatting issue.

The session parameter is properly added to the function signature and forwarded to update_params_with_load_from_db_fields, enabling consistent session management across the call stack.

Fix the line length issue by breaking the long line:

-    custom_params = await update_params_with_load_from_db_fields(
-        custom_component, custom_params, vertex.load_from_db_fields, fallback_to_env_vars=fallback_to_env_vars, session=session
-    )
+    custom_params = await update_params_with_load_from_db_fields(
+        custom_component, 
+        custom_params, 
+        vertex.load_from_db_fields, 
+        fallback_to_env_vars=fallback_to_env_vars, 
+        session=session
+    )
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 514b312 and b3617e6.

📒 Files selected for processing (4)
  • src/backend/base/langflow/api/v1/endpoints.py (2 hunks)
  • src/backend/base/langflow/custom/custom_component/custom_component.py (2 hunks)
  • src/backend/base/langflow/interface/initialize/loading.py (2 hunks)
  • src/backend/base/langflow/services/settings/base.py (2 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
`src/backend/**/*.py`: Run make format_backend to format Python code early and often Run make lint to check for linting issues in backend Python code

src/backend/**/*.py: Run make format_backend to format Python code early and often
Run make lint to check for linting issues in backend Python code

📄 Source: CodeRabbit Inference Engine (.cursor/rules/backend_development.mdc)

List of files the instruction was applied to:

  • src/backend/base/langflow/api/v1/endpoints.py
  • src/backend/base/langflow/custom/custom_component/custom_component.py
  • src/backend/base/langflow/services/settings/base.py
  • src/backend/base/langflow/interface/initialize/loading.py
`src/backend/**/*component*.py`: In your Python component class, set the `icon` attribute to a string matching the frontend icon mapping exactly (case-sensitive).

src/backend/**/*component*.py: In your Python component class, set the icon attribute to a string matching the frontend icon mapping exactly (case-sensitive).

📄 Source: CodeRabbit Inference Engine (.cursor/rules/icons.mdc)

List of files the instruction was applied to:

  • src/backend/base/langflow/custom/custom_component/custom_component.py
🧠 Learnings (1)
src/backend/base/langflow/api/v1/endpoints.py (9)
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/backend_development.mdc:0-0
Timestamp: 2025-06-30T14:39:17.428Z
Learning: Applies to src/backend/base/langflow/components/**/__init__.py : Update __init__.py with alphabetical imports when adding new components
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/backend_development.mdc:0-0
Timestamp: 2025-06-30T14:39:17.428Z
Learning: Applies to src/backend/base/langflow/components/**/*.py : Implement async component methods using async def and await for asynchronous operations
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/backend_development.mdc:0-0
Timestamp: 2025-06-30T14:39:17.428Z
Learning: Applies to src/backend/base/langflow/components/**/*.py : Add new backend components to the appropriate subdirectory under src/backend/base/langflow/components/
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/backend_development.mdc:0-0
Timestamp: 2025-06-30T14:39:17.428Z
Learning: Applies to src/backend/base/langflow/components/**/*.py : Use asyncio.Queue for non-blocking queue operations in async components and handle timeouts appropriately
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/backend_development.mdc:0-0
Timestamp: 2025-06-30T14:39:17.428Z
Learning: Applies to src/backend/base/langflow/components/**/*.py : Use asyncio.create_task for background work in async components and ensure proper cleanup on cancellation
Learnt from: ogabrielluiz
PR: langflow-ai/langflow#0
File: :0-0
Timestamp: 2025-06-26T19:43:18.260Z
Learning: In langflow custom components, the `module_name` parameter is now propagated through template building functions to add module metadata and code hashes to frontend nodes for better component tracking and debugging.
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/testing.mdc:0-0
Timestamp: 2025-06-30T14:41:58.837Z
Learning: Applies to {src/backend/tests/**/*.py,tests/**/*.py} : Use predefined JSON flows and utility functions for flow testing (e.g., 'create_flow', 'build_flow', 'get_build_events', 'consume_and_assert_stream').
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/backend_development.mdc:0-0
Timestamp: 2025-06-30T14:39:17.428Z
Learning: Applies to src/backend/tests/unit/**/*.py : Test component integration within flows using create_flow, build_flow, and get_build_events utilities
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/testing.mdc:0-0
Timestamp: 2025-06-30T14:41:58.837Z
Learning: Applies to {src/backend/tests/**/*.py,tests/**/*.py} : Test component configuration updates by asserting changes in build config dictionaries.
🧬 Code Graph Analysis (1)
src/backend/base/langflow/custom/custom_component/custom_component.py (4)
src/backend/tests/unit/services/variable/test_service.py (1)
  • session (23-28)
src/backend/base/langflow/services/variable/service.py (1)
  • get_variable (56-80)
src/backend/base/langflow/services/variable/base.py (1)
  • get_variable (25-36)
src/backend/base/langflow/services/deps.py (1)
  • session_scope (157-179)
🪛 Ruff (0.11.9)
src/backend/base/langflow/api/v1/endpoints.py

720-720: Blank line contains whitespace

Remove whitespace from blank line

(W293)

src/backend/base/langflow/custom/custom_component/custom_component.py

428-428: Blank line contains whitespace

Remove whitespace from blank line

(W293)


432-432: Unnecessary else after return statement

Remove unnecessary else

(RET505)


434-434: Redefining argument with the local name session

(PLR1704)

src/backend/base/langflow/interface/initialize/loading.py

63-63: Line too long (127 > 120)

(E501)

🪛 Pylint (3.3.7)
src/backend/base/langflow/custom/custom_component/custom_component.py

[refactor] 429-435: Unnecessary "else" after "return", remove the "else" and de-indent the code inside it

(R1705)

⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: Validate PR
  • GitHub Check: Update Starter Projects
  • GitHub Check: Optimize new Python code in this PR
  • GitHub Check: Run Ruff Check and Format
  • GitHub Check: Ruff Style Check (3.13)
🔇 Additional comments (5)
src/backend/base/langflow/services/settings/base.py (2)

85-88: LGTM! Pool size increases support enhanced session management.

The increased pool_size (20→30) and max_overflow (30→50) values align well with the PR's goal of improving database session management under load and reducing connection pool exhaustion.


107-108: Configuration consistency maintained.

The db_connection_settings dictionary correctly reflects the updated pool values, ensuring consistency between individual attributes and the centralized configuration.

src/backend/base/langflow/api/v1/endpoints.py (1)

46-46: Session management import added correctly.

The addition of session_scope import supports the enhanced database session management approach implemented in this PR.

src/backend/base/langflow/custom/custom_component/custom_component.py (1)

407-407: Session parameter addition enhances database session management.

The optional session parameter enables external session management, supporting the PR's goal of reducing connection pool exhaustion through session reuse.

src/backend/base/langflow/interface/initialize/loading.py (1)

114-121: Session forwarding to custom component implemented correctly.

The session parameter is properly propagated to custom_component.get_variables, completing the session management chain from the API endpoint to the database operations.

Comment thread src/backend/base/langflow/custom/custom_component/custom_component.py Outdated
@github-actions github-actions Bot added refactor Maintenance tasks and housekeeping and removed refactor Maintenance tasks and housekeeping labels Jul 1, 2025
@github-actions github-actions Bot added refactor Maintenance tasks and housekeeping and removed refactor Maintenance tasks and housekeeping labels Jul 1, 2025

if session is not None:
# Use the provided session instead of creating a new one
return await variable_service.get_variable(user_id=user_id, name=name, field=field, session=session)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I would probably lean towards just building the session within the method by default. Reasoning that any future users of this method also should err on the side of retrieving all variables at once rather than building a session per variable.

database_connection_retry: bool = False
"""If True, Langflow will retry to connect to the database if it fails."""
pool_size: int = 20
pool_size: int = 30
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Would revert these changes

@jordanrfrazier jordanrfrazier self-requested a review July 2, 2025 23:08
@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Jul 2, 2025
@github-actions github-actions Bot added refactor Maintenance tasks and housekeeping and removed refactor Maintenance tasks and housekeeping labels Jul 2, 2025
- Added a new async `get_variables` method in `CustomComponent` to maintain backward compatibility with the deprecated method, ensuring it calls the existing `get_variable` method with session management.
- This change enhances the robustness of the component while preserving existing functionality.
- Eliminated the unused `session_scope` import from the `endpoints.py` file to streamline the code and improve clarity. This change contributes to maintaining a clean and efficient codebase.
@github-actions github-actions Bot added refactor Maintenance tasks and housekeeping and removed refactor Maintenance tasks and housekeeping labels Jul 3, 2025
@github-actions github-actions Bot added refactor Maintenance tasks and housekeeping and removed refactor Maintenance tasks and housekeeping labels Jul 3, 2025
@github-actions github-actions Bot added refactor Maintenance tasks and housekeeping and removed refactor Maintenance tasks and housekeeping labels Jul 3, 2025
@ogabrielluiz ogabrielluiz changed the title refactor: MCP Tools Mode not working as expected refactor: reuse single session when getting variables from db Jul 3, 2025
@github-actions github-actions Bot added refactor Maintenance tasks and housekeeping and removed refactor Maintenance tasks and housekeeping labels Jul 3, 2025
- Modified the `variables` method to call the new `get_variables` method for improved clarity and consistency. This change maintains backward compatibility while encouraging the use of the updated async method.
@github-actions github-actions Bot added refactor Maintenance tasks and housekeeping and removed refactor Maintenance tasks and housekeeping labels Jul 3, 2025
…ave session in update_build_config

- Replaced instances of the deprecated `get_variable` method with the new `get_variables` method in `LMStudioEmbeddingsComponent`, `LMStudioModelComponent`, and `ChatOllamaComponent`. This change enhances code clarity and maintains consistency across components while ensuring backward compatibility.
@github-actions github-actions Bot added refactor Maintenance tasks and housekeeping and removed refactor Maintenance tasks and housekeeping labels Jul 3, 2025
@ogabrielluiz ogabrielluiz added this pull request to the merge queue Jul 3, 2025
Merged via the queue into main with commit b1ae5e4 Jul 3, 2025
66 checks passed
@ogabrielluiz ogabrielluiz deleted the lfoss-1653 branch July 3, 2025 15:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm This PR has been approved by a maintainer refactor Maintenance tasks and housekeeping size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants