diff --git a/cpp/OPDatabase.cpp b/cpp/OPDatabase.cpp index c4a21bb0..0b20b115 100644 --- a/cpp/OPDatabase.cpp +++ b/cpp/OPDatabase.cpp @@ -181,8 +181,7 @@ void OPDatabase::sync_update_hook_registration() { void OPDatabase::throw_if_closed(const char *function_name) const { if (invalidated) { - throw std::runtime_error(std::string("[op-sqlite][") + function_name + - "] database is closed"); + throw std::runtime_error(database_closed_message(function_name)); } } @@ -362,7 +361,9 @@ void OPDatabase::create_jsi_functions(jsi::Runtime &rt, // Drain any in-flight async queries before closing the db handle. // Without this, a queued/running execute() on the thread pool may // dereference the freed sqlite3* pointer → heap corruption / SIGABRT. - thread_pool->wait_finished(); + // shutdown() runs everything already queued, then joins the worker now + // rather than when the JS object is collected. + thread_pool->shutdown(); release_hooks(); #ifdef OP_SQLITE_USE_LIBSQL opsqlite_libsql_close(db); @@ -410,6 +411,13 @@ void OPDatabase::create_jsi_functions(jsi::Runtime &rt, throw std::runtime_error("[op-sqlite] Delete no longer takes arguments"); } + // Checked before anything is torn down: the database stays open and usable + // when delete() is not supported. + if (delete_db_name.empty()) { + throw std::runtime_error("[op-sqlite][delete] delete() is not supported " + "for remote-only databases"); + } + invalidated = true; // Abort pending native SQLite work before waiting on the thread pool. #if !defined(OP_SQLITE_USE_LIBSQL) && !defined(OP_SQLITE_USE_TURSO) @@ -419,12 +427,7 @@ void OPDatabase::create_jsi_functions(jsi::Runtime &rt, #endif // Drain any in-flight async queries before closing/removing the db handle. // Without this, queued/running work may dereference a freed sqlite handle. - thread_pool->wait_finished(); - - if (delete_db_name.empty()) { - throw std::runtime_error("[op-sqlite][delete] delete() is not supported " - "for remote-only databases"); - } + thread_pool->shutdown(); release_hooks(); #ifdef OP_SQLITE_USE_LIBSQL @@ -447,7 +450,7 @@ void OPDatabase::create_jsi_functions(jsi::Runtime &rt, : std::vector(); return promisify( - rt, thread_pool, + rt, "executeRaw", thread_pool, [this, query, params]() { std::vector> results; #ifdef OP_SQLITE_USE_LIBSQL @@ -522,7 +525,7 @@ void OPDatabase::create_jsi_functions(jsi::Runtime &rt, : std::vector(); return promisify( - rt, thread_pool, + rt, "execute", thread_pool, [this, query, params]() { #ifdef OP_SQLITE_USE_LIBSQL auto status = opsqlite_libsql_execute(db, query, ¶ms); @@ -546,7 +549,7 @@ void OPDatabase::create_jsi_functions(jsi::Runtime &rt, : std::vector(); return promisify( - rt, thread_pool, + rt, "executeWithHostObjects", thread_pool, [this, query, params]() { std::vector results; std::shared_ptr> metadata = @@ -594,7 +597,7 @@ void OPDatabase::create_jsi_functions(jsi::Runtime &rt, to_batch_arguments(rt, batchParams, &commands); return promisify( - rt, thread_pool, + rt, "executeBatch", thread_pool, [this, commands]() { #ifdef OP_SQLITE_USE_LIBSQL auto batchResult = opsqlite_libsql_execute_batch(db, &commands); @@ -655,7 +658,7 @@ void OPDatabase::create_jsi_functions(jsi::Runtime &rt, const std::string sqlFileName = args[0].asString(rt).utf8(rt); return promisify( - rt, thread_pool, + rt, "loadFile", thread_pool, [this, sqlFileName]() { return import_sql_file(db, sqlFileName); }, [](jsi::Runtime &rt, std::any prev) { auto result = std::any_cast(std::move(prev)); @@ -821,8 +824,8 @@ void OPDatabase::create_jsi_functions(jsi::Runtime &rt, } #endif auto preparedStatementHostObject = - std::make_shared(db, statement, - thread_pool); + std::make_shared(weak_from_this(), db, + statement, thread_pool); return jsi::Object::createFromHostObject(rt, preparedStatementHostObject); })); @@ -862,7 +865,14 @@ void OPDatabase::create_jsi_functions(jsi::Runtime &rt, flush_pending_reactive_queries(resolve); }; - thread_pool->queue_work(task); + try { + thread_pool->queue_work(task); + } catch (const ThreadPoolStoppedError &) { + // Thrown from the executor, so the promise rejects with the same error + // as every other call on a closed database. + throw std::runtime_error( + database_closed_message("flushPendingReactiveQueries")); + } return {}; })); @@ -889,8 +899,9 @@ void OPDatabase::invalidate() { } #endif - // Drain in-flight thread pool work before closing the db handle. - thread_pool->wait_finished(); + // Drain in-flight thread pool work and join the worker before closing the + // db handle. + thread_pool->shutdown(); release_hooks(); #ifdef OP_SQLITE_USE_LIBSQL diff --git a/cpp/OPDatabase.hpp b/cpp/OPDatabase.hpp index 9e387a6b..a1c5cbc6 100644 --- a/cpp/OPDatabase.hpp +++ b/cpp/OPDatabase.hpp @@ -77,6 +77,9 @@ class JSI_EXPORT OPDatabase void on_commit(); void on_rollback(); void invalidate(); + // True once close(), delete() or invalidate() ran. Prepared statements check + // it before touching the connection handle they were created from. + bool is_closed() const { return invalidated; } ~OPDatabase() override; private: diff --git a/cpp/OPMacros.hpp b/cpp/OPMacros.hpp index 7537e24b..022173ef 100644 --- a/cpp/OPMacros.hpp +++ b/cpp/OPMacros.hpp @@ -5,3 +5,4 @@ #define HFN(c1) jsi::Function::createFromHostFunction(rt, jsi::PropNameID::forAscii(rt, ""), 0, [c1](jsi::Runtime &rt, const jsi::Value &that, const jsi::Value *args, size_t count) -> jsi::Value #define HFN2(c1, c2) jsi::Function::createFromHostFunction(rt, jsi::PropNameID::forAscii(rt, ""), 0, [c1, c2](jsi::Runtime &rt, const jsi::Value &that, const jsi::Value *args, size_t count) -> jsi::Value #define HFN3(c1, c2, c3) jsi::Function::createFromHostFunction(rt, jsi::PropNameID::forAscii(rt, ""), 0, [c1, c2, c3](jsi::Runtime &rt, const jsi::Value &that, const jsi::Value *args, size_t count) -> jsi::Value +#define HFN4(c1, c2, c3, c4) jsi::Function::createFromHostFunction(rt, jsi::PropNameID::forAscii(rt, ""), 0, [c1, c2, c3, c4](jsi::Runtime &rt, const jsi::Value &that, const jsi::Value *args, size_t count) -> jsi::Value diff --git a/cpp/OPPreparedStatementHostObject.cpp b/cpp/OPPreparedStatementHostObject.cpp index a9ec17b6..7c2b5921 100644 --- a/cpp/OPPreparedStatementHostObject.cpp +++ b/cpp/OPPreparedStatementHostObject.cpp @@ -1,4 +1,5 @@ #include "OPPreparedStatementHostObject.hpp" +#include "OPDatabase.hpp" #if OP_SQLITE_USE_LIBSQL #include "libsql/OPLibsqlBridge.hpp" #else @@ -18,12 +19,22 @@ PreparedStatementHostObject::getPropertyNames(jsi::Runtime &rt) { return keys; } +void PreparedStatementHostObject::throw_if_closed( + const char *function_name) const { + auto database = _database.lock(); + if (database == nullptr || database->is_closed()) { + throw std::runtime_error(database_closed_message(function_name)); + } +} + jsi::Value PreparedStatementHostObject::get(jsi::Runtime &rt, const jsi::PropNameID &propNameID) { auto name = propNameID.utf8(rt); if (name == "bind") { return HFN(this) { + throw_if_closed("bind"); + if (_stmt == nullptr) { throw std::runtime_error("statement has been freed"); } @@ -32,7 +43,7 @@ jsi::Value PreparedStatementHostObject::get(jsi::Runtime &rt, std::vector params = to_variant_vec(rt, js_params); return promisify( - rt, _thread_pool, + rt, "bind", _thread_pool, [this, params]() { #ifdef OP_SQLITE_USE_LIBSQL opsqlite_libsql_bind_statement(_stmt, ¶ms); @@ -49,6 +60,8 @@ jsi::Value PreparedStatementHostObject::get(jsi::Runtime &rt, if (name == "bindSync") { return HFN(this) { + throw_if_closed("bindSync"); + if (_stmt == nullptr) { throw std::runtime_error("statement has been freed"); } @@ -72,12 +85,14 @@ jsi::Value PreparedStatementHostObject::get(jsi::Runtime &rt, if (name == "execute") { return HFN(this) { + throw_if_closed("execute"); + if (_stmt == nullptr) { throw std::runtime_error("statement has been freed"); } return promisify( - rt, _thread_pool, + rt, "execute", _thread_pool, [this]() { std::vector results; auto metadata = std::make_shared>(); @@ -107,6 +122,8 @@ jsi::Value PreparedStatementHostObject::get(jsi::Runtime &rt, } if (name == "executeSync") { return HFN(this) { + throw_if_closed("executeSync"); + if (_stmt == nullptr) { throw std::runtime_error("statement has been freed"); } diff --git a/cpp/OPPreparedStatementHostObject.hpp b/cpp/OPPreparedStatementHostObject.hpp index 567364a4..82376fca 100644 --- a/cpp/OPPreparedStatementHostObject.hpp +++ b/cpp/OPPreparedStatementHostObject.hpp @@ -21,16 +21,22 @@ namespace opsqlite { namespace jsi = facebook::jsi; namespace react = facebook::react; +class OPDatabase; + class PreparedStatementHostObject : public jsi::HostObject { public: #ifdef OP_SQLITE_USE_LIBSQL - PreparedStatementHostObject(DB const &db, libsql_stmt_t stmt, + PreparedStatementHostObject(std::weak_ptr database, DB const &db, + libsql_stmt_t stmt, std::shared_ptr thread_pool) - : _db(db), _stmt(stmt), _thread_pool(thread_pool) {}; + : _database(std::move(database)), _db(db), _stmt(stmt), + _thread_pool(thread_pool) {}; #else - PreparedStatementHostObject(sqlite3 *db, sqlite3_stmt *stmt, + PreparedStatementHostObject(std::weak_ptr database, sqlite3 *db, + sqlite3_stmt *stmt, std::shared_ptr thread_pool) - : _db(db), _stmt(stmt), _thread_pool(thread_pool) {}; + : _database(std::move(database)), _db(db), _stmt(stmt), + _thread_pool(thread_pool) {}; #endif ~PreparedStatementHostObject() override; @@ -40,6 +46,11 @@ class PreparedStatementHostObject : public jsi::HostObject { jsi::Value get(jsi::Runtime &rt, const jsi::PropNameID &propNameID) override; private: + // Throws the same error as OPDatabase::throw_if_closed once the database + // this statement came from is closed (or gone): `_db` is no longer valid. + void throw_if_closed(const char *function_name) const; + + std::weak_ptr _database; #ifdef OP_SQLITE_USE_LIBSQL DB _db; libsql_stmt_t _stmt; diff --git a/cpp/OPThreadPool.cpp b/cpp/OPThreadPool.cpp index 6b7fd24a..7bfd4e13 100644 --- a/cpp/OPThreadPool.cpp +++ b/cpp/OPThreadPool.cpp @@ -1,8 +1,10 @@ #include "OPThreadPool.hpp" +#include + namespace opsqlite { -ThreadPool::ThreadPool() : done(false) { +ThreadPool::ThreadPool() { // This returns the number of threads supported by the system. If the // function can't figure out this information, it returns 0. 0 is not good, // so we create at least 1 @@ -18,40 +20,100 @@ ThreadPool::ThreadPool() : done(false) { // You can achieve more performance when using more threads but you run // into race conditions. This request was brought forth by PowerSync. auto number_of_threads = 1; - for (unsigned i = 0; i < number_of_threads; ++i) { - // The threads will execute the private member `do_work`. Note that we - // need to pass a reference to the function (namespaced with the class - // name) as the first argument, and the current object as second - // argument - threads.emplace_back(&ThreadPool::do_work, this); + try { + for (unsigned i = 0; i < number_of_threads; ++i) { + // The threads will execute the private member `do_work`. Note that we + // need to pass a reference to the function (namespaced with the class + // name) as the first argument, and the current object as second + // argument + threads.emplace_back(&ThreadPool::do_work, this); + worker_ids.push_back(threads.back().get_id()); + } + } catch (...) { + // The destructor does not run when a constructor throws, and destroying a + // joinable std::thread calls std::terminate. Stop and join the threads + // that did start before letting the exception out. + stop_accepting_work(); + join_threads(); + throw; } } -// The destructor joins all the threads so the process (or in our RN case, this -// main-thread/runtime generation) can exit gracefully. This will be executed if -// there is any exception (e.g. creating the threads) -ThreadPool::~ThreadPool() { - // So threads know it's time to shut down - done = true; +// Joins the threads so the process (or in our RN case, this +// main-thread/runtime generation) can exit gracefully. A no-op when shutdown() +// already ran. Never runs on a worker as long as no task owns the pool (see the +// rule in the header). +ThreadPool::~ThreadPool() { shutdown(); } - // Wake up all the threads, so they can finish and be joined +bool ThreadPool::is_worker_thread() const { + return std::find(worker_ids.begin(), worker_ids.end(), + std::this_thread::get_id()) != worker_ids.end(); +} + +void ThreadPool::stop_accepting_work() { + { + // Set under the mutex so a worker between its predicate check and its + // wait cannot miss the wake-up below. + std::lock_guard g(work_queue_mutex); + done = true; + } + + // Wake up all the threads, so they can drain the queue and finish work_pending.notify_all(); +} +void ThreadPool::join_threads() { + // A second caller blocks here until the first caller's join has completed, + // so it can never free this object while the worker is still running. + std::lock_guard g(join_mutex); for (auto &thread : threads) { if (thread.joinable()) { thread.join(); } } - threads.clear(); } +void ThreadPool::shutdown() { + stop_accepting_work(); + + if (is_worker_thread()) { + // Called from a task (e.g. a database invalidated on the worker). The + // worker cannot join itself, and it will not pick up another task until + // this one returns, so run what is left here: when shutdown() returns, + // nothing else will run, as on every other thread. `busy` still counts the + // calling task, which do_work() releases as usual. + while (true) { + std::function task; + { + std::lock_guard g(work_queue_mutex); + if (work_queue.empty()) { + break; + } + task = std::move(work_queue.front()); + work_queue.pop(); + } + task(); + } + work_pending.notify_all(); + return; + } + + join_threads(); +} + // This function will be called by the server every time there is a request // that needs to be processed by the thread pool void ThreadPool::queue_work(const std::function &task) { // Grab the mutex std::lock_guard g(work_queue_mutex); + // A stopped pool has no worker left to run the task, and its promise would + // never settle. Callers map this to their own "database is closed" error. + if (done) { + throw ThreadPoolStoppedError(); + } + // Push the request to the queue work_queue.push(task); @@ -62,8 +124,7 @@ void ThreadPool::queue_work(const std::function &task) { // Function used by the threads to grab work from the queue void ThreadPool::do_work() { - // Loop while the queue is not destructing - while (!done) { + while (true) { std::function task; // Create a scope, so we don't lock the queue for longer than necessary @@ -75,8 +136,9 @@ void ThreadPool::do_work() { return done || !work_queue.empty(); }); - // If we are shutting down exit without trying to process more work - if (done) { + // Shutting down: finish only once every queued task has run, so no + // promise is left unsettled and wait_finished() cannot block forever. + if (work_queue.empty()) { break; } diff --git a/cpp/OPThreadPool.hpp b/cpp/OPThreadPool.hpp index 2294f794..4ee4054e 100644 --- a/cpp/OPThreadPool.hpp +++ b/cpp/OPThreadPool.hpp @@ -6,18 +6,47 @@ #include #include #include +#include #include #include #include namespace opsqlite { +// Thrown by queue_work() once the pool has been shut down. The pool knows +// nothing about databases; callers map this to their own "database is closed" +// error. +class ThreadPoolStoppedError : public std::runtime_error { +public: + ThreadPoolStoppedError() + : std::runtime_error("[op-sqlite] thread pool is stopped") {} +}; + +// Rule: a task must never own the pool it runs on (capture a shared_ptr to +// it). If a task released the last reference, ~ThreadPool would run on the +// worker while do_work() still uses `this`. No task does today: the tasks +// queued by promisify and the reactive flush capture the database or the +// statement by raw pointer, never the pool. class ThreadPool { public: ThreadPool(); ~ThreadPool(); + // Throws ThreadPoolStoppedError after shutdown(). void queue_work(const std::function &task); void wait_finished(); + // Stops the pool: no new work is accepted, every task already queued still + // runs (so no promise is left unsettled), then the worker is joined. When it + // returns, no other task will run. Called by close()/delete()/invalidate() + // rather than leaving it to the destructor, because the pool is shared with + // prepared statements and pending promises and may outlive the database + // until the JS object is garbage collected. + // + // Idempotent and safe to call concurrently: a later caller returns only + // after the first caller's join has completed. Called from a task on the + // worker itself, it runs the remaining queued tasks inline and returns + // without joining (a thread cannot join itself). The worker then exits after + // the current task, and a later shutdown() or the destructor joins it. + void shutdown(); private: unsigned int busy{}; @@ -25,22 +54,34 @@ class ThreadPool { // work to do std::condition_variable_any work_pending; - // We store the threads in a vector, so we can later stop them gracefully + // We store the threads in a vector, so we can later stop them gracefully. + // Only touched by the constructor and, under join_mutex, by shutdown(). std::vector threads; + // Ids of the worker threads. Written only by the constructor, so shutdown() + // can read it from any thread without a lock. + std::vector worker_ids; + // Mutex to protect work_queue std::mutex work_queue_mutex; + // Held by shutdown() while it joins, so a second caller waits for that join + // to finish instead of returning while the worker still uses this object. + std::mutex join_mutex; + // Queue of requests waiting to be processed std::queue> work_queue; - // This will be set to true when the thread pool is shutting down. This - // tells the threads to stop looping and finish. - // Atomic because do_work() reads it in `while (!done)` outside the mutex. - std::atomic done; + // Set (under work_queue_mutex) by shutdown(). queue_work() then refuses new + // work, and the threads stop once the queue is empty. + bool done{false}; + + bool is_worker_thread() const; + void stop_accepting_work(); + void join_threads(); // Function used by the threads to grab work from the queue void do_work(); }; -} // namespace opsqlite \ No newline at end of file +} // namespace opsqlite diff --git a/cpp/OPUtils.cpp b/cpp/OPUtils.cpp index 7f3ccb2d..7311c635 100644 --- a/cpp/OPUtils.cpp +++ b/cpp/OPUtils.cpp @@ -403,16 +403,21 @@ static void reject_with(const std::shared_ptr &invoker, }); } +std::string database_closed_message(const char *function_name) { + return std::string("[op-sqlite][") + function_name + "] database is closed"; +} + jsi::Value -promisify(jsi::Runtime &rt, std::shared_ptr thread_pool, +promisify(jsi::Runtime &rt, const char *function_name, + std::shared_ptr thread_pool, std::function lambda, std::function resolve_callback) { auto promise_constructor = rt.global().getPropertyAsFunction(rt, "Promise"); - auto executor = - HFN3(lambda = std::move(lambda), - resolve_callback = std::move(resolve_callback), thread_pool) { + auto executor = HFN4(lambda = std::move(lambda), + resolve_callback = std::move(resolve_callback), + thread_pool, function_name) { auto resolve = std::make_shared(rt, args[0]); auto reject = std::make_shared(rt, args[1]); @@ -465,7 +470,13 @@ promisify(jsi::Runtime &rt, std::shared_ptr thread_pool, } }; - thread_pool->queue_work(task); + try { + thread_pool->queue_work(task); + } catch (const ThreadPoolStoppedError &) { + // Thrown from the executor, so the promise rejects with the same error + // as every other call on a closed database. + throw std::runtime_error(database_closed_message(function_name)); + } return jsi::Value(nullptr); }); diff --git a/cpp/OPUtils.hpp b/cpp/OPUtils.hpp index 46c2e9a8..0e2a5ff1 100644 --- a/cpp/OPUtils.hpp +++ b/cpp/OPUtils.hpp @@ -107,8 +107,16 @@ jsi::Value create_js_error(jsi::Runtime &rt, const std::string &message, /// properties. A jsi::JSError carries our own Error object through untouched. [[noreturn]] void throw_js_error(jsi::Runtime &rt, const SQLiteError &error); +/// The message every call on a closed database fails with: +/// "[op-sqlite][] database is closed". +std::string database_closed_message(const char *function_name); + +/// `function_name` names the call in the "database is closed" rejection when +/// the pool has already been shut down. It must be a string literal. jsi::Value -promisify(jsi::Runtime &rt, std::shared_ptr thread_pool, std::function lambda, +promisify(jsi::Runtime &rt, const char *function_name, + std::shared_ptr thread_pool, + std::function lambda, std::function resolve_callback); diff --git a/example/src/tests/preparedStatements.ts b/example/src/tests/preparedStatements.ts index cfb128ee..bebf698a 100644 --- a/example/src/tests/preparedStatements.ts +++ b/example/src/tests/preparedStatements.ts @@ -125,4 +125,51 @@ describe("PreparedStatements", () => { const results = selectStatement.executeSync(); expect(results.rows.length).toEqual(5); }); + + it("prepared statement, execute after close rejects", async () => { + const statement = db.prepareStatement("SELECT * FROM User;"); + db.close(); + + // Rejects (does not throw synchronously), like db.execute after close + const promise = statement.execute(); + let error: any; + try { + await promise; + } catch (e) { + error = e; + } + + expect(error?.message).toContain( + "[op-sqlite][execute] database is closed", + ); + expect(error.code).toEqual(undefined); + }); + + it("prepared statement, sync calls after close throw", () => { + const statement = db.prepareStatement( + 'INSERT INTO "User" (id, name) VALUES(?,?);', + ); + db.close(); + + let executeError: any; + try { + statement.executeSync(); + } catch (e) { + executeError = e; + } + expect(executeError?.message).toContain( + "[op-sqlite][executeSync] database is closed", + ); + expect(executeError.code).toEqual(undefined); + + let bindError: any; + try { + statement.bindSync([4, "Juan"]); + } catch (e) { + bindError = e; + } + expect(bindError?.message).toContain( + "[op-sqlite][bindSync] database is closed", + ); + }); }); diff --git a/scripts/test-thread-pool.sh b/scripts/test-thread-pool.sh new file mode 100755 index 00000000..38691073 --- /dev/null +++ b/scripts/test-thread-pool.sh @@ -0,0 +1,25 @@ +#!/bin/bash +# Builds scripts/thread_pool_check.cpp against cpp/OPThreadPool.cpp and runs it +# under ThreadSanitizer and AddressSanitizer. No React Native toolchain needed. +# +# scripts/test-thread-pool.sh # both sanitizers +# scripts/test-thread-pool.sh thread # one of them +set -euo pipefail + +ROOT="$(cd "$(dirname "$0")/.." && pwd)" +OUT="${TMPDIR:-/tmp}/op-sqlite-thread-pool-check" +mkdir -p "$OUT" + +sanitizers=("$@") +if [ ${#sanitizers[@]} -eq 0 ]; then + sanitizers=(thread address) +fi + +for sanitizer in "${sanitizers[@]}"; do + echo "== -fsanitize=$sanitizer" + "${CXX:-clang++}" -std=c++17 -O1 -g -fno-omit-frame-pointer \ + -fsanitize="$sanitizer" -I "$ROOT/cpp" \ + "$ROOT/cpp/OPThreadPool.cpp" "$ROOT/scripts/thread_pool_check.cpp" \ + -o "$OUT/check-$sanitizer" + "$OUT/check-$sanitizer" +done diff --git a/scripts/thread_pool_check.cpp b/scripts/thread_pool_check.cpp new file mode 100644 index 00000000..c40ee201 --- /dev/null +++ b/scripts/thread_pool_check.cpp @@ -0,0 +1,374 @@ +// Standalone lifecycle checks for cpp/OPThreadPool.cpp. No React Native, no +// JSI: it compiles the pool on its own and drives it the way OPDatabase does. +// Run it under ThreadSanitizer and AddressSanitizer with +// scripts/test-thread-pool.sh. +// +// Every check runs under a watchdog: a hang (a wait_finished() that never +// returns, a join that never completes) is reported as a failure instead of +// stalling the run. +#include "OPThreadPool.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef __APPLE__ +#include +#else +#include +#endif + +using opsqlite::ThreadPool; +using opsqlite::ThreadPoolStoppedError; +using namespace std::chrono_literals; + +static int thread_count() { +#ifdef __APPLE__ + thread_act_array_t list; + mach_msg_type_number_t n = 0; + if (task_threads(mach_task_self(), &list, &n) != KERN_SUCCESS) { + return -1; + } + for (mach_msg_type_number_t i = 0; i < n; i++) { + mach_port_deallocate(mach_task_self(), list[i]); + } + vm_deallocate(mach_task_self(), (vm_address_t)list, n * sizeof(thread_act_t)); + return (int)n; +#else + int n = 0; + if (DIR *dir = opendir("/proc/self/task")) { + while (dirent *entry = readdir(dir)) { + if (entry->d_name[0] != '.') { + n++; + } + } + closedir(dir); + } + return n; +#endif +} + +static int failures = 0; + +static void check(bool condition, const std::string &message) { + std::printf("%s %s\n", condition ? "ok: " : "FAIL:", message.c_str()); + std::fflush(stdout); + if (!condition) { + failures++; + } +} + +// Runs `body` on its own thread and gives up on the whole process if it does +// not finish in time: a hung thread cannot be recovered. +template +static void with_watchdog(const char *name, std::chrono::milliseconds limit, + F body) { + std::mutex m; + std::condition_variable cv; + bool finished = false; + std::thread runner([&] { + body(); + std::lock_guard g(m); + finished = true; + cv.notify_all(); + }); + std::unique_lock lock(m); + if (!cv.wait_for(lock, limit, [&] { return finished; })) { + std::printf("FAIL: %s did not finish within %lld ms (hang)\n", name, + (long long)limit.count()); + std::printf("\nFAILED (hang)\n"); + std::fflush(stdout); + std::_Exit(2); + } + lock.unlock(); + runner.join(); +} + +// A one-shot gate a task can block on, so a test controls exactly when the +// worker is busy. +struct Gate { + std::mutex m; + std::condition_variable cv; + bool open = false; + void release() { + std::lock_guard g(m); + open = true; + cv.notify_all(); + } + void wait() { + std::unique_lock g(m); + cv.wait(g, [&] { return open; }); + } +}; + +// (a) close() frees the worker now, while the pool is still referenced (by a +// prepared statement, or by the JS object awaiting GC). +static void closed_pools_release_their_threads() { + const int base = thread_count(); + std::vector> held; + for (int i = 0; i < 300; i++) { + auto pool = std::make_shared(); + std::atomic ran{false}; + pool->queue_work([&] { ran = true; }); + pool->shutdown(); + if (!ran) { + check(false, "work queued before shutdown ran"); + } + held.push_back(pool); + } + const int extra = thread_count() - base; + check(extra <= 1, "300 shut down pools, still referenced, leave <= 1 extra " + "thread (extra=" + + std::to_string(extra) + ")"); +} + +// (b) Work queued on a stopped pool is refused with the pool's own error, so a +// promise rejects instead of waiting on a queue nobody drains. +static void queue_after_shutdown_throws() { + auto pool = std::make_shared(); + pool->shutdown(); + bool threw = false; + std::string what; + try { + pool->queue_work([] {}); + } catch (const ThreadPoolStoppedError &e) { + threw = true; + what = e.what(); + } + check(threw, "queue_work after shutdown throws ThreadPoolStoppedError"); + check(what == "[op-sqlite] thread pool is stopped", + "the error is the pool's generic one (\"" + what + "\")"); + pool->wait_finished(); + check(true, "wait_finished after shutdown returns"); +} + +// (c) Tasks queued before shutdown() all run before it returns, with or +// without a wait_finished() first, and on the destructor path too. +static void queued_tasks_drain_on_shutdown() { + { + auto pool = std::make_shared(); + Gate gate; + std::atomic ran{0}; + pool->queue_work([&] { + gate.wait(); + ran++; + }); + for (int i = 0; i < 50; i++) { + pool->queue_work([&] { ran++; }); + } + // The worker is parked in the first task, so all 50 are still queued + // when shutdown() starts. + std::thread opener([&] { + std::this_thread::sleep_for(20ms); + gate.release(); + }); + pool->shutdown(); + check(ran == 51, "shutdown() drains the queue before returning (ran=" + + std::to_string(ran.load()) + "/51)"); + pool->wait_finished(); + check(true, "wait_finished after a draining shutdown returns"); + opener.join(); + } + { + std::atomic ran{0}; + Gate gate; + std::thread opener; + { + auto pool = std::make_shared(); + pool->queue_work([&] { + gate.wait(); + ran++; + }); + for (int i = 0; i < 50; i++) { + pool->queue_work([&] { ran++; }); + } + opener = std::thread([&] { + std::this_thread::sleep_for(20ms); + gate.release(); + }); + } // ~ThreadPool, no shutdown() and no wait_finished() before it + check(ran == 51, "the destructor drains the queue too (ran=" + + std::to_string(ran.load()) + "/51)"); + opener.join(); + } +} + +// (d) Two concurrent shutdown() calls, then the destructor. Neither caller may +// return while the worker is still inside a task: the destructor that follows +// would free the mutex, condition variable and queue under it. +static void concurrent_shutdowns_then_destructor() { + for (int round = 0; round < 20; round++) { + auto *pool = new ThreadPool(); + Gate started; + std::atomic task_finished{false}; + pool->queue_work([&] { + started.release(); + std::this_thread::sleep_for(30ms); + task_finished = true; + }); + started.wait(); + + std::atomic first_saw_finished{false}; + std::atomic second_saw_finished{false}; + std::thread first([&] { + pool->shutdown(); + first_saw_finished = task_finished.load(); + }); + std::thread second([&] { + // Start only once `first` has stopped the pool (queue_work refuses + // work from then on) and is on its way into the join. + while (true) { + try { + pool->queue_work([] {}); + } catch (const ThreadPoolStoppedError &) { + break; + } + std::this_thread::sleep_for(1ms); + } + std::this_thread::sleep_for(5ms); + pool->shutdown(); + second_saw_finished = task_finished.load(); + if (!second_saw_finished) { + // Report before the delete below, which then frees the pool under + // the running worker. + check(false, "the second shutdown() returned before the join " + "completed"); + } + delete pool; // ~ThreadPool right after a shutdown() returned + }); + first.join(); + second.join(); + if (!first_saw_finished || !second_saw_finished) { + check(false, "round " + std::to_string(round) + + ": a shutdown() returned while the worker was still " + "running a task"); + return; + } + } + check(true, "concurrent shutdown() x2 + destructor: every caller waits for " + "the join (20 rounds)"); +} + +// (e) shutdown() from a task on the pool's own worker, as OPDatabase's +// invalidate()/destructor would if it ran there. It must not self-join, it +// must not hang, the tasks behind it must still run, and `busy` must be +// released so wait_finished() returns. +static void shutdown_from_the_worker() { + auto pool = std::make_shared(); + Gate queued; + std::atomic after{0}; + std::atomic after_seen_by_shutdown{-1}; + std::atomic threw_on_worker{false}; + pool->queue_work([&] { + queued.wait(); + pool->shutdown(); + // Runs the rest inline: nothing is left to run once shutdown() returns. + after_seen_by_shutdown = after.load(); + try { + pool->queue_work([] {}); + } catch (const ThreadPoolStoppedError &) { + threw_on_worker = true; + } + }); + for (int i = 0; i < 10; i++) { + pool->queue_work([&] { after++; }); + } + queued.release(); + + pool->wait_finished(); // hangs if do_work never decremented busy + check(true, "wait_finished returns after shutdown() ran on the worker"); + check(after_seen_by_shutdown == 10, + "shutdown() on the worker ran the queued tasks before returning " + "(saw " + + std::to_string(after_seen_by_shutdown.load()) + "/10)"); + check(threw_on_worker, "queue_work from the worker after its shutdown " + "throws"); + pool->wait_finished(); + check(true, "a second wait_finished also returns"); + pool->shutdown(); // joins the worker, which exited on its own + check(true, "shutdown() from another thread then joins the worker"); + pool.reset(); + check(true, "destructor after a worker-side shutdown"); +} + +// (f) A constructor that cannot start its thread throws without terminating +// the process. The pool starts one thread, so this exercises the failure of +// the first (and only) one; the catch block's join of already-started +// threads matters only if number_of_threads is raised. +static void constructor_throw_is_clean() { + std::mutex m; + std::condition_variable cv; + bool release = false; + std::vector hogs; + bool exhausted = false; + for (int i = 0; i < 10000; i++) { + try { + hogs.emplace_back([&] { + std::unique_lock g(m); + cv.wait(g, [&] { return release; }); + }); + } catch (const std::system_error &) { + exhausted = true; + break; + } + } + bool threw = false; + if (exhausted) { + try { + ThreadPool pool; + } catch (const std::system_error &) { + threw = true; + } + } + { + std::lock_guard g(m); + release = true; + } + cv.notify_all(); + for (auto &t : hogs) { + t.join(); + } + if (!exhausted) { + check(true, "constructor throw: could not exhaust threads (" + + std::to_string(hogs.size()) + " started), skipped"); + return; + } + check(threw, "constructor throws std::system_error when its thread cannot " + "start, without std::terminate (after " + + std::to_string(hogs.size()) + " threads)"); + // The pool still works once threads are available again. + ThreadPool pool; + std::atomic ran{false}; + pool.queue_work([&] { ran = true; }); + pool.shutdown(); + check(ran, "a pool constructed afterwards works"); +} + +int main() { + with_watchdog("(a) closed pools release threads", 30s, + closed_pools_release_their_threads); + with_watchdog("(b) queue after shutdown", 10s, queue_after_shutdown_throws); + with_watchdog("(c) drain on shutdown", 10s, queued_tasks_drain_on_shutdown); + with_watchdog("(d) concurrent shutdowns + destructor", 30s, + concurrent_shutdowns_then_destructor); + with_watchdog("(e) shutdown from the worker", 10s, shutdown_from_the_worker); + if (std::getenv("SKIP_CONSTRUCTOR_THROW") == nullptr) { + with_watchdog("(f) constructor throw", 60s, constructor_throw_is_clean); + } + + if (failures != 0) { + std::printf("\n%d FAILED\n", failures); + return 1; + } + std::printf("\nall passed\n"); + return 0; +} diff --git a/src/functions.ts b/src/functions.ts index fdf1185e..9fa528b4 100644 --- a/src/functions.ts +++ b/src/functions.ts @@ -178,7 +178,11 @@ function enhanceDB(db: _InternalDB, options: DBParams): DB { bind: async (params: Scalar[]) => { await stmt.bind(params); }, - execute: stmt.execute, + // async, like bind: a native throw (e.g. database is closed) becomes + // a rejection, as it does for db.execute + execute: async () => { + return await stmt.execute(); + }, executeSync: stmt.executeSync, }; },