Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ add_library(RPostgres
DbResult.h
DbResultImpl.h
DbResultImplDecl.h
DeferredWarnings.cpp
DeferredWarnings.h
PqColumnDataSource.cpp
PqColumnDataSource.h
PqColumnDataSourceFactory.cpp
Expand Down
3 changes: 2 additions & 1 deletion src/DbColumn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "DbColumn.h"
#include "DbColumnDataSource.h"
#include "DbColumnStorage.h"
#include "DeferredWarnings.h"

DbColumn::DbColumn(
DATA_TYPE dt,
Expand Down Expand Up @@ -76,7 +77,7 @@ void DbColumn::warn_type_conflicts(const cpp11::r_string& name) const {
ss << format_data_type(*it);
}

cpp11::warning("%s", ss.str().c_str());
defer_warning(ss.str());
}

DbColumn::operator SEXP() const {
Expand Down
11 changes: 4 additions & 7 deletions src/DbConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "DbConnection.h"
#include "encode.h"
#include "DbResult.h"
#include "DeferredWarnings.h"

#ifdef _WIN32
#include <winsock2.h>
Expand Down Expand Up @@ -64,13 +65,9 @@ void DbConnection::set_current_result(const DbResult* pResult) {

// try to clean up remnants of any previous queries.
// (even if (the new) pResult is NULL, we should try to reset the back-end.)
// result_create() warns about cancelling the previous query
// before any C++ work starts.
if (pCurrentResult_ != NULL) {
if (pResult != NULL) {
cpp11::warning(
std::string("Closing open result set, cancelling previous query")
);
}

cleanup_query();
}

Expand Down Expand Up @@ -106,7 +103,7 @@ void DbConnection::cancel_query() {
// PQcancel() actually issues the cancel command to the backend.
char errbuf[256];
if (!PQcancel(cancel, errbuf, sizeof(errbuf))) {
cpp11::warning("%s", errbuf);
defer_warning(errbuf);
}

// free up the data structure allocated by PQgetCancel().
Expand Down
19 changes: 14 additions & 5 deletions src/DbResult.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "DbResult.h"
#include "DbConnection.h"
#include "DbResultImpl.h"
#include "DeferredWarnings.h"

// Construction ////////////////////////////////////////////////////////////////

Expand All @@ -14,11 +15,11 @@ DbResult::DbResult(const DbConnectionPtr& pConn) : pConn_(pConn) {
}

DbResult::~DbResult() {
try {
if (is_active()) {
pConn_->reset_current_result(this);
}
} catch (...) {}
// Also runs during garbage collection, inside an unrelated call:
// warnings from here are dropped.
// result_release() calls deactivate() first to keep them.
DeferredWarningsSuppressor suppress;
deactivate();
}

// Publics /////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -61,6 +62,14 @@ cpp11::list DbResult::get_column_info() {
return out;
}

void DbResult::deactivate() {
try {
if (is_active()) {
pConn_->reset_current_result(this);
}
} catch (...) {}
}

void DbResult::close() {
// Called from destructor
if (impl) {
Expand Down
1 change: 1 addition & 0 deletions src/DbResult.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class DbResult : boost::noncopyable {

public:
void close();
void deactivate();

bool complete() const;
bool is_active() const;
Expand Down
36 changes: 36 additions & 0 deletions src/DeferredWarnings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "pch.h"
#include "DeferredWarnings.h"

// R calls into the library from its main thread only.
static std::vector<std::string> deferred_warnings;
static int suppress_depth = 0;

void defer_warning(const std::string& msg) {
if (suppress_depth > 0) {
return;
}
deferred_warnings.push_back(msg);
}

void clear_deferred_warnings() {
deferred_warnings.clear();
}

void raise_deferred_warnings() {
// Take ownership first, so that the queue is empty
// even if a warning is turned into an error.
std::vector<std::string> warnings;
warnings.swap(deferred_warnings);

for (const auto& msg : warnings) {
cpp11::warning("%s", msg.c_str());
}
}

DeferredWarningsSuppressor::DeferredWarningsSuppressor() {
++suppress_depth;
}

DeferredWarningsSuppressor::~DeferredWarningsSuppressor() {
--suppress_depth;
}
36 changes: 36 additions & 0 deletions src/DeferredWarnings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef __RPOSTGRES_DEFERRED_WARNINGS__
#define __RPOSTGRES_DEFERRED_WARNINGS__

#include <string>

// Warnings raised while C++ code holds a result or a connection
// are queued here instead of being signaled right away.
// Signaling a warning runs R calling handlers,
// which may clear the result or close the connection under our feet.
// The entry points in result.cpp and connection.cpp signal the queued warnings
// after the C++ work is done, just before returning to R.
// Modeled after r-dbi/RMariaDB#632.
void defer_warning(const std::string& msg);

// Drops the warnings left by an earlier call that failed before signaling.
// Call at the start of each entry point that signals deferred warnings,
// so that they never surface in an unrelated later call.
void clear_deferred_warnings();

// Signals and clears all warnings queued by the current call.
// Call only when no C++ object state is touched afterwards:
// a calling handler may free it, and with options(warn = 2)
// the first warning becomes an error that unwinds the caller.
void raise_deferred_warnings();

// While an instance lives, defer_warning() drops its warning.
// Destructors that may run during garbage collection create one,
// so that a collection inside another call does not queue a warning
// that would then surface in that unrelated call.
class DeferredWarningsSuppressor {
public:
DeferredWarningsSuppressor();
~DeferredWarningsSuppressor();
};

#endif
7 changes: 3 additions & 4 deletions src/PqResultImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "DbResult.h"
#include "DbColumnStorage.h"
#include "PqDataFrame.h"
#include "DeferredWarnings.h"

#ifdef _WIN32
#include <winsock2.h>
Expand Down Expand Up @@ -425,10 +426,8 @@ cpp11::list PqResultImpl::fetch_rows(const int n_max, int& n) {
PqDataFrame data(this, cache.names_, n_max, cache.types_);

if (complete_ && data.get_ncols() == 0) {
cpp11::warning(
std::string(
"Don't need to call dbFetch() for statements, only for queries"
)
defer_warning(
"Don't need to call dbFetch() for statements, only for queries"
);
}

Expand Down
11 changes: 6 additions & 5 deletions src/connection.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "pch.h"
#include "RPostgres_types.h"
#include "DeferredWarnings.h"

[[cpp11::register]]
int client_version() {
Expand All @@ -26,23 +27,23 @@ bool connection_valid(cpp11::external_pointer<DbConnectionPtr> con_) {

[[cpp11::register]]
void connection_release(cpp11::external_pointer<DbConnectionPtr> con_) {
clear_deferred_warnings();
if (!connection_valid(con_)) {
cpp11::warning(std::string("Already disconnected"));
return;
}

DbConnectionPtr* con = con_.get();
if (con->get()->has_query()) {
cpp11::warning(
std::string(
"There is a result object still in use.\n"
"The connection will be automatically released when it is closed"
)
defer_warning(
"There is a result object still in use.\n"
"The connection will be automatically released when it is closed"
);
}

con->get()->disconnect();
con_.reset();
raise_deferred_warnings();
}

[[cpp11::register]]
Expand Down
41 changes: 39 additions & 2 deletions src/result.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,53 @@
#include "pch.h"
#include "RPostgres_types.h"
#include "PqResult.h"
#include "DeferredWarnings.h"

[[cpp11::register]]
cpp11::external_pointer<DbResult> result_create(
cpp11::external_pointer<DbConnectionPtr> con,
std::string sql,
bool immediate
) {
clear_deferred_warnings();

(*con)->check_connection();
if ((*con)->has_query()) {
// Before any C++ work: a calling handler may clear the previous result
// or close the connection, both are checked again below.
cpp11::warning(
std::string("Closing open result set, cancelling previous query")
);
if (con.get() == NULL) {
cpp11::stop("Invalid connection");
}
(*con)->check_connection();
}

DbResult* res = PqResult::create_and_send_query(*con, sql, immediate);
return cpp11::external_pointer<DbResult>(res, true);
cpp11::external_pointer<DbResult> out(res, true);
try {
raise_deferred_warnings();
} catch (...) {
// A warning turned into an error (warn = 2, an exiting handler):
// the caller never sees the result, release it now
// so that it does not stay the connection's current result.
out.reset();
throw;
}
return out;
}

[[cpp11::register]]
void result_release(cpp11::external_pointer<DbResult> res) {
clear_deferred_warnings();
DbResult* pRes = res.get();
if (pRes != NULL) {
// Outside the destructor, which drops warnings
pRes->deactivate();
}
res.reset();
raise_deferred_warnings();
}

[[cpp11::register]]
Expand All @@ -26,12 +58,17 @@ bool result_valid(cpp11::external_pointer<DbResult> res_) {

[[cpp11::register]]
cpp11::list result_fetch(DbResult* res, const int n) {
return res->fetch(n);
clear_deferred_warnings();
cpp11::list out = res->fetch(n);
raise_deferred_warnings();
return out;
}

[[cpp11::register]]
void result_bind(DbResult* res, cpp11::list params) {
clear_deferred_warnings();
res->bind(params);
raise_deferred_warnings();
}

[[cpp11::register]]
Expand Down
Loading