codeant-ai-for-open-source[bot] commented on code in PR #40194:
URL: https://github.com/apache/superset/pull/40194#discussion_r3255649726
##########
superset/models/core.py:
##########
@@ -468,13 +468,31 @@ def get_sqla_engine( # pylint: disable=too-many-arguments
engine_context_manager = app.config["ENGINE_CONTEXT_MANAGER"]
with engine_context_manager(self, catalog, schema):
with check_for_oauth2(self):
- yield self._get_sqla_engine(
+ engine = self._get_sqla_engine(
catalog=catalog,
schema=schema,
nullpool=nullpool,
source=source,
sqlalchemy_uri=sqlalchemy_uri,
)
+ prequeries = self.db_engine_spec.get_prequeries(
+ database=self,
+ catalog=catalog,
+ schema=schema,
+ )
+ if prequeries:
+
+ def run_prequeries(
+ dbapi_connection: Any,
+ connection_record: Any, # pylint:
disable=unused-argument
+ ) -> None:
+ cursor = dbapi_connection.cursor()
+ for prequery in prequeries:
+ cursor.execute(prequery)
+ cursor.close()
Review Comment:
**Suggestion:** The cursor in the connect-event callback is only closed on
the success path; if any prequery execution raises, `cursor.close()` is
skipped. Over time this can leak DBAPI cursor resources on failed connection
setup. Wrap cursor usage in `try/finally` (or `contextlib.closing`) so cleanup
always happens even when a prequery fails. [missing cleanup]
<details>
<summary><b>Severity Level:</b> Major ⚠️</summary>
```mdx
- ⚠️ Failed prequeries can leak cursors per connection attempt.
- ⚠️ Repeated failures may slowly increase backend resource usage.
```
</details>
<details>
<summary><b>Steps of Reproduction ✅ </b></summary>
```mdx
1. Consider a database engine spec that returns a failing prequery when a
schema is
provided, for example PostgreSQL with a non-existent schema so that
`PostgresEngineSpec.get_prequeries()` (implemented at
`superset/db_engine_specs/postgres.py:679-705`) returns `['set search_path =
\"bad_schema\"']`, which will cause an error on execution.
2. Any Superset code path that acquires an engine via
`DBEngineSpec.get_engine(database,
catalog, schema, source)` (wrapper defined at
`superset/db_engine_specs/base.py:1048-1067`) or directly via
`Database.get_sqla_engine(...)` (context manager at
`superset/models/core.py:468-495`)
will create a SQLAlchemy engine and, when
`db_engine_spec.get_prequeries(...)` returns a
non-empty list, register the `run_prequeries` callback on the engine
`"connect"` event at
`superset/models/core.py:485-494`.
3. When the first DBAPI connection is opened from that engine (e.g. via
`engine.connect()`
in query execution code using `DBEngineSpec.get_engine`), SQLAlchemy fires
the `"connect"`
event and calls `run_prequeries` (core callback at
`superset/models/core.py:485-492`),
which creates a cursor with `cursor = dbapi_connection.cursor()` and
iterates `for
prequery in prequeries: cursor.execute(prequery)`; when it executes the
invalid `set
search_path` statement, the driver raises, and the function exits before
reaching
`cursor.close()` on line 492.
4. Because `run_prequeries` does not wrap the cursor in a `try/finally` or
context
manager, the DBAPI cursor created at line 490 remains unclosed on the
failure path,
relying on connection teardown or garbage collection for cleanup; repeated
failing
connection attempts with misconfigured or permission-denied prequeries will
accumulate
unclosed cursors on the database side until the process or connections are
recycled.
```
</details>
[Fix in
Cursor](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt=This%20is%20a%20comment%20left%20during%20a%20code%20review.%0A%0A%2A%2APath%3A%2A%2A%20superset%2Fmodels%2Fcore.py%0A%2A%2ALine%3A%2A%2A%20485%3A492%0A%2A%2AComment%3A%2A%2A%0A%09%2AMissing%20Cleanup%3A%20The%20cursor%20in%20the%20connect-event%20callback%20is%20only%20closed%20on%20the%20success%20path%3B%20if%20any%20prequery%20execution%20raises%2C%20%60cursor.close%28%29%60%20is%20skipped.%20Over%20time%20this%20can%20leak%20DBAPI%20cursor%20resources%20on%20failed%20connection%20setup.%20Wrap%20cursor%20usage%20in%20%60try%2Ffinally%60%20%28or%20%60contextlib.closing%60%29%20so%20cleanup%20always%20happens%20even%20when%20a%20prequery%20fails.%0A%0AValidate%20the%20correctness%20of%20the%20flagged%20issue.%20If%20correct%2C%20How%20can%20I%20resolve%20this%3F%20If%20you%20propose%20a%20fix%2C%20implement%20it%20and%20please%20make%20it%20concise.%0AOnce%20fix%20is%20implemented%2C%20also%20check%20other%20c
omments%20on%20the%20same%20PR%2C%20and%20ask%20user%20if%20the%20user%20wants%20to%20fix%20the%20rest%20of%20the%20comments%20as%20well.%20if%20said%20yes%2C%20then%20fetch%20all%20the%20comments%20validate%20the%20correctness%20and%20implement%20a%20minimal%20fix%0A)
| [Fix in VSCode
Claude](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt=This%20is%20a%20comment%20left%20during%20a%20code%20review.%0A%0A%2A%2APath%3A%2A%2A%20superset%2Fmodels%2Fcore.py%0A%2A%2ALine%3A%2A%2A%20485%3A492%0A%2A%2AComment%3A%2A%2A%0A%09%2AMissing%20Cleanup%3A%20The%20cursor%20in%20the%20connect-event%20callback%20is%20only%20closed%20on%20the%20success%20path%3B%20if%20any%20prequery%20execution%20raises%2C%20%60cursor.close%28%29%60%20is%20skipped.%20Over%20time%20this%20can%20leak%20DBAPI%20cursor%20resources%20on%20failed%20connection%20setup.%20Wrap%20cursor%20usage%20in%20%60try%2Ffinally%60%20%28or%20%60contextlib.closing%60%29%20so%20cleanup%20always%20happens%20even%20when%20a%20pr
equery%20fails.%0A%0AValidate%20the%20correctness%20of%20the%20flagged%20issue.%20If%20correct%2C%20How%20can%20I%20resolve%20this%3F%20If%20you%20propose%20a%20fix%2C%20implement%20it%20and%20please%20make%20it%20concise.%0AOnce%20fix%20is%20implemented%2C%20also%20check%20other%20comments%20on%20the%20same%20PR%2C%20and%20ask%20user%20if%20the%20user%20wants%20to%20fix%20the%20rest%20of%20the%20comments%20as%20well.%20if%20said%20yes%2C%20then%20fetch%20all%20the%20comments%20validate%20the%20correctness%20and%20implement%20a%20minimal%20fix%0A)
*(Use Cmd/Ctrl + Click for best experience)*
<details>
<summary><b>Prompt for AI Agent 🤖 </b></summary>
```mdx
This is a comment left during a code review.
**Path:** superset/models/core.py
**Line:** 485:492
**Comment:**
*Missing Cleanup: The cursor in the connect-event callback is only
closed on the success path; if any prequery execution raises, `cursor.close()`
is skipped. Over time this can leak DBAPI cursor resources on failed connection
setup. Wrap cursor usage in `try/finally` (or `contextlib.closing`) so cleanup
always happens even when a prequery fails.
Validate the correctness of the flagged issue. If correct, How can I resolve
this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask
user if the user wants to fix the rest of the comments as well. if said yes,
then fetch all the comments validate the correctness and implement a minimal fix
```
</details>
<a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40194&comment_hash=d145c10587080431ca5b37c595f0e220e087c0e8625711063b98e3940daed56b&reaction=like'>👍</a>
| <a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40194&comment_hash=d145c10587080431ca5b37c595f0e220e087c0e8625711063b98e3940daed56b&reaction=dislike'>👎</a>
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]