Skip to content

fix(connection_pool): do not cache cursor when cursor_init fails - #6303

Open
brucearctor wants to merge 1 commit into
SQLMesh:mainfrom
brucearctor:fix/connection-pool-cursor-init-cache
Open

brucearctor wants to merge 1 commit into
SQLMesh:mainfrom
brucearctor:fix/connection-pool-cursor-init-cache

Conversation

@brucearctor

Copy link
Copy Markdown
Contributor

Summary

Fixes a bug in ConnectionPool.get_cursor() (noticed during review of #6300) where the newly created cursor was cached in self._thread_cursors[thread_id] (_ThreadLocalBase) or self._cursor (SingletonConnectionPool) before self._cursor_init(cursor) ran.

If _cursor_init raises (for example, a failed DuckDB extension load, RisingWave session SET, or Doris SWITCH/USE), a subsequent get_cursor() retry on the same thread/pool would return the uninitialized cached cursor and skip _cursor_init.

  • Only cache the cursor after _cursor_init(cursor) succeeds.
  • On failure, close the uninitialized cursor via _try_close(cursor, "cursor") and re-raise.
  • Add parametrized unit test across SingletonConnectionPool, ThreadLocalConnectionPool, and ThreadLocalSharedConnectionPool.

Verification

  • ruff check and ruff format --check --line-length 100: passed
  • mypy sqlmesh/utils/connection_pool.py: passed
  • pytest tests/utils/test_connection_pool.py: 17 passed
Signed-off-by: Bruce Arctor <5032356+brucearctor@users.noreply.github.com>
@coderabbitai

coderabbitai Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Summary

This change caches a cursor only after cursor_init succeeds. If cursor_init raises, the code closes the cursor with _try_close and re-raises the exception. Before this change, a failed cursor could stay cached and be returned without initialization. The change applies to the get_cursor cursor-creation logic in sqlmesh/utils/connection_pool.py. A new parametrized test covers SingletonConnectionPool, ThreadLocalConnectionPool, and ThreadLocalSharedConnectionPool.

Scope: +14/-4 lines, excluding tests and docs. Changed files under sqlmesh/utils: sqlmesh/utils/connection_pool.py. No changed files under sqlmesh/core.

Cursor pools now close a newly created cursor if initialization fails and propagate the exception. They cache the cursor only after successful initialization. Tests cover failure handling, retry, and reuse across three pool classes.

Changes: Cursor pool initialization

Layer / File(s) Summary
Initialize before caching and test failure handling
sqlmesh/utils/connection_pool.py, tests/utils/test_connection_pool.py
_ThreadLocalBase.get_cursor and SingletonConnectionPool.get_cursor now initialize a local cursor before caching it. If initialization fails, they attempt to close the cursor and re-raise the exception. Tests cover failure handling and successful reuse across all three pool classes.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: treysp, adityaanikam, mday-io

Merge Risk: Minimal · up to fe3f2

This change stops a cursor whose initialization failed from being cached and reused, so a retry now re-runs initialization. The change is small and tested, and no merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "fix(connection_pool): do not cache cursor when cursor_init fails" names the main change: the cursor is cached only after cursor_init succeeds. It matches the raw summary, which says the cur…
Description check Passed The description covers the required template areas. The Summary section explains the bug, the fix, and the new test. The Test Plan appears as a Verification section that lists ruff, mypy, and pytest r…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "fix(connection_pool): do not cache cursor when cursor_init fails" names the main change: the cursor is cached only after cursor_init succeeds. It matches the raw summary, which says the cursor is stored in the pool only after initialization succeeds, and the PR objectives, which state the same fix. The title is concise, specific, and follows a conventional-commit style.

Full details: Description check

Explanation

The description covers the required template areas. The Summary section explains the bug, the fix, and the new test. The Test Plan appears as a Verification section that lists ruff, mypy, and pytest results. The Checklist from the template is missing: it does not show whether make style was run, whether all tests pass via make fast-test, or whether commits are signed off with DCO. The 'Test Plan' heading is also not present by name.

Resolution

Optionally add the template's '## Test Plan' heading (the Verification section can move under it) and include the '## Checklist' section with its four items, marking them as applicable. The DCO sign-off status and the make fast-test result are not reported and should be confirmed before merge.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 9fa0b6e and fe3f217.

Files selected for processing: 2

  • sqlmesh/utils/connection_pool.py
  • tests/utils/test_connection_pool.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant