Skip to content

fix: A query on a disconnected connection fails with a clear error - #640

Merged
krlmlr merged 3 commits into
mainfrom
claude/closed-connection-error
Nov 29, 2026
Merged

krlmlr merged 3 commits into
mainfrom
claude/closed-connection-error

Conversation

@krlmlr

@krlmlr krlmlr commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

Fixes #637.

After dbDisconnect(), the connection's external pointer is cleared. result_create(), connection_begin_transaction(), connection_commit() and connection_rollback() dereferenced it with cpp11's operator*, which throws std::bad_weak_ptr. So dbSendQuery(), dbSendStatement(), dbGetQuery(), dbExecute(), dbBegin(), dbCommit() and dbRollback() failed with "bad_weak_ptr". The functions that take a DbConnection* (dbGetInfo(), dbQuoteString(), dbWriteTable()) already failed with "Invalid connection" through as_cpp(). Nothing crashed.

  • One inline helper, connection_ptr() in src/RMariaDB_types.h, stops with "Invalid or closed connection", the wording DbConnection::check_connection() already uses. as_cpp() and the four external-pointer entry points use it.
  • dbExistsTable(), which checks validity in R, uses the same message.
  • Valid connections, dbIsValid() and the "Already disconnected" warning are unchanged.
  • tests/testthat/test-dbDisconnect.R gets one self-contained test that calls eleven methods on a disconnected connection, plus a second dbDisconnect().

Tests: built locally, and each entry point was checked against a connection with a cleared pointer. The local database server is down, so the test suite runs in CI only: a dispatched R-CMD-check smoke test against a live server passed on the first commit (FAIL 0, PASS 9092) in run 46275762621. The full matrix did not run.

Accepted limitations

🤖 Generated with Claude Code

https://claude.ai/code/session_01Hr1rd5PUWgaCJzotWt9NhJ

)

dbSendQuery(), dbExecute(), dbBegin(), dbCommit() and dbRollback()
failed with "bad_weak_ptr" after dbDisconnect(),
and dbQuoteString() and dbGetInfo() with "Invalid connection".
All now fail with "Invalid or closed connection".

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

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Accepted limitations

The clean review found nothing above Low. It confirmed that a cleared pointer can't be freed twice, that valid connections pay only one null check outside the fetch path, and that every R caller of conn@ptr now goes through the new check. One Low is fixed in 7e32846: dbExistsTable() on a closed connection said "Invalid connection" while every other method says "Invalid or closed connection". The test now covers it.

  • src/RMariaDB_types.h: dbGetInfo(), dbQuoteString() and dbWriteTable() on a closed connection now say "Invalid or closed connection" instead of "Invalid connection". This only matters to code that matches the old text; no test or DBItest spec does. Low relevance, high confidence.

Generated by Claude Code

@krlmlr
krlmlr marked this pull request as ready for review November 29, 2026 15:09

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

The red "MySQL server" job (job 141391540221) is not this PR's. Its 23 failures are exactly those of #638, which main has as well: test-dbBind-types.R, test-dbBind-completed.R, test-dbBind.R:9 and test-fetch-error.R:11. None of them is in test-dbDisconnect.R, and the new test passes against MySQL 8.4. The fix for #638 is in progress in a separate PR from main.


Generated by Claude Code

result_create() keeps the deferred warnings and the re-check after the
cancel warning from #632, and reads the connection through
connection_ptr(), so that a connection closed by a handler fails with
"Invalid or closed connection".

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd5PUWgaCJzotWt9NhJ
@krlmlr
krlmlr merged commit b59d080 into main Nov 29, 2026
34 of 35 checks passed
@krlmlr
krlmlr deleted the claude/closed-connection-error branch November 29, 2026 16:52
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 on a disconnected connection fails with the cryptic error "bad_weak_ptr"

2 participants