Skip to content

fix(fabric): close connections when any thread references a dropped warehouse - #6284

Open
mday-io wants to merge 1 commit into
SQLMesh:mainfrom
mday-io:fix/fabric-drop-catalog-close-all-threads
Open

mday-io wants to merge 1 commit into
SQLMesh:mainfrom
mday-io:fix/fabric-drop-catalog-close-all-threads

Conversation

@mday-io

@mday-io mday-io commented Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Why

engine-tests-cloud (fabric) fails on every main run in test_janitor[fabric-catalog]. The janitor deletes the dev warehouse, for example ci_728e0c04__dev. The next DROP TABLE in the prod warehouse then fails to connect: Could not login because the authentication failed. (18456) (SQLDriverConnect). Fabric returns that error when a connection's DATABASE= names a warehouse that doesn't exist.

The Fabric adapter keeps each thread's catalog in two thread-local attributes on the connection pool: target_catalog and connected_catalog. _drop_catalog closed connections only when the calling thread referenced the dropped warehouse. Other threads were not checked. A worker thread that switched to the dev warehouse during plan promotion could still target it or hold a connection on it after the warehouse was deleted. So could a new thread that reuses a finished thread's ident and inherits its pool state.

How

  • _drop_catalog now checks the target_catalog and connected_catalog of every thread with get_all_attributes. It also checks the calling thread's resolved catalog, because a thread with no attributes yet still resolves to the default warehouse. If any of these name the dropped warehouse, it closes all connections, which also clears every thread's catalog state. When nothing references the warehouse, it leaves connections alone, as before.
  • get_all_attributes iterates without a lock, so a thread that starts at the same moment can raise RuntimeError. In that case the adapter closes all connections.
  • Two log lines will help diagnose the next main run:
    • An INFO line names which state matched when connections are closed.
    • A WARNING line names the warehouse when creating a connection fails.

This removes the stale-state cause. I haven't confirmed that it explains the CI failure. If Fabric itself briefly rejects logins after a warehouse is deleted, the job will keep failing. The new logs will then show that the failed connection targeted an existing warehouse.

Test Plan

  • New tests in tests/core/engine_adapter/test_fabric.py. Each one uses real threads on a thread-local pool:
    • another thread still targets the dropped warehouse
    • another thread lazily restored to the default with its connection left on the dropped warehouse
    • another thread targets it with no open connection, which is the likely CI case
    • a fresh calling thread drops the default warehouse
    • no thread references the warehouse, so connections are kept
    • get_all_attributes raises, so connections are closed
    • the connection-failure WARNING names the warehouse
  • With fabric.py reverted to main, 5 of the new tests fail and 2 pass. The 2 that pass are the negative control and a check that main already covered. With the fix, pytest tests/core/engine_adapter/test_fabric.py tests/utils/test_connection_pool.py passes: 48 passed.
  • ruff check, ruff format --check and mypy pass on the changed files.
  • make fast-test ran in a local venv that is missing optional packages (pyodbc, agate, pyspark, PyGithub). Its failures are import errors, and the Fabric connection-config tests fail the same way on main. I'm relying on CI for the full fast-test run.
  • Cloud engine tests run only on main, so the Fabric integration test runs after merge.

Checklist

  • I have run make style and fixed any issues
  • I have added tests for my changes (if applicable)
  • All existing tests pass (make fast-test)
  • My commits are signed off (git commit -s) per the DCO

Summary by CodeRabbit

  • Bug Fixes
    • Dropping a warehouse now closes connections associated with it, including connections tracked across threads, while keeping connections to other warehouses open.
    • Dropping the configured default warehouse also closes connections when their catalog state is neutral or cannot be inspected.
    • If a connection cannot be created, the warning identifies the target warehouse, and the original error continues to be reported.
…arehouse

_drop_catalog only checked the calling thread's catalog state before
deciding whether to close connections. Another thread could still target,
or hold a connection on, the dropped warehouse, and Fabric rejects a
connection to a missing warehouse with an authentication error (18456).

Check every thread's target and connected catalog, and log which state
matched and which warehouse a failed connection targeted.

Signed-off-by: mday-io <mdaytn@gmail.com>
@coderabbitai

coderabbitai Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The Fabric adapter now logs the target warehouse when connection creation fails. When dropping a warehouse, it checks catalog state across threads and closes connections when that state references the warehouse or cannot be inspected.

Changes: Fabric connection and warehouse cleanup

Layer / File(s) Summary
Connection failure logging
sqlmesh/core/engine_adapter/fabric.py, tests/core/engine_adapter/test_fabric.py
The connection factory logs a warning with the target warehouse when connection creation fails, then re-raises the exception. Tests verify the warning and exception propagation.
Warehouse-drop connection cleanup
sqlmesh/core/engine_adapter/fabric.py, tests/core/engine_adapter/test_fabric.py
Warehouse removal checks the current catalog and thread target and connected catalogs. It closes connections when a catalog matches the dropped warehouse or thread inspection raises RuntimeError. Tests cover matching, unrelated, default, and uninspectable thread state.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Possibly related PRs

  • SQLMesh/sqlmesh#6247: Both PRs change when Fabric connections are closed after warehouse deletion.

Suggested reviewers: fresioas, cmgoffena13

Merge Risk: Minimal · up to b40b2

Fabric connection failures now log the target warehouse. Dropping a warehouse now closes connections when any thread references it. Cleanup flows reach this path only through isolated cleanup adapters, so no concrete merge-blocking risk remains. The Fabric integration test runs only on main, so real-environment confirmation comes after merge.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: _drop_catalog now closes connections when any thread references a dropped Fabric warehouse. It matches the raw summary and the PR objectives. It is 77 characters, with…
Description check Passed The description follows the repository template. It has a Description section with Why and How subsections, a Test Plan section that lists the new tests, the commands run, and the results, and a Check…
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 names the main change: _drop_catalog now closes connections when any thread references a dropped Fabric warehouse. It matches the raw summary and the PR objectives. It is 77 characters, within a reasonable length for a single-sentence title, and uses the conventional fix(fabric) prefix.

Full details: Description check

Explanation

The description follows the repository template. It has a Description section with Why and How subsections, a Test Plan section that lists the new tests, the commands run, and the results, and a Checklist. The checklist leaves the make style and make fast-test items unchecked, and the author explains why fast-test could not fully run locally. The description is detailed and specific, and it states its own uncertainty about the root cause.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
Fix failing CI checks

  • 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

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 0dff3588-6add-4b40-ba79-1c3dd93bd32d
Commits

Reviewing files that changed from the base of the PR and between cfde8eb and b40b249.

Files selected for processing: 2

  • sqlmesh/core/engine_adapter/fabric.py
  • tests/core/engine_adapter/test_fabric.py

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

@github-actions github-actions Bot added the Bug Something isn't working label Nov 30, 2026
@mday-io
mday-io marked this pull request as ready for review November 30, 2026 17:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Something isn't working

1 participant