Skip to content

fix: Results of a closed connection fail cleanly instead of crashing - #624

Merged
krlmlr merged 3 commits into
mainfrom
claude/fetch-after-disconnect
Nov 29, 2026
Merged

krlmlr merged 3 commits into
mainfrom
claude/fetch-after-disconnect

Conversation

@krlmlr

@krlmlr krlmlr commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

Closes #615, closes #622. Based on main; it doesn't depend on #611. #629 is stacked on it.

Before this change, dbDisconnect() with a live result only warned and closed the connection. A later dbFetch() on that result crashed R with MariaDB Connector/C (#615). Likewise, dbBind() on a result that a newer query had cancelled crashed R with both client libraries (#622), although dbIsValid() already returned FALSE.

Change

Tests

tests/testthat/test-dbDisconnect.R (new) covers queries, statements, immediate = TRUE, a parameterized query, garbage collection, and #622. On MariaDB servers, it also checks that disconnecting after fetching one row of a 20-million-row result takes less than 0.5 s. That took about 1 s before the close order was fixed, and takes 0.001 s now. Under valgrind, these tests and test-dbClearResult.R show 0 errors with MariaDB Connector/C 3.3.17. With libmysqlclient 8.0.46, valgrind shows only 248 bytes "possibly lost" inside the library's own startup. Local runs against MariaDB 10.11 with NOT_CRAN=true, with both client libraries, after rebuilding this PR as a single commit on main: the whole suite had 0 failures and the one test-timezone.R error ("US/Alaska" unknown to CCTZ in this container), which also happens on main. 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


Generated by Claude Code

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Accepted limitations

Three review rounds:

  • The first found a Medium: closing the statement before the connection made dbDisconnect() read every remaining row of a half-fetched result. That is fixed in b52751f, which now closes the connection first and takes 0.001 s instead of about 1 s for 20 million rows.
  • The second found that the new timing test needs MariaDB's seq_1_to_N tables. It is skipped on other servers since 188c63f.
  • The third found that the test's immediate loop added nothing, because dbSendQuery() ignores immediate. It is back to a single run in 504b037.

Valgrind is clean with both client libraries.

  • src/DbConnection.cpp: disconnecting with a result open no longer commits through that result first, so a manual transaction still open at that point is rolled back. Relevance: low, arguably safer. Reviewer confidence: high.
  • After dbDisconnect(), dbGetRowCount(), dbGetRowsAffected(), dbHasCompleted() and dbColumnInfo() now error instead of returning cached values, consistent with dbIsValid() returning FALSE. A result cancelled by a newer query still returns cached values for these. Relevance: low. Reviewer confidence: high.
  • tests/testthat/test-dbDisconnect.R: the timing test runs on MariaDB only, detected from VERSION(), and covers prepared statements only. Relevance: low. Reviewer confidence: high.
  • dbSendQuery() ignores immediate = TRUE. This is intentional on main ("immediate = TRUE not supported"), and not changed here.

Generated by Claude Code

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

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Does this need #611, especially given that it's revisited? Unstack and implement against main.

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

No, it doesn't: the diff against #611 mentions the busy guard only in context lines. I'm rebuilding this PR as a single commit on main without #611, then retargeting it to main and taking it out of the stack. I'll post here when it's pushed and checked.


Generated by Claude Code

Closes #615, closes #622.

`dbDisconnect()` closes the connection first and frees its result afterward,
so that closing the statement no longer reads the remaining rows of an unbuffered result.
The methods of a result check that its connection is still open
and fail with a clear error otherwise.
`dbBind()` on an inactive result, such as one cancelled by a newer query, errors like `dbFetch()` does.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd5PUWgaCJzotWt9NhJ
@krlmlr
krlmlr force-pushed the claude/fetch-after-disconnect branch from 504b037 to 5d4a99b Compare November 29, 2026 14:07
@krlmlr
krlmlr removed this pull request from stack #625 November 29, 2026 14:08
@krlmlr
krlmlr changed the base branch from claude/fetch-clear-uaf to main November 29, 2026 14:08
@krlmlr
krlmlr added this pull request to stack #633 November 29, 2026 14:08

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Done: this PR is now a single commit on main (5d4a99b), targets main, and contains nothing from #611. It is no longer in the old stack; #629 is stacked on it instead. test-dbDisconnect.R passes with both client libraries, valgrind is clean, and the whole suite has 0 failures.


Generated by Claude Code

@krlmlr
krlmlr enabled stacked auto-merge (squash) November 29, 2026 14:26
@krlmlr

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Resolve conflicts for the stack.

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Done. The latest main is merged into this PR (f61a9fe), and this PR into #629 (6e17a99). Both are mergeable again. A merge of main into #629 that someone pushed earlier had brought along CI's formatting of a line introduced here, so this PR now has that formatting too, and #629's diff shows only its own three files. The test-dbDisconnect.R tests and the files touched by the newly merged PRs pass locally.


Generated by Claude Code

@krlmlr
krlmlr merged commit aa0f4bc into main Nov 29, 2026
9 checks passed
@krlmlr
krlmlr deleted the claude/fetch-after-disconnect branch November 29, 2026 14:41
krlmlr pushed a commit that referenced this pull request Nov 29, 2026
krlmlr pushed a commit that referenced this pull request Nov 29, 2026
Resolve the conflict in src/DbResult.cpp by keeping both sides:
fetch() and get_column_info() take layer 2's `bigint` argument and
start with the check_connection() call from #624, as on main.
cpp11 registrations are unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd7atAZVq1J42WzXJpS
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.

dbBind() on a result cancelled by a newer query crashes R dbFetch() after dbDisconnect() crashes R with MariaDB Connector/C

2 participants