Skip to content

fix: Connection entry points refuse a disconnected or foreign pointer - #942

Draft
krlmlr wants to merge 3 commits into
claude/rb-result-tagfrom
claude/rb-connection-tag
Draft

krlmlr wants to merge 3 commits into
claude/rb-result-tagfrom
claude/rb-connection-tag

Conversation

@krlmlr

@krlmlr krlmlr commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

sqliteIsTransacting() on a disconnected connection, and sqliteCopyDatabase() from one, now fail with "Invalid or closed connection" instead of crashing R. A connection that was saved and restored fails the same way instead of with "bad_weak_ptr", also one saved by RSQLite 3.54.1 or earlier (without the tag) or made with new("SQLiteConnection"). initExtension() on a disconnected connection, and sqliteCopyDatabase() to one, fail the same way instead of with "disconnected". dbIsValid() and dbDisconnect() keep their behavior for both (FALSE, and TRUE with the "Already disconnected" warning).

The internal entry points that take a connection read any external pointer as one: given a result's pointer, connection_valid() returned TRUE and connection_release() disconnected the result's connection, and given a pointer that holds NULL, connection_in_transaction() and connection_copy_database() crashed R. connection_connect() now tags the pointer with the symbol RSQLite_connection, the same design as the result tag of #937. The functions that need a live connection (connection_in_transaction(), connection_limit_length(), connection_copy_database(), connection_import_file(), set_busy_handler(), extension_load(), result_create()) now take DbConnectionPtr* through an as_cpp() specialization in RSQLite_types.h that checks the address ("Invalid or closed connection", with or without the tag, as connection_valid() does) and then the tag ("Invalid connection"); this replaces an unused as_cpp() in connection.cpp that said "Invalid result set". connection_valid() and connection_release() treat a pointer that holds NULL as closed, with or without the tag, and check the tag otherwise, like result_valid() and result_release() in #937. DbConnection::in_transaction() and DbConnection::copy_to() now call check_connection(): they passed the NULL handle of a closed connection to SQLite. DbConnection::conn() calls it too, instead of failing with "disconnected".

sqliteCopyDatabase() with the same connection as source and destination crashed R (also in RSQLite 3.54.1): sqlite3_backup_init() returns NULL and copy_to() dereferenced it. It now fails with SQLite's error from the destination, "Could not start copy:\nsource and destination must be distinct". A failed copy step now calls sqlite3_backup_finish(), which releases the backup and its write lock on the destination; before, another connection could not write to the destination. This fix is ported down from #943, which raises the same errors as classed conditions.

Tests: the new tests in test-dbConnect.R, test-transactions.R and test-sqliteCopyDatabase.R run the calls that could crash in a callr session. On the base branch, the three callr tests fail because the session crashes, and the two in-process tests fail with "bad_weak_ptr"; with this change all pass. Two in-process tests in test-sqliteCopyDatabase.R cover the copy: the same-connection copy crashes a callr session on the base branch and fails with the error above with this change; a copy into a WAL database with a different page size fails after locking the destination, and on the base branch a second connection then fails to write to the destination with "database is locked", while with this change it writes and the source and destination stay usable. Full suite with NOT_CRAN=true: FAIL 0, WARN 3 (locale), SKIP 63, PASS 11060.

The crash of #940 (a busy handler that clears the result whose bind waits for the lock) is fixed by #934: the reprex from #940 ends with "database is locked" on the base branch instead of a segfault, and #934's test "a busy handler can't clear the result whose bind waits for it" in test-dbClearResult.R covers that case, so this PR adds no test for it. The other items of #940 (a handler that disconnects its own connection, documentation) remain open.

Layer B7 of the rebuilt chain, planned in #880; based on #937. New in the rebuilt chain, not ported from an old PR.

Closes #938.
Closes #939.

Coordinating session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4



Generated by Claude Code

connection_connect() tags the external pointer with `RSQLite_connection`,
and every entry point that takes a connection checks the tag,
as result pointers do since #937.
A pointer that isn't a connection's fails with "Invalid connection"
instead of being read as one, which let connection_release() disconnect a result's connection.
connection_valid() and connection_release() keep treating a pointer that holds NULL as closed, with or without the tag;
the other entry points fail for it instead of crashing R.

sqliteIsTransacting() and sqliteCopyDatabase() from a disconnected connection
fail with "Invalid or closed connection" instead of passing a NULL handle to SQLite and crashing R.

Closes #938.
Closes #939.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
A connection pointer that holds NULL fails with "Invalid or closed connection" before the tag is checked,
as connection_valid() does,
so that a connection saved by RSQLite 3.54.1 or earlier fails as NEWS describes.
DbConnection::conn() fails with the same message instead of "disconnected",
which covers initExtension() and sqliteCopyDatabase() to a disconnected connection.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
…finish the copy

`sqlite3_backup_init()` returns `NULL` when it can't start the copy,
for instance for the same connection as source and destination,
and the copy dereferenced it and crashed R.
It now fails with SQLite's error from the destination, such as "source and destination must be distinct".

A copy step that fails now finishes the backup,
which releases the backup and its write lock on the destination.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4

This branch has not been deployed

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

Labels

None yet

Projects

None yet

2 participants