Skip to content

[fix] KeyError crash when completing right after USE#1934

Merged
scottnemes merged 1 commit into
dbcli:mainfrom
scottnemes:fix/completions-use-refresh-race-condition
Jun 17, 2026
Merged

[fix] KeyError crash when completing right after USE#1934
scottnemes merged 1 commit into
dbcli:mainfrom
scottnemes:fix/completions-use-refresh-race-condition

Conversation

@scottnemes

@scottnemes scottnemes commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Description

Issue

Running USE immediately points the live completer at the new schema so unqualified completions reflect the switch, but that schema's tables are still loading on a background thread. If you triggered a completion in that window, mycli crashed:

File ".../mycli/sqlcompleter.py", line 1757, in populate_scoped_cols
    for table in meta["tables"][self.dbname]:
                 ~~~~~~~~~~~~~~^^^^^^^^^^^^^
KeyError: 'new_db'

To reproduce (difficult unless you have a large database and/or slow computer):

USE some_db;
SELECT 

Fix

populate_scoped_cols now reads the per-schema metadata with meta["tables"].get(self.dbname, {}), snapshotting it once. During the load window it suggests * instead of raising an error, and shows real columns once the refresh completes. Grabbing the reference once also avoids a concurrent refresh swapping the dict out mid-iteration.

Added regression tests in test_completer_use_switch.py covering the unloaded-schema window, the post-load result, and concurrent USE switching.

Fixes #1933

Checklist

  • I added this contribution to the changelog.md file.
  • I added my name to the AUTHORS file (or it's already there).
  • To lint and format the code, I ran
    uv run ruff check && uv run ruff format && uv run mypy --install-types .

@scottnemes scottnemes self-assigned this Jun 17, 2026
@scottnemes scottnemes requested a review from rolandwalker June 17, 2026 00:58
@scottnemes scottnemes merged commit bbda180 into dbcli:main Jun 17, 2026
12 of 13 checks passed
@scottnemes scottnemes deleted the fix/completions-use-refresh-race-condition branch June 17, 2026 15:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] Crash when fetching incomplete completer metadata

2 participants