Skip to content

fix: dbClearResult() refuses a result while one of its fetches or binds runs - #934

Draft
krlmlr wants to merge 6 commits into
claude/rb-fetch-row-aheadfrom
claude/rb-busy-guard
Draft

krlmlr wants to merge 6 commits into
claude/rb-fetch-row-aheadfrom
claude/rb-busy-guard

Conversation

@krlmlr

@krlmlr krlmlr commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

A calling handler of a warning that dbFetch() or dbColumnInfo() raises, or a busy handler that SQLite calls while a fetch or dbBind() steps, runs R code while the C++ code of the result is on the stack. When such a handler called dbClearResult() on that result, the result and its statement were freed under the running code: R crashed when the fetch went on (for example after invokeRestart("muffleWarning")), and a fetch, bind or dbColumnInfo() of the same result from the handler stepped the statement under the running fetch, so the outer fetch lost rows and dbGetRowCount() counted wrong.

What changes for users: a result is marked busy while one of its fetches or binds runs, or while dbColumnInfo() reads its first row. dbClearResult() on a busy result fails with "Can't clear a result, or send a query on its connection, while one of its fetches or binds is running, as from a warning or busy handler." and leaves the result as it is. A fetch, a bind or dbColumnInfo() of a busy result fails with "Can't fetch from or bind to a result, or read its column info, while one of its fetches or binds is running, …". A new query on the same connection from such a handler fails with the first message, because dbSendQuery() clears the connection's open result first. Exiting handlers (tryCatch()), on.exit(dbClearResult(res)) and garbage collection work as before. The mark is released on every exit path, also when an R error longjmps past the C++ frames, through R_ExecWithCleanup(); C++ exceptions are caught inside the guarded body and rethrown after it, so none crosses R's C frames. The warning for a fetch from a statement that is no query is now raised through cpp11::warning(), so that an error made of it (for example under options(warn = 2)) unwinds the C++ frames instead of longjmping past them; its condition call is now NULL instead of result_fetch(res@ptr, n = n).

What this layer does not change: an error that leaves a fetch still leaves the result where it was (layer B4 completes it), and a failed bind keeps its state (layer B5).

Tests: eight new tests in tests/testthat/test-dbClearResult.R. Seven run the handler in a callr session, because the unguarded code crashes R, and skip unless that session loads the RSQLite under test (tests/testthat/helper-callr.R from #830, now with skip_if_not_installed("callr")). On the base branch all seven fail: four because the session crashes (muffled fetch warning, dbColumnInfo() warning, busy handler during dbBind(), re-entrant fetch/bind/column info/new query), and three because dbClearResult() freed the result without an error, so it was no longer valid afterwards. With this change they pass. The eighth test, in process, pins that a result can be cleared once its fetch is left, by an exiting handler or by an error under options(warn = 2). Full suite with NOT_CRAN=true: FAIL 0, WARN 3 (locale), SKIP 63, PASS 10820.

Layer B3 of the rebuilt chain, planned in #880; based on #933. Ported from #830 (now a draft): commit ae87b09 (run_guarded(), result_release() refusing a busy result), the Rf_warning() → cpp11::warning() change of 1d0d70f, and helper-callr.R; the guard and its messages are those of #795 without its Arrow and thread parts. #830's open question on the wording of the busy guard's message for a handler that sends a new query applies here unchanged, since this layer introduces that message.

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


https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4


Generated by Claude Code

…inds runs

A calling handler of a warning raised by `dbFetch()` or `dbColumnInfo()`, or a busy handler called while a fetch or `dbBind()` steps, runs R code while the C++ code of the result is on the stack.
When such a handler called `dbClearResult()` on that result, the result and its statement were freed under that code, and R crashed when the fetch went on.
A result is now marked busy while one of its fetches or binds runs, or while `dbColumnInfo()` reads its first row.
`dbClearResult()` on a busy result fails and leaves the result as it is.
A fetch, a bind or `dbColumnInfo()` of a busy result fails too, and so does a new query on its connection, because it clears the open result first.
The mark is released on every exit path, also when an R error longjmps past the C++ frames, through `R_ExecWithCleanup()`.
The warning of a fetch from a statement that is no query is raised through cpp11, so that an error made of it unwinds the C++ frames.
The tests run the handlers in a callr session, because the unguarded code crashes R.
Ported from #830 (ae87b09, and the warning change of 1d0d70f).

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 00:02
`result_release()` refused a busy result with `cpp11::stop()`.
With cpp11 before 0.4.6, that error, raised from a handler during a fetch,
could longjmp past the C++ destructors of the fetch.
It now throws a `std::runtime_error`, as `run_guarded()` does,
which the cpp11 entry point turns into an R error.

Both refusal messages now name what they refuse, including a new query on the connection,
and both causes, a warning handler or a busy handler.

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