Skip to content

Keep R and Python connections open when Data Connections is enabled - #17228

Merged
juliasilge merged 4 commits into
mainfrom
fix/keep-runtime-connection-comms
Nov 10, 2026
Merged

juliasilge merged 4 commits into
mainfrom
fix/keep-runtime-connection-comms

Conversation

@juliasilge

Copy link
Copy Markdown
Member

The first step toward #16709!

With Data Connections enabled (the default since #16288), Positron disconnects the database connections that R and Python code opens. This PR stops that:

Screenshot 2026-11-09 at 3 58 40 PM

It does not yet show these connections in the Data Connections pane but I'll do an additional PR to add them there.

Cause

PositronConnectionsService closed every connection comm when it arrived, because the old Connections pane is not registered when Data Connections is on. A close from the frontend runs the disconnect function of the connection itself in the kernel:

  1. client.dispose() sends comm_close to the kernel.
  2. In R, Ark receives the close and calls .ps.connection_close(id), which calls con$disconnect().
  3. In Python, _on_comm_close calls _close_connection, which closes the object (for example, sqlite3 calls conn.close()).

As a result, a connection from connections::connection_open() (or from an odbc connection in R) was not valid after the next call. In Python, %connection_show con closed con.

Change

  • PositronConnectionsService now keeps comms whose metadata language_id is r or python open, until the runtime closes them or the session ends. It closes all other comms as before (for example, ggsql comms).
  • The service still records the code of each comm for getSessionConnectionCode; that behavior does not change.
  • A comm that is open at startup can time out before it sends its metadata. Ark answers get_metadata only when R is idle, so a window reload during a long computation could cause this. Such a comm now takes the language of its session. The same rule applies when the metadata request fails. Before, the service closed these comms, and R then disconnected the user's object when it became idle.
  • With Data Connections off, the old Connections pane path does not change.

Release Notes

New Features

  • N/A

Bug Fixes

Validation Steps

@:connections

Unit tests are in positronConnectionsService.vitest.ts and runtimeDataConnections.vitest.ts.

To test this out manually, use a build with Data Connections enabled (the default).

R (needs the connections and RSQLite packages):

con <- connections::connection_open(RSQLite::SQLite(), tempfile())

Check that you have a con object and then run:

DBI::dbIsValid(con@con)

The result is now TRUE. Before this fix, it is FALSE. (connection_open() returns a wrapper, so the DBI connection is in its con slot.)

Python:

import sqlite3
con = sqlite3.connect(":memory:")
%connection_show con

Then run:

con.execute("select 1").fetchone()

The result is (1,). Before this fix, Python shows sqlite3.ProgrammingError: Cannot operate on a closed database.

@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

E2E Tests 🚀
This PR will run tests tagged with: @:critical @:connections

Why these tags?
Tag Source
@:critical Always runs (required)
@:connections PR description

More on automatic tags from changed files.

Test count: Linux: 100 tests.

readme  valid tags

@juliasilge
juliasilge marked this pull request as ready for review November 9, 2026 23:28
@juliasilge
juliasilge merged commit 463c2f0 into main Nov 10, 2026
5 checks passed
@juliasilge
juliasilge deleted the fix/keep-runtime-connection-comms branch November 10, 2026 00:40
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 10, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant