Repository navigation
fix: Raise warnings only after the C++ code is done with the result - #669
Conversation
Part of #668. Warnings raised while C++ code holds a result or a connection are queued and signaled by the entry points just before returning to R, so that a calling handler can no longer free the result or close the connection under our feet. The warning about cancelling the previous query is raised in `result_create()` before the new result is created, and the connection is checked again after it. A warning left by a failed call is dropped at the start of the next one. Modeled after r-dbi/RMariaDB#632. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd6e5NcxC9GSrNJDokc
Question: Server noticesServer notices (
Generated by Claude Code |
Question: A result opened from the handler for "Closing open result set"The warning now comes at the start of
Generated by Claude Code |
Question: Queued warnings that no entry point signalsOnly
A result collected by garbage collection now drops its
Generated by Claude Code |
Superseded: Accepted limitationsSee the current list. From the author's own review; the clean review by a fresh agent is still to come, and this list will be updated after it.
Generated by Claude Code |
…`dbDisconnect()` A result collected by gc() inside another call queued its `PQcancel()` failure warning, which then surfaced in that unrelated call. The destructor now drops warnings, and `result_release()` deactivates the result before releasing it to keep them for `dbClearResult()`. New tests cover handlers that clear the result or disconnect again while `dbDisconnect()` warns about a result still in use. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd6e5NcxC9GSrNJDokc
Accepted limitationsAfter the clean review (round 1, nothing above Low).
Generated by Claude Code |
No conflicts with the deferred warnings of #669. An RPostgres_error leaves any warnings queued by the failing call in the queue; the next entry point that raises deferred warnings clears it first, so they never surface in a later call. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd6e5NcxC9GSrNJDokc
Resolves the conflict in `PqResultImpl::fetch_rows()`: `main` now raises the "Don't need to call dbFetch() for statements" warning through `defer_warning()` (#669) and dropped nothing else the row sink needs. The warning moves into `fetch_into()` with `defer_warning()`, and the unused row-count argument of `fetch_rows()` from `main` is not carried over. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd5LPjEzyaTRpXAuS6g
Part of #668: fixes the warning sites. Server notices, the remaining item there, are an open question below; this PR closes #668 only if they go in here.
Warnings that RPostgres signals while its C++ code holds a result or a connection used to run R calling handlers right away. A handler that called
dbClearResult()ordbDisconnect()freed the object under the C++ code's feet:dbDisconnect()from a handler for "Closing open result set" crashed R, anddbClearResult()from a handler for "Don't need to call dbFetch()" made the fetch read freed memory. This ports the fix from r-dbi/RMariaDB#632 (for r-dbi/RMariaDB#601).Current state
src/DeferredWarnings.{h,cpp}holds a queue:defer_warning(),clear_deferred_warnings(),raise_deferred_warnings().DbConnection::cancel_query(),PqResultImpl::fetch_rows(),DbColumn::warn_type_conflicts()and the "result object still in use" warning inconnection_release()are queued.result_create(),result_release(),result_fetch(),result_bind()andconnection_release()clear the queue on entry, so that a warning left by a failed call never surfaces in a later one, and signal it just before returning.DbConnection::set_current_result()to the start ofresult_create(), before any C++ work, and the connection is checked again afterwards: a handler that disconnects now gets "Invalid connection" instead of a crash.result_create()(options(warn = 2), an exiting handler), the new result is released at once, so that it does not stay the connection's current result.~DbResult()can reach thePQcancel()failure warning and may run during garbage collection inside another call, so it drops queued warnings with aDeferredWarningsSuppressorguard;result_release()deactivates the result before releasing it, sodbClearResult()keeps them.tests/testthat/test-deferred-warnings.Rports the RMariaDB tests and adds handlers that clear the result or disconnect again from the warning indbDisconnect(). Against CRAN RPostgres 1.4.10 the disconnect test crashes R. Theoptions(warn = 2)test fordbSendQuery()passes on the old code too; it guards the order of checks, not a regression.Review
A clean review (round 1) found nothing above Low; the Lows are fixed or listed in the Accepted limitations. Locally, the full suite passes with
NOT_CRAN=trueagainst PostgreSQL 16, and valgrind reports no errors for the reprexes, which read freed memory before.Overlap
#638 (row sink) and #666 (SQLSTATE errors) also touch
src/PqResultImpl.cpp, and #666 touchessrc/DbConnection.cpp. The change here is a one-call swap ofcpp11::warning()fordefer_warning()infetch_rows()andcancel_query(), plus the removal of the warning fromset_current_result(), so a conflict is a trivial one.https://claude.ai/code/session_01Hr1rd6e5NcxC9GSrNJDokc
Generated by Claude Code