diff --git a/android/CMakeLists.txt b/android/CMakeLists.txt index 3b52778e..a132c506 100644 --- a/android/CMakeLists.txt +++ b/android/CMakeLists.txt @@ -43,7 +43,7 @@ add_library( SHARED ../cpp/OPSqlite.cpp ../cpp/OPUtils.cpp - ../cpp/OPThreadPool.cpp + ../cpp/OPWorkerThread.cpp ../cpp/OPSmartHostObject.cpp ../cpp/OPPreparedStatementHostObject.cpp ../cpp/OPDumbHostObject.cpp diff --git a/cpp/OPDatabase.cpp b/cpp/OPDatabase.cpp index c4a21bb0..213faa6c 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)); } } @@ -206,7 +205,7 @@ void OPDatabase::release_hooks() { OPDatabase::OPDatabase(jsi::Runtime &rt, jsi::Object &js_object, std::string &url, std::string &auth_token) : db_name(url) { - thread_pool = std::make_shared(); + worker_thread = std::make_shared(); db = opsqlite_libsql_open_remote(url, auth_token); create_jsi_functions(rt, js_object); @@ -221,7 +220,7 @@ OPDatabase::OPDatabase(jsi::Runtime &rt, jsi::Object &js_object, std::string &remote_encryption_key) : base_path(path), db_name(db_name), delete_db_name(db_name) { - thread_pool = std::make_shared(); + worker_thread = std::make_shared(); db = opsqlite_libsql_open_sync(db_name, path, url, auth_token, sync_interval, @@ -237,7 +236,7 @@ OPDatabase::OPDatabase(jsi::Runtime &rt, jsi::Object &js_object, std::string &base_path) : base_path(base_path), db_name(url), delete_db_name(turso_remote_db_name(url)) { - thread_pool = std::make_shared(); + worker_thread = std::make_shared(); db = opsqlite_open_remote(url, auth_token, base_path); create_jsi_functions(rt, js_object); @@ -250,7 +249,7 @@ OPDatabase::OPDatabase(jsi::Runtime &rt, jsi::Object &js_object, std::string &remote_encryption_key) : base_path(path), db_name(db_name), delete_db_name(db_name) { - thread_pool = std::make_shared(); + worker_thread = std::make_shared(); db = opsqlite_open_sync(db_name, path, url, auth_token, remote_encryption_key); @@ -265,7 +264,7 @@ OPDatabase::OPDatabase(jsi::Runtime &rt, jsi::Object &js_object, std::string &path, bool readOnly, bool failOnCreate, std::string &encryption_key) : base_path(base_path), db_name(db_name), delete_db_name(db_name) { - thread_pool = std::make_shared(); + worker_thread = std::make_shared(); #ifdef OP_SQLITE_USE_SQLCIPHER db = opsqlite_open(db_name, path, readOnly, failOnCreate, encryption_key); @@ -353,16 +352,18 @@ void OPDatabase::create_jsi_functions(jsi::Runtime &rt, js_object.setProperty(rt, "close", HFN(this) { invalidated = true; - // Abort pending native SQLite work before waiting on the thread pool. + // Abort pending native SQLite work before waiting on the worker thread. #if !defined(OP_SQLITE_USE_LIBSQL) && !defined(OP_SQLITE_USE_TURSO) if (db != nullptr) { sqlite3_interrupt(db); } #endif // Drain any in-flight async queries before closing the db handle. - // Without this, a queued/running execute() on the thread pool may + // Without this, a queued/running execute() on the worker thread 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. + worker_thread->shutdown(); release_hooks(); #ifdef OP_SQLITE_USE_LIBSQL opsqlite_libsql_close(db); @@ -410,8 +411,15 @@ 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. + // Abort pending native SQLite work before waiting on the worker thread. #if !defined(OP_SQLITE_USE_LIBSQL) && !defined(OP_SQLITE_USE_TURSO) if (db != nullptr) { sqlite3_interrupt(db); @@ -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"); - } + worker_thread->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", worker_thread, [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", worker_thread, [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", worker_thread, [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", worker_thread, [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", worker_thread, [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, worker_thread); 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 { + worker_thread->queue_work(task); + } catch (const WorkerThreadStoppedError &) { + // 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 {}; })); @@ -880,17 +890,18 @@ void OPDatabase::invalidate() { // Abort whatever is currently inside sqlite3_step so the drain below can // actually finish. Parity with the close and delete host functions, which - // already do this. Without it a long running query holds the pool past React - // Native's module invalidation budget, after which the runtime is destroyed - // anyway and the drain has bought nothing. + // already do this. Without it a long running query holds the worker thread + // past React Native's module invalidation budget, after which the runtime is + // destroyed anyway and the drain has bought nothing. #if !defined(OP_SQLITE_USE_LIBSQL) && !defined(OP_SQLITE_USE_TURSO) if (db != nullptr) { sqlite3_interrupt(db); } #endif - // Drain in-flight thread pool work before closing the db handle. - thread_pool->wait_finished(); + // Drain in-flight work and join the worker thread before closing the db + // handle. + worker_thread->shutdown(); release_hooks(); #ifdef OP_SQLITE_USE_LIBSQL diff --git a/cpp/OPDatabase.hpp b/cpp/OPDatabase.hpp index 9e387a6b..ba6948c6 100644 --- a/cpp/OPDatabase.hpp +++ b/cpp/OPDatabase.hpp @@ -1,7 +1,7 @@ #pragma once -#include "OPThreadPool.hpp" #include "OPTypes.hpp" +#include "OPWorkerThread.hpp" #include #include #include @@ -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: @@ -100,7 +103,7 @@ class JSI_EXPORT OPDatabase // by the dead one. std::shared_ptr invoker = opsqlite::invoker; std::shared_ptr> alive = opsqlite::generation_alive; - std::shared_ptr thread_pool; + std::shared_ptr worker_thread; std::string db_name; std::string delete_db_name; std::shared_ptr update_hook_callback; 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..ab39ee4e 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", _worker_thread, [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", _worker_thread, [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..695a89fb 100644 --- a/cpp/OPPreparedStatementHostObject.hpp +++ b/cpp/OPPreparedStatementHostObject.hpp @@ -13,7 +13,7 @@ #include #endif #endif -#include "OPThreadPool.hpp" +#include "OPWorkerThread.hpp" #include #include @@ -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, - std::shared_ptr thread_pool) - : _db(db), _stmt(stmt), _thread_pool(thread_pool) {}; + PreparedStatementHostObject(std::weak_ptr database, DB const &db, + libsql_stmt_t stmt, + std::shared_ptr worker_thread) + : _database(std::move(database)), _db(db), _stmt(stmt), + _worker_thread(worker_thread) {}; #else - PreparedStatementHostObject(sqlite3 *db, sqlite3_stmt *stmt, - std::shared_ptr thread_pool) - : _db(db), _stmt(stmt), _thread_pool(thread_pool) {}; + PreparedStatementHostObject(std::weak_ptr database, sqlite3 *db, + sqlite3_stmt *stmt, + std::shared_ptr worker_thread) + : _database(std::move(database)), _db(db), _stmt(stmt), + _worker_thread(worker_thread) {}; #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; @@ -48,7 +59,7 @@ class PreparedStatementHostObject : public jsi::HostObject { // This shouldn't be de-allocated until sqlite3_finalize is called on it sqlite3_stmt *_stmt; #endif - std::shared_ptr _thread_pool; + std::shared_ptr _worker_thread; }; } // namespace opsqlite diff --git a/cpp/OPSqlite.cpp b/cpp/OPSqlite.cpp index e97158f9..3ee40eaa 100644 --- a/cpp/OPSqlite.cpp +++ b/cpp/OPSqlite.cpp @@ -1,7 +1,7 @@ #include "OPSqlite.hpp" #include "OPDatabase.hpp" #include "OPDumbHostObject.hpp" -#include "OPThreadPool.hpp" +#include "OPWorkerThread.hpp" #ifdef OP_SQLITE_USE_LIBSQL #include "libsql/OPLibsqlBridge.hpp" #else diff --git a/cpp/OPThreadPool.cpp b/cpp/OPThreadPool.cpp deleted file mode 100644 index 6b7fd24a..00000000 --- a/cpp/OPThreadPool.cpp +++ /dev/null @@ -1,108 +0,0 @@ -#include "OPThreadPool.hpp" - -namespace opsqlite { - -ThreadPool::ThreadPool() : done(false) { - // 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 - // auto number_of_threads = std::thread::hardware_concurrency(); - // if (number_of_threads == 0) { - // number_of_threads = 1; - // } - - // To keep sqlite threading model working, we cannot access via multiple - // threads in the same process therefore we cap the number of off-threads - // to one per connection - // - // 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); - } -} - -// 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; - - // Wake up all the threads, so they can finish and be joined - work_pending.notify_all(); - - for (auto &thread : threads) { - if (thread.joinable()) { - thread.join(); - } - } - - threads.clear(); -} - -// 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); - - // Push the request to the queue - work_queue.push(task); - - // Wake every waiter. wait_finished() and do_work() share this condition - // so we force all conditions to check - work_pending.notify_all(); -} - -// Function used by the threads to grab work from the queue -void ThreadPool::do_work() { - // Loop while the queue is not destructing - while (!done) { - std::function task; - - // Create a scope, so we don't lock the queue for longer than necessary - { - std::unique_lock g(work_queue_mutex); - work_pending.wait(g, [&] { - // Only wake up if there are elements in the queue or the - // program is shutting down - return done || !work_queue.empty(); - }); - - // If we are shutting down exit without trying to process more work - if (done) { - break; - } - - task = work_queue.front(); - work_queue.pop(); - ++busy; - } - - task(); - - // Release the task (and everything it captured, e.g. JSI values) before - // signalling idle, so wait_finished()/close() can't observe busy == 0 - // while task-owned resources are still pending destruction. - task = nullptr; - - { - std::lock_guard g(work_queue_mutex); - --busy; - } - work_pending.notify_all(); - } -} - -void ThreadPool::wait_finished() { - std::unique_lock g(work_queue_mutex); - work_pending.wait(g, [&] { return work_queue.empty() && (busy == 0); }); -} - -} // namespace opsqlite diff --git a/cpp/OPThreadPool.hpp b/cpp/OPThreadPool.hpp deleted file mode 100644 index 2294f794..00000000 --- a/cpp/OPThreadPool.hpp +++ /dev/null @@ -1,46 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace opsqlite { - -class ThreadPool { -public: - ThreadPool(); - ~ThreadPool(); - void queue_work(const std::function &task); - void wait_finished(); - -private: - unsigned int busy{}; - // This condition variable is used for the threads to wait until there is - // work to do - std::condition_variable_any work_pending; - - // We store the threads in a vector, so we can later stop them gracefully - std::vector threads; - - // Mutex to protect work_queue - std::mutex work_queue_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; - - // Function used by the threads to grab work from the queue - void do_work(); -}; - -} // namespace opsqlite \ No newline at end of file diff --git a/cpp/OPUtils.cpp b/cpp/OPUtils.cpp index 7f3ccb2d..401b262a 100644 --- a/cpp/OPUtils.cpp +++ b/cpp/OPUtils.cpp @@ -5,8 +5,8 @@ #ifndef OP_SQLITE_USE_LIBSQL #include "OPBridge.hpp" #endif -#include "OPThreadPool.hpp" #include "OPMacros.hpp" +#include "OPWorkerThread.hpp" #include #include #include @@ -382,7 +382,7 @@ void log_to_console(jsi::Runtime &runtime, const std::string &message) { log.call(runtime, jsi::String::createFromUtf8(runtime, message)); } -/// Rejects a promisified call from the thread pool. +/// Rejects a promisified call from the worker thread. /// /// `resolve` is captured in the invokeAsync lambda alongside `reject` so it is /// disposed on the JS thread. A negative `code` means the failure did not come @@ -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 worker_thread, 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), + worker_thread, 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 { + worker_thread->queue_work(task); + } catch (const WorkerThreadStoppedError &) { + // 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..01eafbd8 100644 --- a/cpp/OPUtils.hpp +++ b/cpp/OPUtils.hpp @@ -9,12 +9,12 @@ #else #include #endif +#include "OPWorkerThread.hpp" #include #include #include #include #include -#include "OPThreadPool.hpp" namespace opsqlite { @@ -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 worker thread 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 worker_thread, + std::function lambda, std::function resolve_callback); diff --git a/cpp/OPWorkerThread.cpp b/cpp/OPWorkerThread.cpp new file mode 100644 index 00000000..62ae4764 --- /dev/null +++ b/cpp/OPWorkerThread.cpp @@ -0,0 +1,79 @@ +#include "OPWorkerThread.hpp" + +namespace opsqlite { + +WorkerThread::WorkerThread() : thread(&WorkerThread::do_work, this) { + thread_id = thread.get_id(); +} + +// Joins the thread so the process (or in our RN case, this +// main-thread/runtime generation) can exit gracefully. A no-op when shutdown() +// already ran. +WorkerThread::~WorkerThread() { shutdown(); } + +void WorkerThread::shutdown() { + // Checked before call_once: on the worker thread it could otherwise block on + // another caller's join of this very thread. + if (std::this_thread::get_id() == thread_id) { + throw std::system_error( + std::make_error_code(std::errc::resource_deadlock_would_occur), + "[op-sqlite] WorkerThread::shutdown() called from its own thread"); + } + + std::call_once(shutdown_once, [this] { + { + // Set under the mutex so the thread, between its predicate check and + // its wait, cannot miss the wake-up below. + std::lock_guard g(mutex); + done = true; + } + work_pending.notify_one(); + thread.join(); + }); +} + +// Called by the database and prepared statement host objects (through +// promisify and the reactive flush) for every async call +void WorkerThread::queue_work(const std::function &task) { + { + std::lock_guard g(mutex); + + // A stopped worker has no thread left to run the task, and its promise + // would never settle. Callers map this to their own "database is closed" + // error. + if (done) { + throw WorkerThreadStoppedError(); + } + + work_queue.push(task); + } + + work_pending.notify_one(); +} + +void WorkerThread::do_work() { + while (true) { + std::function task; + + // Create a scope, so we don't lock the queue for longer than necessary + { + std::unique_lock g(mutex); + work_pending.wait(g, [&] { return done || !work_queue.empty(); }); + + // Shutting down: finish only once every queued task has run, so no + // promise is left unsettled. + if (work_queue.empty()) { + return; + } + + task = std::move(work_queue.front()); + work_queue.pop(); + } + + // The task (and everything it captured, e.g. JSI values) is released at + // the end of this iteration, before the thread can exit and be joined. + task(); + } +} + +} // namespace opsqlite diff --git a/cpp/OPWorkerThread.hpp b/cpp/OPWorkerThread.hpp new file mode 100644 index 00000000..4321b7c3 --- /dev/null +++ b/cpp/OPWorkerThread.hpp @@ -0,0 +1,75 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +namespace opsqlite { + +// Thrown by queue_work() once the worker has been shut down. WorkerThread knows +// nothing about databases; callers map this to their own "database is closed" +// error. +class WorkerThreadStoppedError : public std::runtime_error { +public: + WorkerThreadStoppedError() + : std::runtime_error("[op-sqlite] worker thread is stopped") {} +}; + +// Runs a connection's async work, one task at a time, on a single background +// thread. To keep sqlite's threading model working a connection must not be +// used from several threads at once, so each connection gets exactly one +// (more threads were faster but raced; requested by PowerSync). +// +// Rule: a task must never own the WorkerThread it runs on, nor call +// shutdown(). The thread cannot join itself (shutdown() throws +// std::system_error). No task does today: the tasks queued by promisify and +// the reactive flush capture the database or the statement by raw pointer. +class WorkerThread { +public: + WorkerThread(); + ~WorkerThread(); + // Throws WorkerThreadStoppedError after shutdown(). + void queue_work(const std::function &task); + // Stops the worker: no new work is accepted, every task already queued still + // runs (so no promise is left unsettled), then the thread is joined. Called + // by close()/delete()/invalidate() rather than leaving it to the destructor, + // because the WorkerThread 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 blocks until the + // first caller's join has completed. + void shutdown(); + +private: + // Protects work_queue and done + std::mutex mutex; + + // Signalled when work is queued or the worker is shutting down + std::condition_variable work_pending; + + // Tasks waiting to run, in order + std::queue> work_queue; + + // Set by shutdown(). queue_work() then refuses new work, and the thread + // exits once the queue is empty. + bool done = false; + + std::once_flag shutdown_once; + + // Declared last so it starts only once everything above is constructed. If + // it fails to start, nothing else needs cleaning up. + std::thread thread; + + // Written only by the constructor, so shutdown() can read it without a lock + std::thread::id thread_id; + + // The thread's loop: runs queued tasks until shut down and drained + void do_work(); +}; + +} // namespace opsqlite diff --git a/example/src/tests/hooks.ts b/example/src/tests/hooks.ts index 1b623dc2..798c85ea 100644 --- a/example/src/tests/hooks.ts +++ b/example/src/tests/hooks.ts @@ -251,7 +251,7 @@ describe("Hooks", () => { // capture raw `this` in the invoker->invokeAsync lambda and read // commit_hook_callback/rollback_hook_callback (members of OPDatabase) when // the lambda finally ran on the JS thread. close()/delete() only drain the - // native thread pool, not the invoker queue, so a db freed right after the + // native worker thread, not the invoker queue, so a db freed right after the // hook fires but before the queued callback runs would leave that lambda // dereferencing a dangling OPDatabase. Closing/deleting immediately after // triggering the hook - without waiting for its callback to run - recreates 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-worker-thread.sh b/scripts/test-worker-thread.sh new file mode 100755 index 00000000..ac0cd058 --- /dev/null +++ b/scripts/test-worker-thread.sh @@ -0,0 +1,25 @@ +#!/bin/bash +# Builds scripts/worker_thread_check.cpp against cpp/OPWorkerThread.cpp and runs it +# under ThreadSanitizer and AddressSanitizer. No React Native toolchain needed. +# +# scripts/test-worker-thread.sh # both sanitizers +# scripts/test-worker-thread.sh thread # one of them +set -euo pipefail + +ROOT="$(cd "$(dirname "$0")/.." && pwd)" +OUT="${TMPDIR:-/tmp}/op-sqlite-worker-thread-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/OPWorkerThread.cpp" "$ROOT/scripts/worker_thread_check.cpp" \ + -o "$OUT/check-$sanitizer" + "$OUT/check-$sanitizer" +done diff --git a/scripts/worker_thread_check.cpp b/scripts/worker_thread_check.cpp new file mode 100644 index 00000000..3b7c09de --- /dev/null +++ b/scripts/worker_thread_check.cpp @@ -0,0 +1,365 @@ +// Standalone lifecycle checks for cpp/OPWorkerThread.cpp. No React Native, no +// JSI: it compiles WorkerThread on its own and drives it the way OPDatabase +// does. +// Run it under ThreadSanitizer and AddressSanitizer with +// scripts/test-worker-thread.sh. +// +// Every check runs under a watchdog: a hang (a join that never completes) is +// reported as a failure instead of stalling the run. +#include "OPWorkerThread.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef __APPLE__ +#include +#else +#include +#endif + +using opsqlite::WorkerThread; +using opsqlite::WorkerThreadStoppedError; +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 thread now, while the WorkerThread is still referenced +// (by a prepared statement, or by the JS object awaiting GC). +static void closed_workers_release_their_threads() { + const int base = thread_count(); + std::vector> held; + for (int i = 0; i < 300; i++) { + auto worker = std::make_shared(); + std::atomic ran{false}; + worker->queue_work([&] { ran = true; }); + worker->shutdown(); + if (!ran) { + check(false, "work queued before shutdown ran"); + } + held.push_back(worker); + } + const int extra = thread_count() - base; + check(extra <= 1, "300 shut down workers, still referenced, leave <= 1 " + "extra thread (extra=" + + std::to_string(extra) + ")"); +} + +// (b) Work queued on a stopped worker is refused with the worker's own error, +// so a promise rejects instead of waiting on a queue nobody drains. +static void queue_after_shutdown_throws() { + auto worker = std::make_shared(); + worker->shutdown(); + bool threw = false; + std::string what; + try { + worker->queue_work([] {}); + } catch (const WorkerThreadStoppedError &e) { + threw = true; + what = e.what(); + } + check(threw, "queue_work after shutdown throws WorkerThreadStoppedError"); + check(what == "[op-sqlite] worker thread is stopped", + "the error is the worker's generic one (\"" + what + "\")"); + worker->shutdown(); + check(true, "a second shutdown returns"); +} + +// (c) Tasks queued before shutdown() all run before it returns, and on the +// destructor path too. +static void queued_tasks_drain_on_shutdown() { + { + auto worker = std::make_shared(); + Gate gate; + std::atomic ran{0}; + worker->queue_work([&] { + gate.wait(); + ran++; + }); + for (int i = 0; i < 50; i++) { + worker->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(); + }); + worker->shutdown(); + check(ran == 51, "shutdown() drains the queue before returning (ran=" + + std::to_string(ran.load()) + "/51)"); + opener.join(); + } + { + std::atomic ran{0}; + Gate gate; + std::thread opener; + { + auto worker = std::make_shared(); + worker->queue_work([&] { + gate.wait(); + ran++; + }); + for (int i = 0; i < 50; i++) { + worker->queue_work([&] { ran++; }); + } + opener = std::thread([&] { + std::this_thread::sleep_for(20ms); + gate.release(); + }); + } // ~WorkerThread, no shutdown() 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 *worker = new WorkerThread(); + Gate started; + std::atomic task_finished{false}; + worker->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([&] { + worker->shutdown(); + first_saw_finished = task_finished.load(); + }); + std::thread second([&] { + // Start only once `first` has stopped the worker (queue_work refuses + // work from then on) and is on its way into the join. + while (true) { + try { + worker->queue_work([] {}); + } catch (const WorkerThreadStoppedError &) { + break; + } + std::this_thread::sleep_for(1ms); + } + std::this_thread::sleep_for(5ms); + worker->shutdown(); + second_saw_finished = task_finished.load(); + if (!second_saw_finished) { + // Report before the delete below, which then frees the worker under + // the running worker. + check(false, "the second shutdown() returned before the join " + "completed"); + } + delete worker; // ~WorkerThread 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 running on the worker breaks the rule in the +// header. It must fail loudly (std::system_error from the self-join) rather +// than hang or terminate, the tasks behind it must still run, and a later +// shutdown() from another thread must still join the worker cleanly. +static void shutdown_from_the_worker_throws() { + auto worker = std::make_shared(); + Gate queued; + std::atomic after{0}; + std::atomic threw_on_worker{false}; + worker->queue_work([&] { + queued.wait(); + try { + worker->shutdown(); + } catch (const std::system_error &) { + threw_on_worker = true; + } + }); + for (int i = 0; i < 10; i++) { + worker->queue_work([&] { after++; }); + } + + // Release the worker while this thread is already in its own shutdown(): + // the worker's call must throw rather than wait on a join of itself. + std::thread opener([&] { + std::this_thread::sleep_for(20ms); + queued.release(); + }); + worker->shutdown(); + opener.join(); + check(threw_on_worker, "shutdown() on the worker throws std::system_error"); + check(after == 10, "the tasks queued behind it still ran (" + + std::to_string(after.load()) + "/10)"); + worker.reset(); + check(true, "destructor after a worker-side shutdown attempt"); +} + +// (f) A constructor that cannot start its thread throws without terminating +// the process. +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 { + WorkerThread worker; + } 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 worker still works once threads are available again. + WorkerThread worker; + std::atomic ran{false}; + worker.queue_work([&] { ran = true; }); + worker.shutdown(); + check(ran, "a worker constructed afterwards works"); +} + +int main() { + with_watchdog("(a) closed workers release threads", 30s, + closed_workers_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_throws); + 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..d26722ef 100644 --- a/src/functions.ts +++ b/src/functions.ts @@ -175,10 +175,10 @@ function enhanceDB(db: _InternalDB, options: DBParams): DB { bindSync: (params: Scalar[]) => { stmt.bindSync(params); }, - bind: async (params: Scalar[]) => { - await stmt.bind(params); - }, - execute: stmt.execute, + bind: async (params: Scalar[]) => stmt.bind(params), + // async, like bind: a native throw (e.g. database is closed) becomes + // a rejection, as it does for db.execute + execute: async () => stmt.execute(), executeSync: stmt.executeSync, }; },