fix: close SQLAlchemy sessions in datasource_provider_service#37549
Open
ifer47 wants to merge 1 commit into
Open
fix: close SQLAlchemy sessions in datasource_provider_service#37549ifer47 wants to merge 1 commit into
ifer47 wants to merge 1 commit into
Conversation
The pattern `with Session(db.engine).no_autoflush as session:` creates a Session that is never closed. The `no_autoflush` context manager only manages the autoflush flag — it does NOT close the session when the `with` block exits. This leaks database connections from the connection pool. Both `is_system_oauth_params_exist()` and `get_oauth_client()` are read-only SELECT queries that don't need `no_autoflush` at all. Replace with the standard `with Session(db.engine) as session:` pattern used elsewhere in the codebase, which properly closes the session on exit. Co-Authored-By: zhipu/glm-5 <zai-org@claude-code-best.win>
Contributor
Pyrefly Type Coverage
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The pattern
with Session(db.engine).no_autoflush as session:creates a Session that is never closed. Theno_autoflushcontext manager only manages the autoflush flag — it does NOT close the session when thewithblock exits. This leaks database connections from the connection pool.Two methods in
DatasourceProviderServiceused this pattern:is_system_oauth_params_exist()(line 436) — read-only SELECT queryget_oauth_client()(line 513) — read-only SELECT queryNeither function needs
no_autoflushsince they only perform SELECT queries and never callsession.add()orsession.commit()that would trigger autoflush.Fix
Replaced
with Session(db.engine).no_autoflush as session:with the standardwith Session(db.engine) as session:pattern used elsewhere in the codebase (e.g.,plugin_parameter_service.py,plugin_migration.py,workflow_comment_service.py), which properly closes the session on context manager exit.How the bug works
vs. the correct pattern:
🤖 Generated with Claude Code Best