diff --git a/src_cpp/include/node_connection.h b/src_cpp/include/node_connection.h index dbddf17..a6c47c7 100644 --- a/src_cpp/include/node_connection.h +++ b/src_cpp/include/node_connection.h @@ -13,7 +13,7 @@ using namespace lbug::main; using namespace lbug::common; -class NodeConnection : public Napi::ObjectWrap { +class NodeConnection : public Napi::ObjectWrap, public DatabaseDependent { friend class ConnectionInitAsyncWorker; friend class ConnectionExecuteAsyncWorker; friend class ConnectionQueryAsyncWorker; @@ -22,7 +22,7 @@ class NodeConnection : public Napi::ObjectWrap { 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); @@ -40,6 +40,7 @@ class NodeConnection : public Napi::ObjectWrap { 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; diff --git a/src_cpp/include/node_database.h b/src_cpp/include/node_database.h index 85b67bc..7b491e5 100644 --- a/src_cpp/include/node_database.h +++ b/src_cpp/include/node_database.h @@ -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 { friend class NodeConnection; friend class DatabaseInitAsyncWorker; diff --git a/src_cpp/include/node_query_result.h b/src_cpp/include/node_query_result.h index 5e32fbf..3ce964a 100644 --- a/src_cpp/include/node_query_result.h +++ b/src_cpp/include/node_query_result.h @@ -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" @@ -12,7 +13,7 @@ using namespace lbug::processor; using namespace lbug::main; -class NodeQueryResult : public Napi::ObjectWrap { +class NodeQueryResult : public Napi::ObjectWrap, public DatabaseDependent { friend class NodeQueryResultGetNextAsyncWorker; friend class NodeQueryResultGetColumnMetadataAsyncWorker; friend class NodeQueryResultGetNextQueryResultAsyncWorker; @@ -47,6 +48,7 @@ class NodeQueryResult : public Napi::ObjectWrap { void PopulateColumnNames(); void Close(const Napi::CallbackInfo& info); void Close(); + void ReleaseDatabase() override; QueryResult& GetQueryResult() const; void AcquireAsyncUse(); void ReleaseAsyncUse(); diff --git a/src_cpp/node_connection.cpp b/src_cpp/node_connection.cpp index 45f381e..8e96aea 100644 --- a/src_cpp/node_connection.cpp +++ b/src_cpp/node_connection.cpp @@ -123,6 +123,21 @@ NodeConnection::NodeConnection(const Napi::CallbackInfo& info) Napi::HandleScope scope(env); NodeDatabase* nodeDatabase = Napi::ObjectWrap::Unwrap(info[0].As()); 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) { @@ -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) { diff --git a/src_cpp/node_database.cpp b/src_cpp/node_database.cpp index 57cab92..75b6caf 100644 --- a/src_cpp/node_database.cpp +++ b/src_cpp/node_database.cpp @@ -1,5 +1,48 @@ #include "include/node_database.h" +#include +#include +#include + +namespace { +// Query results register from the worker thread that produced them. +std::mutex dependentsMtx; +std::unordered_map> 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 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); @@ -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(); } diff --git a/src_cpp/node_query_result.cpp b/src_cpp/node_query_result.cpp index 3ab1a47..fcbd39b 100644 --- a/src_cpp/node_query_result.cpp +++ b/src_cpp/node_query_result.cpp @@ -59,10 +59,16 @@ void NodeQueryResult::AdoptQueryResult( std::unique_ptr queryResult, std::shared_ptr connection, std::shared_ptr 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 NodeQueryResult::DetachNextQueryResult() { @@ -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(); diff --git a/src_js/connection.js b/src_js/connection.js index 8bd6be5..63b2f05 100644 --- a/src_js/connection.js +++ b/src_js/connection.js @@ -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; @@ -611,6 +612,7 @@ class Connection { this._isInitialized = true; this._isClosed = true; delete this._connection; + this._database._untrackConnection(this._trackingRef); return; } } @@ -618,6 +620,7 @@ class Connection { this._connection.close(); delete this._connection; this._isClosed = true; + this._database._untrackConnection(this._trackingRef); } /** @@ -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); } } diff --git a/src_js/database.js b/src_js/database.js index ab79624..123b4be 100644 --- a/src_js/database.js +++ b/src_js/database.js @@ -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); } /** @@ -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) { @@ -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() { @@ -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; diff --git a/test/test_database.js b/test/test_database.js index 2743e1a..63d3012 100644 --- a/test/test_database.js +++ b/test/test_database.js @@ -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, _) => {