Skip to content

refactor: Own results through a shared pointer, as connections are - #949

Draft
krlmlr wants to merge 4 commits into
claude/rb-param-sourcefrom
claude/rb-result-ptr
Draft

krlmlr wants to merge 4 commits into
claude/rb-param-sourcefrom
claude/rb-result-ptr

Conversation

@krlmlr

@krlmlr krlmlr commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

No change of behavior. The external pointer of a result now owns a DbResultPtr, a boost::shared_ptr<DbResult>, as the external pointer of a connection owns a DbConnectionPtr. A later layer gives an Arrow stream a share of the result, so that the stream can share the result's lifetime.

  • DbResult.h declares DbResultPtr next to DbConnectionPtr and makes ~DbResult() virtual, because the shared pointer deletes the SqliteResult that create_and_send_query() returns through a DbResult*.
  • result_create() wraps the new result in a heap-allocated DbResultPtr owned by the tagged external pointer. dbClearResult() (result_release()) and the finalizer delete that DbResultPtr, which drops R's share. R's share is the only one, so a result, its statement and its share of the connection are freed at the same moments as before.
  • The as_cpp() conversions for DbResult* and SqliteResult* still check the RSQLite_result tag, then read the result through the shared pointer. result_release() and result_valid() keep the NULL-address handling for cleared, restored and untagged pointers, and the busy guard of fix: dbClearResult() refuses a result while one of its fetches or binds runs #934 looks through the shared pointer. result_fetch() and its error value from fix: The error of a fetch carries the rows it read before the error #945 are unchanged, as is the connection handling of fix: Connection entry points refuse a disconnected or foreign pointer #942. The checks for an empty shared pointer never fire today, because create_and_send_query() throws instead of returning NULL.

Tests: five tests ported from #856 to test-dbClearResult.R pin validity before and after dbClearResult(), the error of every use of a cleared result, a result cleared by the next query, a result collected by gc() without being cleared, and dbDisconnect() with results in use, after which the last result keeps a read lock until it is collected. With the finalizer disabled, the last two fail. The new tests pass on the base as well. The full suite passes with NOT_CRAN=true (FAIL 0, WARN 3 from unavailable locales, SKIP 63, PASS 11328). A script of lifetime scenarios (clear then gc(), a clear refused while a fetch runs and done after, dbDisconnect() first, the connection collected before or together with an uncleared result, failing creates and create/clear loops, a serialized result, objects left to the exit finalizers) prints the same on the base and on this branch, also with a garbage collection every 20 allocations (gctorture2(20), with fewer loop iterations), and valgrind's memcheck reports no errors and no definitely or indirectly lost bytes.

Still applies from #856: the disconnect test sees the finalizer through "database is locked" from a second connection, so it relies on SQLite's default rollback journal and on RSQLite setting no busy timeout by default (question on #856, #856 (comment), default kept: the test stays as it is and is adjusted if either default changes; a comment in the test now says so). #856's two review points are carried in: the virtual destructor, and the disconnect test's check that collecting the last result releases its lock.

Layer R2 of the rebuilt chain, planned in #880; based on #947. Ported from #856 (draft).

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


https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4


Generated by Claude Code

The external pointer of a result now owns a `DbResultPtr`,
a `boost::shared_ptr<DbResult>`,
as that of a connection owns a `DbConnectionPtr`,
so that a later layer can give an Arrow stream a share of the result.
R's share is the only one,
so a result is freed at the same moments as before,
by `dbClearResult()` or by the finalizer.
`~DbResult()` is virtual,
because the shared pointer deletes a `SqliteResult` through a `DbResult*`.
The result tag and the busy guard work as before.

Ported from #856.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
@krlmlr
krlmlr added this pull request to stack #928 December 1, 2026 03:04

krlmlr commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

Question: make the disconnect test independent of the journal mode and the busy timeout?

Carried over from #856's question, which nobody answered; this pull request ports #856 and its disconnect test, whose code comment now says what it relies on. The code links below point at #856's lines.

The new test "disconnecting with an open result warns, the result holds the connection" checks that the finalizer releases the last result after dbDisconnect() by writing from a second connection: the write fails with "database is locked" until gc() collects the result (tests/testthat/test-dbClearResult.R#L114-L120). That relies on SQLite's default rollback journal, since in WAL mode a reader doesn't block a writer, and on RSQLite setting no busy timeout by default, since with one the write waits instead of failing at once. If either default changes, the test fails and its expectation has to be adjusted.

  • Keep the test as it is: a change of either default makes it fail visibly, and it is adjusted then (recommended)
  • Set the journal mode and the busy timeout explicitly in the test, so that it doesn't depend on the defaults

Kirill decides. If nobody decides, the test stays as it is, and the limitation stays checked in the "Accepted limitations".


Generated by Claude Code

`result_create()` holds the new `DbResultPtr` in a `std::unique_ptr`
until the finalizer of its external pointer is registered,
so that a failed allocation in R no longer leaks the result,
its statement and its share of the connection.
It registers the finalizer through `cpp11::safe` for the same reason,
and returns the external pointer's SEXP,
as cpp11's move constructor would drop the tag and the finalizer.

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

Development

Successfully merging this pull request may close these issues.

2 participants