Skip to content

fix: A fetch stops cleanly when R code it calls closes its result or connection - #629

Merged
krlmlr merged 2 commits into
mainfrom
claude/busy-result-connection
Nov 29, 2026
Merged

krlmlr merged 2 commits into
mainfrom
claude/busy-result-connection

Conversation

@krlmlr

@krlmlr krlmlr commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

Closes #621. Stacked on #624.

Before this change, R code that ran during a fetch could run a query or call dbDisconnect() on the same connection, and R then crashed or read freed memory. The fetch loop checks for interrupts every 1024 rows, so an interrupt handler that resumes is one way in. Rebuilt as you suggested, with the same remedy as #615 and without #611's busy flag.

Change

  • A query or dbDisconnect() from such R code is no longer refused. It closes the result or the connection as usual, which sets the result's statement to NULL. The C++ result object stays alive, because R holds it.
  • A new MariaResultPrep::check_open() runs right after cpp11::check_user_interrupt() in the fetch loop. If the statement or the connection has been closed, it stops with "The result was closed while it was being fetched." before any closed state is used.
  • Warnings are raised after the C++ work in fix: Raise warnings only after the C++ code is done with the result #632, so they don't run R code during a fetch any more.

Not covered: dbClearResult() on the result being fetched, from an interrupt handler. That call deletes the C++ object itself, not just its statement; see the question below.

Tests

tests/testthat/test-dbDisconnect.R gains a test that sends an interrupt to its own process just before calling the C++ fetch on a 10,000-row cross join. The interrupt handler runs a query or dbDisconnect() and resumes. The test then expects the new error and an invalid result. The interrupt lands inside the fetch on the first attempt in 49 of 50 runs. The test retries up to 10 times, and skips with a clear message if every attempt misses. It is skipped on CRAN and Windows. Without the fix, it crashes R.

Under valgrind, the issue's interrupt reproducer ends in the new error with 0 errors, for MODE=send and MODE=disc, with both MariaDB Connector/C 3.3.17 and libmysqlclient 8.0.46. Local runs against MariaDB 10.11 with NOT_CRAN=true: the whole suite had 0 failures with both client libraries, apart from the test-timezone.R "US/Alaska" error, which comes from this container. MySQL servers were not tested: there is no server in this container, and CI's MySQL entries currently run MariaDB (#592).

Session: https://claude.ai/code/session_01Hr1rd5PUWgaCJzotWt9NhJ

https://claude.ai/code/session_01Hr1rd5PUWgaCJzotWt9NhJ


Generated by Claude Code

@krlmlr
krlmlr added this pull request to stack #625 November 29, 2026 12:34

krlmlr commented Nov 29, 2026 •

Copy link
Copy Markdown
Member Author

Superseded: Accepted limitations

The clean review found nothing above Low. The reviewer confirmed that dbBegin(), dbCommit(), dbRollback() and dbQuoteString() still work from a handler during a fetch, and that the busy flag is reset after the new error.

  • src/DbConnection.cpp: the destructor calls disconnect(), which can now throw. It can't while a fetch runs, because a busy result holds a reference to its connection, so the connection can't be destroyed then. A future change that breaks this would turn the error into a call to std::terminate(). Relevance: low. Reviewer confidence: high.
  • dbListTables(), dbListFields(), dbReadTable() and dbWriteTable() from R code running during a fetch now fail with the new error, because they run queries. dbExistsTable() swallows every error, so it returns FALSE in that situation even for an existing table. Before, all of these could crash R. Relevance: low. Reviewer confidence: high.
  • dbCommit() and dbRollback() during a streaming fetch ignore the client library's return value. This already happens on main, and the reviewer couldn't trigger an error.

Generated by Claude Code

@krlmlr
krlmlr marked this pull request as ready for review November 29, 2026 12:36
@krlmlr

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Same remedy as proposed for #615 possible?

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Yes, I think so. Instead of refusing a query or dbDisconnect() while a result is busy, let it close the result or the connection as it normally would. The fetch then checks, after every point where R code can run (the interrupt check in the fetch loop), that its result is still active and its connection still open. If not, it stops with a clear error and doesn't touch the closed state. This needs no busy flag, so it no longer depends on #611. I'm rebuilding this PR that way, on top of the rebuilt #624. Warnings are handled separately, by the alternative to #611 that raises them after the C++ work.


Generated by Claude Code

@krlmlr
krlmlr force-pushed the claude/busy-result-connection branch from 7dcc9cc to 50c2cb1 Compare November 29, 2026 14:07
@krlmlr
krlmlr removed this pull request from stack #625 November 29, 2026 14:08
@krlmlr
krlmlr added this pull request to stack #633 November 29, 2026 14:08
@krlmlr krlmlr changed the title fix: Refuse a query or dbDisconnect() while a result of the connection is being fetched fix: A fetch stops cleanly when R code it calls closes its result or connection Nov 29, 2026
@krlmlr
krlmlr marked this pull request as draft November 29, 2026 14:08

krlmlr commented Nov 29, 2026 •

Copy link
Copy Markdown
Member Author

Superseded by the question below: R code from an interrupt handler that uses the result being fetched

With #632, warnings no longer run R code during a fetch, but the interrupt check in the fetch loop still does. A query or dbDisconnect() from an interrupt handler is now safe (this PR). Two calls on the result being fetched are not:

  • dbClearResult(): result_release() deletes the C++ object itself while fetch() is still running on it.
  • dbBind(): it runs the statement again and changes the column types. The resumed fetch then fails with an R error ("SET_STRING_ELT() can only be applied to a 'character vector'") that skips C++ destructors, or, with the same types, joins rows from two executions. This already happens on main. Valgrind stays clean.

Both need a handler that resumes after an interrupt and uses that very result.

  • Add a "fetch in progress" flag to the result. dbBind() and a nested dbFetch() stop with an error while it is set. dbClearResult() only closes the result, and the C++ object is deleted once the fetch returns, which then stops with this PR's error (recommended)
  • Don't run R code during the fetch at all: check for a pending interrupt without running handlers, stop the fetch, and let R handle the interrupt after returning. invokeRestart("resume") can then no longer resume the fetch
  • Accept the gap, since it needs an unusual handler

If nobody decides, I'll implement the first option in a PR stacked on this one.


Generated by Claude Code

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Accepted limitations

This is for the version rebuilt without #611's busy flag. Its clean review found nothing above Low. Under valgrind, the interrupt reproducer ends in the new error with 0 errors, for a query and for dbDisconnect() in the handler, with both client libraries. Without the fix, the new test aborts R.

Two Lows are fixed in 126b5b3:


Generated by Claude Code

@krlmlr
krlmlr marked this pull request as ready for review November 29, 2026 14:17
@krlmlr

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

If released memory is the problem, can't we take ownership of all objects that we need to be alive and/or avoid reentrancy or destruction by setting a guard? The current solution looks like a lot of complexity for an edge case, also willing to go with declaring this limitation.

Resolve conflicts.

krlmlr commented Nov 29, 2026 •

Copy link
Copy Markdown
Member Author

The conflicts are resolved: #624 has the latest main, and this PR has #624 (6e17a99). Both are mergeable.

Resolved: Guard, or declare the limitation?

Resolved by the merge of this PR on 2026-11-29: the post-check stays as it is.

The memory at stake is the MYSQL_STMT of the result, freed when another query or dbClearResult() closes the result, and the MYSQL connection, freed by dbDisconnect(). Both can be freed from R code that runs during a fetch, which today means an interrupt handler that resumes. Taking ownership amounts to a guard: while a fetch runs, closing only marks the result or the connection as closed, and the fetch frees them when it returns. That covers dbClearResult(), dbBind(), another query and dbDisconnect() alike, and makes the post-check in this PR unnecessary. It touches close() and disconnect() in a few lines each, plus one flag. Most of this PR's size is the interrupt test, which any variant would need.

  • Declare the limitation: close this PR, keep fix: Results of a closed connection fail cleanly instead of crashing #624, and document in ?dbFetch that R code run from an interrupt handler during a fetch must not use the same connection (recommended; the case needs a handler that resumes after an interrupt)
  • Replace this PR's post-check with the guard that defers freeing until the fetch returns, covering all four calls
  • Keep this PR as it is

If nobody decides, I'll go with the first option.


Generated by Claude Code

Base automatically changed from claude/fetch-after-disconnect to main November 29, 2026 14:41
…connection

Closes #621.

An interrupt handler that resumes can run a query or `dbDisconnect()` during `dbFetch()`,
which closes the result being fetched as usual.
After each interrupt check, the fetch now checks that its statement is still open,
and fails with a clear error instead of reading the freed statement.
The same check follows the metadata update of a further parameter set,
which can warn.

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

Call result_fetch() directly after the signal so that R handles the
interrupt inside the fetch loop, and skip() if no attempt hits it.
Drop the check_open() after update_metadata() in step(): the only
callback there is the type warning.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd5PUWgaCJzotWt9NhJ
@krlmlr
krlmlr force-pushed the claude/busy-result-connection branch from 6e17a99 to ce0bbc5 Compare November 29, 2026 14:41
@krlmlr
krlmlr merged commit 1e0d0b1 into main Nov 29, 2026
9 checks passed
@krlmlr
krlmlr deleted the claude/busy-result-connection branch November 29, 2026 17:19
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.

A query or dbDisconnect() from R code running during dbFetch() frees the result being fetched

2 participants