Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 3, 2025

When multiple editors view the same SQL file (split views or tabs), they share a single QueryRunner instance and SQL Server session (SPID). This causes UI state synchronization issues: if Tab 1 starts a transaction, Tab 2's completed query incorrectly shows "executing..." until Tab 1 commits.

Changes

Key generation for editor instances

  • Added getEditorInstanceKey() to generate unique keys per editor using URI + viewColumn (e.g., "file:///test.sql::2")
  • Added getDocumentUriFromEditorKey() to extract document URI from instance keys

Execution state isolation

  • SqlOutputContentProvider: Keys QueryRunner instances by editor instance key instead of document URI
  • QueryRunner: Tracks both document URI (for SQL Tools Service/connection lookup) and editor instance key (for StatusView/UI state)
  • MainController: Uses editor instance keys for query execution while connections remain keyed by document URI (shared)

Document lifecycle handling

  • onDidCloseTextDocument(): Cleans up all editor instances for closed document (handles keys matching documentUri or documentUri::*)
  • onUntitledFileSaved() / updateQueryRunnerUri(): Remaps all editor instances when document is saved/renamed

Architecture

// Before: All editors shared same QueryRunner
_queryResultsMap["file:///test.sql"] = QueryRunnerState

// After: Each editor gets its own QueryRunner
_queryResultsMap["file:///test.sql::1"] = QueryRunnerState  // Tab 1
_queryResultsMap["file:///test.sql::2"] = QueryRunnerState  // Tab 2

// QueryRunner internals
constructor(
  documentUri,        // "file:///test.sql" - for SQL Tools Service
  editorInstanceKey   // "file:///test.sql::2" - for UI state
)

Connections remain keyed by document URI (shared), while execution state and SPIDs are isolated per editor instance.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • update.code.visualstudio.com
    • Triggering command: /usr/local/bin/node ./out/test/unit/runTest.js --grep Editor Instance Key (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>Keeps running while in transaction</issue_title>
<issue_description>
Type: Bug

begin tran
delete command
exec sp
select
while committing (or even on previous commands), the editor keep "executing query"

Extension version: 1.30.0
VS Code version: Code 1.99.2 (4949701c880d4bdb949e3c0e6b400288da7f474b, 2025-04-10T01:21:25.295Z)
OS version: Windows_NT x64 10.0.26100
Modes:

System Info
Item Value
CPUs 12th Gen Intel(R) Core(TM) i7-1255U (12 x 2611)
GPU Status 2d_canvas: enabled
canvas_oop_rasterization: enabled_on
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
multiple_raster_threads: enabled_on
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
skia_graphite: disabled_off
video_decode: enabled
video_encode: enabled
vulkan: disabled_off
webgl: enabled
webgl2: enabled
webgpu: enabled
webnn: disabled_off
Load (avg) undefined
Memory (System) 15.68GB (2.06GB free)
Process Argv --crash-reporter-id be4b83fa-1a15-441d-a95b-0802ecbe4b43
Screen Reader no
VM 0%
A/B Experiments
vsliv368:30146709
vspor879:30202332
vspor708:30202333
vspor363:30204092
vscod805cf:30301675
binariesv615:30325510
c4g48928:30535728
azure-dev_surveyone:30548225
a9j8j154:30646983
962ge761:30959799
2e7ec940:31000449
pythontbext0:30879054
cppperfnew:31000557
dwnewjupyter:31046869
pythonrstrctxt:31112756
nativeloc1:31192215
5fd0e150:31155592
dwcopilot:31170013
6074i472:31201624
dwoutputs:31242946
customenabled:31248079
hdaa2157:31222309
copilot_t_ci:31222730
e5gg6876:31282496
pythoneinst12cf:31262606
bgtreat:31268568
4gafe986:31271826
31787653:31262186
3e8i5726:31271747
49da9784:31282606
useunpkgapi:31280918
747dc170:31275177
aj496949:31278748
aj953862:31281341

<agent_instructions>Problem Statement
When a user opens the same SQL file in multiple editor instances (tabs, split views, or windows), all editors share the same query execution state and SQL Server session (SPID). This causes critical UI state synchronization issues:

Primary Issue - Shared Execution State:
When multiple tabs/editors are open for the same file and they share the same SPID:

Tab 1 executes a query that starts a transaction (e.g., BEGIN TRANSACTION; UPDATE table SET column = value;)
Tab 2 executes a different query (e.g., SELECT * FROM another_table;)
Problem: Tab 2's query completes successfully and returns results, BUT the UI shows the query as still "executing" indefinitely because Tab 1's transaction hasn't been committed
The execution state is shared between both tabs, so Tab 1's active transaction causes Tab 2's UI to remain in "executing" state even though Tab 2's query finished
Secondary Issue - Session Sharing:
Both editors use the same SQL Server session (SPID), which means:

Any transaction state from one editor affects the other
Query execution contexts are not isolated
Users cannot work independently in different tabs of the same file
Reproduction Steps:

Open a SQL file (e.g., test.sql)
Split the editor or open the same file in a second tab
In Tab 1: Execute BEGIN TRANSACTION; UPDATE table SET column = value; (leaves transaction open)
In Tab 2: Execute SELECT * FROM another_table; (different query, no locks needed)
Result: Tab 2 gets query results successfully, but the UI shows "executing..." indefinitely until Tab 1 commits the transaction
Root Cause
The extension currently keys all connection state, query runners, and execution state tracking by document URI only. When multiple editors view the same document, they all share:

The same QueryRunner instance
The same execution state tracking in SqlOutputContentProvider
The same SQL Server session (SPID)
The same status bar state in StatusView
This means UI updates and execution state from one editor instance overwrite or interfere with another editor's state.

Required Solution
Implement per-editor-instance isolation for query execution state and SQL Server sessions while maintaining shared connection profiles:

Generate unique editor instance keys using both document URI and VS Code's viewColumn property (e.g., "file:///path/to/file.sql::1", "file:///path/to/file.sql::2")

Separate connection sharing from execution context: Connection credentials can be shared, but each editor instance must get:

Its own QueryRunner with independent execution state tracking
Its own SQL Server session (SPID) for query execution
Its own status bar state showing query execution status
Update state management across these components:

ConnectionManager: Track execution contexts per editor instance while sharing connection profiles
SqlOutputContentProvider: Key query runners by editor instance, not just document URI - each editor needs its own QueryRunnerState
QueryRunner: Us...


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 4 commits November 3, 2025 23:38
- Add getEditorInstanceKey() helper to generate unique keys per editor (URI + viewColumn)
- Update VscodeWrapper to provide activeTextEditorInstanceKey property
- Update MainController to use editor instance keys for query execution
- Update SqlOutputContentProvider to key QueryRunner instances by editor instance
- Update QueryRunner to track both document URI (for SQL Tools Service) and editor instance key (for UI state)
- Update StatusView calls to use editor instance key for per-editor status tracking

This ensures each editor viewing the same document has independent query execution state.

Co-authored-by: allancascante <755488+allancascante@users.noreply.github.com>
- Update onDidCloseTextDocument to clean up all editor instances
- Update onUntitledFileSaved to remap all editor instances
- Update updateQueryRunnerUri to migrate all editor instances to new URI
- Ensure proper cleanup of actual plan statuses per editor instance

This ensures editor instances are properly managed during document lifecycle events.

Co-authored-by: allancascante <755488+allancascante@users.noreply.github.com>
- Test getEditorInstanceKey generates unique keys per editor
- Test getDocumentUriFromEditorKey extracts URI correctly
- Verify different view columns generate different keys
- Verify same view column generates same key
- Test edge cases (undefined editor, missing view column)

Co-authored-by: allancascante <755488+allancascante@users.noreply.github.com>
The function can return undefined, so update the return type from 'string' to 'string | undefined' to match the actual behavior.

Co-authored-by: allancascante <755488+allancascante@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix editor executing query during transaction Fix: Isolate query execution state per editor instance to prevent shared SPID issues Nov 3, 2025
Copilot AI requested a review from allancascante November 3, 2025 23:52
Copilot finished work on behalf of allancascante November 3, 2025 23:52
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.

Keeps running while in transaction

2 participants