Skip to content
Open
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
5 changes: 3 additions & 2 deletions src_cpp/include/node_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
using namespace lbug::main;
using namespace lbug::common;

class NodeConnection : public Napi::ObjectWrap<NodeConnection> {
class NodeConnection : public Napi::ObjectWrap<NodeConnection>, public DatabaseDependent {
friend class ConnectionInitAsyncWorker;
friend class ConnectionExecuteAsyncWorker;
friend class ConnectionQueryAsyncWorker;
Expand All @@ -22,7 +22,7 @@ class NodeConnection : public Napi::ObjectWrap<NodeConnection> {
public:
static Napi::Object Init(Napi::Env env, Napi::Object exports);
explicit NodeConnection(const Napi::CallbackInfo& info);
~NodeConnection() override = default;
~NodeConnection() override;

private:
Napi::Value InitAsync(const Napi::CallbackInfo& info);
Expand All @@ -40,6 +40,7 @@ class NodeConnection : public Napi::ObjectWrap<NodeConnection> {
Napi::Value CreateArrowRelTableSync(const Napi::CallbackInfo& info);
Napi::Value DropArrowTableSync(const Napi::CallbackInfo& info);
void Close(const Napi::CallbackInfo& info);
void ReleaseDatabase() override;

private:
std::shared_ptr<Database> database;
Expand Down
15 changes: 15 additions & 0 deletions src_cpp/include/node_database.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@

using namespace lbug::main;

// A native object that keeps a Database alive (a connection or a query result). The registry
// lets NodeDatabase::Close release the ones that are still around, including those whose JS
// wrapper was garbage-collected but not finalized yet, so that the database is destroyed on
// close() instead of whenever the last finalizer runs.
class DatabaseDependent {
public:
virtual ~DatabaseDependent() = default;
static void Register(Database* database, DatabaseDependent* dependent);
static void Unregister(Database* database, DatabaseDependent* dependent);
static void ReleaseAll(Database* database);

private:
virtual void ReleaseDatabase() = 0;
};

class NodeDatabase : public Napi::ObjectWrap<NodeDatabase> {
friend class NodeConnection;
friend class DatabaseInitAsyncWorker;
Expand Down
4 changes: 3 additions & 1 deletion src_cpp/include/node_query_result.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "binder/bound_statement_result.h"
#include "main/lbug.h"
#include "node_database.h"
#include "node_util.h"
#include "planner/operator/logical_plan.h"
#include "processor/result/factorized_table.h"
Expand All @@ -12,7 +13,7 @@
using namespace lbug::processor;
using namespace lbug::main;

class NodeQueryResult : public Napi::ObjectWrap<NodeQueryResult> {
class NodeQueryResult : public Napi::ObjectWrap<NodeQueryResult>, public DatabaseDependent {
friend class NodeQueryResultGetNextAsyncWorker;
friend class NodeQueryResultGetColumnMetadataAsyncWorker;
friend class NodeQueryResultGetNextQueryResultAsyncWorker;
Expand Down Expand Up @@ -47,6 +48,7 @@ class NodeQueryResult : public Napi::ObjectWrap<NodeQueryResult> {
void PopulateColumnNames();
void Close(const Napi::CallbackInfo& info);
void Close();
void ReleaseDatabase() override;
QueryResult& GetQueryResult() const;
void AcquireAsyncUse();
void ReleaseAsyncUse();
Expand Down
18 changes: 16 additions & 2 deletions src_cpp/node_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,21 @@ NodeConnection::NodeConnection(const Napi::CallbackInfo& info)
Napi::HandleScope scope(env);
NodeDatabase* nodeDatabase = Napi::ObjectWrap<NodeDatabase>::Unwrap(info[0].As<Napi::Object>());
database = nodeDatabase->database;
if (database) {
DatabaseDependent::Register(database.get(), this);
}
}

NodeConnection::~NodeConnection() {
ReleaseDatabase();
}

void NodeConnection::ReleaseDatabase() {
if (database) {
DatabaseDependent::Unregister(database.get(), this);
}
connection.reset();
database.reset();
}

Napi::Value NodeConnection::InitAsync(const Napi::CallbackInfo& info) {
Expand Down Expand Up @@ -176,8 +191,7 @@ void NodeConnection::SetQueryTimeout(const Napi::CallbackInfo& info) {
void NodeConnection::Close(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
this->connection.reset();
this->database.reset();
ReleaseDatabase();
}

Napi::Value NodeConnection::ExecuteAsync(const Napi::CallbackInfo& info) {
Expand Down
46 changes: 46 additions & 0 deletions src_cpp/node_database.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,48 @@
#include "include/node_database.h"

#include <mutex>
#include <unordered_map>
#include <unordered_set>

namespace {
// Query results register from the worker thread that produced them.
std::mutex dependentsMtx;
std::unordered_map<Database*, std::unordered_set<DatabaseDependent*>> dependents;
} // namespace

void DatabaseDependent::Register(Database* database, DatabaseDependent* dependent) {
std::lock_guard lck{dependentsMtx};
dependents[database].insert(dependent);
}

void DatabaseDependent::Unregister(Database* database, DatabaseDependent* dependent) {
std::lock_guard lck{dependentsMtx};
auto it = dependents.find(database);
if (it == dependents.end()) {
return;
}
it->second.erase(dependent);
if (it->second.empty()) {
dependents.erase(it);
}
}

void DatabaseDependent::ReleaseAll(Database* database) {
std::unordered_set<DatabaseDependent*> toRelease;
{
std::lock_guard lck{dependentsMtx};
auto it = dependents.find(database);
if (it == dependents.end()) {
return;
}
toRelease = std::move(it->second);
dependents.erase(it);
}
for (auto* dependent : toRelease) {
dependent->ReleaseDatabase();
}
}

Napi::Object NodeDatabase::Init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);

Expand Down Expand Up @@ -131,6 +174,9 @@ void NodeDatabase::InitCppDatabase() {
void NodeDatabase::Close(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
if (database) {
DatabaseDependent::ReleaseAll(database.get());
}
database.reset();
}

Expand Down
17 changes: 17 additions & 0 deletions src_cpp/node_query_result.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,16 @@ void NodeQueryResult::AdoptQueryResult(
std::unique_ptr<QueryResult> queryResult, std::shared_ptr<Connection> connection,
std::shared_ptr<Database> database) {
ThrowIfAsyncOperationInFlight("replace");
if (this->database) {
DatabaseDependent::Unregister(this->database.get(), this);
}
columnNames.reset();
ownedQueryResult = std::move(queryResult);
this->connection = std::move(connection);
this->database = std::move(database);
if (this->database) {
DatabaseDependent::Register(this->database.get(), this);
}
}

std::unique_ptr<QueryResult> NodeQueryResult::DetachNextQueryResult() {
Expand Down Expand Up @@ -336,7 +342,18 @@ void NodeQueryResult::Close(const Napi::CallbackInfo& info) {
}
}

void NodeQueryResult::ReleaseDatabase() {
// A result with an async operation in flight is being read by a worker thread: leave it.
// It still holds the database, which is then destroyed when the result is closed.
if (activeAsyncUses.load(std::memory_order_acquire) == 0) {
Close();
}
}

void NodeQueryResult::Close() {
if (database) {
DatabaseDependent::Unregister(database.get(), this);
}
columnNames.reset();
ownedQueryResult.reset();
connection.reset();
Expand Down
5 changes: 5 additions & 0 deletions src_js/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Connection {
this._isInitialized = false;
this._initPromise = null;
this._isClosed = false;
this._trackingRef = database._trackConnection(this);
numThreads = parseInt(numThreads);
if (numThreads && numThreads > 0) {
this._numThreads = numThreads;
Expand Down Expand Up @@ -611,13 +612,15 @@ class Connection {
this._isInitialized = true;
this._isClosed = true;
delete this._connection;
this._database._untrackConnection(this._trackingRef);
return;
}
}
// Connection is initialized, close it.
this._connection.close();
delete this._connection;
this._isClosed = true;
this._database._untrackConnection(this._trackingRef);
}

/**
Expand All @@ -635,11 +638,13 @@ class Connection {
this._isInitialized = true;
this._isClosed = true;
delete this._connection;
this._database._untrackConnection(this._trackingRef);
return;
}
this._connection.close();
delete this._connection;
this._isClosed = true;
this._database._untrackConnection(this._trackingRef);
}
}

Expand Down
40 changes: 38 additions & 2 deletions src_js/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,34 @@ class Database {
this._isInitialized = false;
this._initPromise = null;
this._isClosed = false;
// Connections opened on this database, so that close() can close them.
// They are held weakly: a connection that goes out of scope must still be
// garbage-collected.
this._openConnections = new Set();
this._connectionFinalizer = new FinalizationRegistry((ref) => {
this._openConnections.delete(ref);
});
}

/**
* Internal function to track an open connection.
* @param {lbug.Connection} connection the connection.
* @returns {WeakRef} the handle to pass to `_untrackConnection`.
*/
_trackConnection(connection) {
const ref = new WeakRef(connection);
this._openConnections.add(ref);
this._connectionFinalizer.register(connection, ref, ref);
return ref;
}

/**
* Internal function to stop tracking a closed connection.
* @param {WeakRef} ref the handle returned by `_trackConnection`.
*/
_untrackConnection(ref) {
this._openConnections.delete(ref);
this._connectionFinalizer.unregister(ref);
}

/**
Expand Down Expand Up @@ -176,7 +204,8 @@ class Database {
}

/**
* Close the database.
* Close the database. Connections and query results that are still open
* are closed with it, so the database is released when this returns.
*/
async close() {
if (this._isClosed) {
Expand All @@ -195,13 +224,17 @@ class Database {
}
}
// Database is initialized, close it.
for (const ref of [...this._openConnections]) {
await ref.deref()?.close();
}
this._database.close();
delete this._database;
this._isClosed = true;
}

/**
* Close the database synchronously.
* Close the database synchronously. Connections and query results that are
* still open are closed with it.
* @throws {Error} if there is an ongoing asynchronous initialization.
*/
closeSync() {
Expand All @@ -218,6 +251,9 @@ class Database {
return;
}
}
for (const ref of [...this._openConnections]) {
ref.deref()?.closeSync();
}
this._database.close();
delete this._database;
this._isClosed = true;
Expand Down
27 changes: 27 additions & 0 deletions test/test_database.js
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,33 @@ describe("Database close", function () {
assert.isTrue(testDb._isClosed);
});

it("should release the database on close while a connection and a query result are still open", async function () {
if (process.platform === "win32") {
this._runnable.title += " (skipped: not implemented on Windows)";
this.skip();
}
const tmpDbPath = await new Promise((resolve, reject) => {
tmp.dir({ unsafeCleanup: true }, (err, path, _) => {
if (err) {
return reject(err);
}
return resolve(path);
});
});
const dbPath = path.join(tmpDbPath, "db.lbdb");
const testDb = new lbug.Database(dbPath, 1 << 28 /* 256MB */);
const conn = new lbug.Connection(testDb);
const res = await conn.query("RETURN 1");
await testDb.close();
// The lock on the database file is only released when the native database is destroyed.
const subProcessResult = await openDatabaseOnSubprocess(dbPath);
assert.isEmpty(subProcessResult.stderr);
assert.equal(subProcessResult.code, 0);
assert.include(subProcessResult.stdout, "Database initialized.");
assert.isTrue(conn._isClosed);
assert.throws(() => res.getNumTuples(), "Query result is closed.");
});

it("should not crash when file-backed async query results are discarded before close", async function () {
const tmpDbPath = await new Promise((resolve, reject) => {
tmp.dir({ unsafeCleanup: true }, (err, path, _) => {
Expand Down
Loading