Skip to content

fix: dbFetch() never restarts a statement after an error - #935

Draft
krlmlr wants to merge 5 commits into
claude/rb-busy-guardfrom
claude/rb-error-ends-fetch
Draft

krlmlr wants to merge 5 commits into
claude/rb-busy-guardfrom
claude/rb-error-ends-fetch

Conversation

@krlmlr

@krlmlr krlmlr commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

What changes for users: dbFetch() never restarts a statement after an error. Any error that leaves a fetch now completes the result and resets its statement, so it releases its read lock. That covers an SQLite error from a step, the error that #933 keeps from the step past a fetch's rows, a parameter set that fails to bind, an error while converting values (also an R error that R's API raises, such as a failed allocation), a warning turned into an error (or left by a handler, such as tryCatch(warning = ), which drops the remaining rows without an error), and an interrupt. After that, dbHasCompleted() returns TRUE and further fetches return zero rows with the result's columns until the next dbBind(). dbGetRowCount() counts a fetch's rows only once the fetch returns them, in 64 bits. Before this change, the fetch after the error returned a row of NA and then ran the statement again from its first row, with the row count jumping. A later parameter set that had failed to bind ("Can only bind lists of raw vectors") ran again with the value bound as NULL. A write that failed in a fetch, or in the first step of dbBind(), ran once more on the next fetch (which can succeed and insert after a transient "database is locked"). A conversion error either repeated on every fetch, so a while (!dbHasCompleted(res)) loop that caught it never ended, or skipped that fetch's rows while the result went on. dbFetch(n = 0) and dbColumnInfo() read no row, so an error while converting the row they look at leaves the result as it is.

Why: SQLite answers sqlite3_step() on a failed statement by resetting it and running it again instead of returning an error, and RSQLite stepped such a statement again on the next fetch. The catch in fetch_rows() relies on the busy guard of #934, which keeps the statement in place while the fetch unwinds. An R error from R's API (for example Rf_allocVector() for a large blob) longjmps instead of unwinding, so the fetch loop runs in cpp11::unwind_protect(), which turns the longjmp into a cpp11::unwind_exception that the catch sees and rethrows, and that cpp11's entry point then resumes as the original R error. C++ exceptions inside the protected code are kept and rethrown after it, so that none leaves through the C frames of R_UnwindProtect(); the catch calls no R API. A failed step() also resets the statement, so that a failed first step in dbBind() ends it like a failed step in a fetch. A failed step() also completes the result for every caller, which matters for dbBind(): until layer B5 gives a failed dbBind() one state, a bind whose first step fails now leaves a completed result whose fetch returns zero rows instead of running the statement again.

kept_error_completes_ (the question at #830 (comment)): it is dropped, as recommended. Once the catch completes the result on every exception, the flag decided nothing, and the catch now clears the kept error with the rest of that state. The question on keeping the rows of a fetch that a conversion error or an interrupt ends (#830 (comment)) still applies here: those rows are dropped with the error, as the code comment says. #872 decided that a later layer (B2) attaches the rows read before an error to the condition, so this pull request does not keep them. The question on a warning handler clearing the result (#830 (comment)) belongs to the busy guard in #934 and is not touched here.

Dependencies: LinkingTo now requires cpp11 (>= 0.5.0). With cpp11 0.4.x, cpp11::unwind_protect() does nothing while the cpp11_should_unwind_protect option, shared by all packages built with cpp11 0.4.x, is FALSE, which happens while another such package's function calls back into R; the protection of the fetch would then be skipped.

How it's tested: the new tests/testthat/test-fetch-error.R covers the NA row and restart after a step error, a later parameter set that fails in the statement, a later parameter set that fails to bind (no re-run with NULL), a write that fails in a fetch (both while stepping and in the read-ahead step) and is not run again, a fetch after a dbBind() whose first step failed, the read lock released after a step error, after a raised kept error and after a conversion error (a second connection can write), conversion errors under options(warn = 2) and from a handler that leaves the fetch, a conversion error that drops a kept error, and an interrupt in a callr session. All 13 tests fail on the base branch and pass here. test-read-ahead.R gains expectations that a raised kept error completes the result, and test-out-of-memory.R gets #830's new test that a fetch that runs out of memory completes the result; its test that reads a row again after out of memory now runs out of memory in a dbFetch(n = 0). A further test in test-out-of-memory.R lowers mem.maxVSize() so that R fails to allocate a large blob in a fetch: it fails without the unwind_protect() (the result stays incomplete and the next fetch returns the row after the lost one) and passes with it; it is skipped on CRAN, like the other out-of-memory tests. os() moves from the top level of test-dbConnect.R to helper-os.R, so that its test runs on its own. Unlike #830, the new tests reuse B1's local_json_error_at_5() instead of adding helper-json-error.R, and live in their own file rather than in test-dbSendQuery.R. The full suite with NOT_CRAN=true passes.

Layer B4 of the rebuilt chain, planned in #880; based on #934. Ported from #830 (now a draft): commits 55c06bb, 6436e9a, 1b77244, 5ed2a6f, 1d0d70f, 9356df9, dd9cb33 and dc3ac22, the os() move and the out-of-memory tests, without the busy guard (already in #934) and without the state after a failed dbBind() (bind_started_, layer B5).

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


Generated by Claude Code

The test of VFS values called `os()`, which the file defined outside of any test,
so that the test failed when run on its own.
The function now lives in `helper-os.R`, which testthat and `pkgload::load_all()` source before the tests.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
When a fetch failed, the result went on.
SQLite answers a step of a failed statement by resetting it and running it again,
so the next `dbFetch()` returned a row of `NA` and restarted the query from its first row.
The same happened after the error kept from the step past a fetch's rows was raised.
A later parameter set that had failed to bind ran again with `NULL`,
and a write that had failed in a fetch or in `dbBind()` ran once more.
An error while converting values, a warning made an error, or an interrupt
either repeated on every further fetch or skipped the rows of that fetch.

Now any error that unwinds through a fetch completes the result
and resets its statement, so that it releases its read lock.
A failed step completes the result for every caller of `step()`.
The busy guard of #934 keeps the statement in place for the catch.
Rows count as fetched only once the fetch returns them, in 64 bits.
`kept_error_completes_` decided nothing once the catch completes the result, and is dropped.
`dbFetch(n = 0)` and `dbColumnInfo()` read no row,
so an error while converting the row they look at leaves the result as it is;
the out-of-memory test that reads a row again now runs out of memory there.

Ported from #830.

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

An R error from R's API while values are read, such as a failed allocation,
longjmped past the catch that completes the result,
so the next fetch went on after the rows that were lost.
The fetch loop now runs in `cpp11::unwind_protect()`,
which turns the longjmp into an exception that the catch sees.
A failed first step in `dbBind()` also resets the statement now,
like a failed step in a fetch.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd7QnTD4acgzTUihnF4
With cpp11 0.4.x, `cpp11::unwind_protect()` does nothing
while another package's cpp11 code calls back into R,
so a fetch could still skip its cleanup on an R error.
The out-of-memory test matches the wording of R before 4.4.0 too,
and skips before R 3.6.0, which lacks `mem.maxVSize()`.
The comment on `GuardedCall` no longer claims the longjmp
leaves a fetch in the middle of a step.

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