Repository navigation
fix: Raise warnings only after the C++ code is done with the result - #632
Conversation
Closes #601. 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 before the new result is created, and destructors only queue. This is an alternative to #611, which refuses dbClearResult() from such a handler. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd5PUWgaCJzotWt9NhJ
|
Status: the clean review found one Medium, now being fixed. The destructor's "call dbDisconnect()" warning from a connection collected by Generated by Claude Code |
…d call The warning for a connection collected by gc() went into the same queue as the warnings of the current call, and surfaced in the next call on any connection. With options(warn = 2), it turned a healthy call into an error and lost its result. Destructors now queue into a separate queue. A task callback registered in .onLoad() drains it after each top-level call, and R prints its warnings after the output of that call. An error from the callback, e.g. with options(warn = 2), is printed and keeps the callback registered. The tests ignore unrelated warnings. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd5PUWgaCJzotWt9NhJ
A call that queued a warning and then failed never signaled it, so the warning surfaced in a later, unrelated call. Each entry point that signals queued warnings now clears the queue first. A warning that preceded an error is dropped, the error is what matters. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd5PUWgaCJzotWt9NhJ
With options(warn = 2) or an exiting warning handler, dbSendQuery() never returned the new result, which stayed the connection's current result until garbage collection. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd5PUWgaCJzotWt9NhJ
…n `dbConnect()` too Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd5PUWgaCJzotWt9NhJ
Superseded: Accepted limitationsThree review rounds. The first found a Medium: the destructor's leak warning surfaced in unrelated calls. It now has its own queue, raised at top level by a task callback (1044b9f), together with three Lows (4a55a0c, 7c752d3). The second found two Mediums: the callback was removed by position, and leak warnings were lost when R never returns to top level. Both are fixed in 94c197a: the callback is registered by name, and
Generated by Claude Code |
|
Drop the top-level callback, this is overkill. Instead, raise with the next call into the library, and amend the warning text to indicate that the origin may be from previous operations. |
…f from a task callback Drop the top-level task callback. Warnings queued by destructors during garbage collection are now raised at the start of the next call into the library (result_create(), result_fetch(), result_bind(), connection_create()), before any C++ work, so that an error under options(warn = 2) leaves no state changed and no result lost. The leak warning text says that the connection may stem from an earlier operation. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd5PUWgaCJzotWt9NhJ
…matting Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd5PUWgaCJzotWt9NhJ
A clean review of the change found nothing above Low. The local test server is down, so this version was only built and reviewed here; CI runs the tests. Accepted limitations
Generated by Claude Code |
Keep the deferred warning from this branch with the new text from #624. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd5PUWgaCJzotWt9NhJ
|
Merged the latest 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
…669) * fix: Raise warnings only after the C++ code is done with the result 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 * fix: Drop warnings from result destructors, and test the handlers in `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 --------- Co-authored-by: Claude <[email protected]>
Closes #601. An alternative to #611, as asked there: instead of refusing
dbClearResult()while a fetch runs, warnings are queued and raised just before returning to R.Before this change,
cpp11::warning()insideMariaResultPrep::fetch()ran R's calling handlers while the C++ code still held the result. A handler that calleddbClearResult()freed it, and the fetch then read freed memory.Change
src/DeferredWarnings.{h,cpp}collects warnings during C++ work. The text and number of warnings are unchanged.fetch(), the warning for an unrecognized field type (also reached fromupdate_metadata()since fix: Read result types afterdbBind(), so that placeholder columns keep their values #618), and "result object still in use" indisconnect().src/result.cppandsrc/connection.cppraise the queue after the C++ work is done:result_fetch()andresult_bind()raise it after the work.result_create()raises it at the end, once R owns the new result, so that nothing leaks underoptions(warn = 2).result_release()andconnection_release()raise it after resetting their pointers.options(warn = 2)the first warning becomes an error, the rest are dropped, and nothing is left behind.result_create()before any C++ work, and the connection is checked again afterwards. A handler that clears the old result works, and one that disconnects gets a clean "Invalid connection" error.dbConnect(), before that call does any work, so that underoptions(warn = 2)the error leaves nothing half-done. Onmain, a leaked connection collected bygc()underoptions(warn = 2)aborts R, becausestd::terminate()is called on cpp11's unwind exception.The interrupt check in the fetch loop (#621) is handled separately, in #629.
Tests
tests/testthat/test-deferred-warnings.R(new) covers:dbFetch()when a warning handler clears the result #601 handler for prepared andimmediate = TRUEresults, which now gets the empty data frame back, with the result cleared afterwards;options(warn = 2)leaves the result valid and clearable;gc()case, with the warning raised by the next query and bydbConnect(), and underoptions(warn = 2)before the next query starts.Under valgrind, the #601 script shows 5 invalid reads on
mainand 0 errors with this change. The new tests andtest-queries.Rshow 0 memory errors with MariaDB Connector/C 3.3.17 and libmysqlclient 8.0.46.Local runs against MariaDB 10.11 with
NOT_CRAN=true, with each client library and with-D_GLIBCXX_ASSERTIONS, before the merge with the latestmain: the whole suite had 0 failures. Twotest-timezone.Rerrors came from this container: "US/Alaska" unknown to CCTZ, and "SYSTEM" as the server's time zone, which had been reset and is fixed again. After the merge, the targeted test files pass. 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