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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion android/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
71 changes: 41 additions & 30 deletions cpp/OPDatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand All @@ -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<ThreadPool>();
worker_thread = std::make_shared<WorkerThread>();
db = opsqlite_libsql_open_remote(url, auth_token);

create_jsi_functions(rt, js_object);
Expand All @@ -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<ThreadPool>();
worker_thread = std::make_shared<WorkerThread>();

db =
opsqlite_libsql_open_sync(db_name, path, url, auth_token, sync_interval,
Expand All @@ -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<ThreadPool>();
worker_thread = std::make_shared<WorkerThread>();
db = opsqlite_open_remote(url, auth_token, base_path);

create_jsi_functions(rt, js_object);
Expand All @@ -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<ThreadPool>();
worker_thread = std::make_shared<WorkerThread>();

db =
opsqlite_open_sync(db_name, path, url, auth_token, remote_encryption_key);
Expand All @@ -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<ThreadPool>();
worker_thread = std::make_shared<WorkerThread>();

#ifdef OP_SQLITE_USE_SQLCIPHER
db = opsqlite_open(db_name, path, readOnly, failOnCreate, encryption_key);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -410,21 +411,23 @@ 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);
}
#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
Expand All @@ -447,7 +450,7 @@ void OPDatabase::create_jsi_functions(jsi::Runtime &rt,
: std::vector<JSVariant>();

return promisify(
rt, thread_pool,
rt, "executeRaw", worker_thread,
[this, query, params]() {
std::vector<std::vector<JSVariant>> results;
#ifdef OP_SQLITE_USE_LIBSQL
Expand Down Expand Up @@ -522,7 +525,7 @@ void OPDatabase::create_jsi_functions(jsi::Runtime &rt,
: std::vector<JSVariant>();

return promisify(
rt, thread_pool,
rt, "execute", worker_thread,
[this, query, params]() {
#ifdef OP_SQLITE_USE_LIBSQL
auto status = opsqlite_libsql_execute(db, query, &params);
Expand All @@ -546,7 +549,7 @@ void OPDatabase::create_jsi_functions(jsi::Runtime &rt,
: std::vector<JSVariant>();

return promisify(
rt, thread_pool,
rt, "executeWithHostObjects", worker_thread,
[this, query, params]() {
std::vector<DumbHostObject> results;
std::shared_ptr<std::vector<SmartHostObject>> metadata =
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<BatchResult>(std::move(prev));
Expand Down Expand Up @@ -821,8 +824,8 @@ void OPDatabase::create_jsi_functions(jsi::Runtime &rt,
}
#endif
auto preparedStatementHostObject =
std::make_shared<PreparedStatementHostObject>(db, statement,
thread_pool);
std::make_shared<PreparedStatementHostObject>(weak_from_this(), db,
statement, worker_thread);

return jsi::Object::createFromHostObject(rt, preparedStatementHostObject);
}));
Expand Down Expand Up @@ -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 {};
}));
Expand All @@ -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
Expand Down
7 changes: 5 additions & 2 deletions cpp/OPDatabase.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "OPThreadPool.hpp"
#include "OPTypes.hpp"
#include "OPWorkerThread.hpp"
#include <ReactCommon/CallInvoker.h>
#include <jsi/jsi.h>
#include <set>
Expand Down Expand Up @@ -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:
Expand All @@ -100,7 +103,7 @@ class JSI_EXPORT OPDatabase
// by the dead one.
std::shared_ptr<react::CallInvoker> invoker = opsqlite::invoker;
std::shared_ptr<std::atomic<bool>> alive = opsqlite::generation_alive;
std::shared_ptr<ThreadPool> thread_pool;
std::shared_ptr<WorkerThread> worker_thread;
std::string db_name;
std::string delete_db_name;
std::shared_ptr<jsi::Value> update_hook_callback;
Expand Down
1 change: 1 addition & 0 deletions cpp/OPMacros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 19 additions & 2 deletions cpp/OPPreparedStatementHostObject.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "OPPreparedStatementHostObject.hpp"
#include "OPDatabase.hpp"
#if OP_SQLITE_USE_LIBSQL
#include "libsql/OPLibsqlBridge.hpp"
#else
Expand All @@ -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");
}
Expand All @@ -32,7 +43,7 @@ jsi::Value PreparedStatementHostObject::get(jsi::Runtime &rt,
std::vector<JSVariant> 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, &params);
Expand All @@ -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");
}
Expand All @@ -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<DumbHostObject> results;
auto metadata = std::make_shared<std::vector<SmartHostObject>>();
Expand Down Expand Up @@ -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");
}
Expand Down
27 changes: 19 additions & 8 deletions cpp/OPPreparedStatementHostObject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,30 @@
#include <sqlite3.h>
#endif
#endif
#include "OPThreadPool.hpp"
#include "OPWorkerThread.hpp"
#include <string>
#include <utility>

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<ThreadPool> thread_pool)
: _db(db), _stmt(stmt), _thread_pool(thread_pool) {};
PreparedStatementHostObject(std::weak_ptr<OPDatabase> database, DB const &db,
libsql_stmt_t stmt,
std::shared_ptr<WorkerThread> worker_thread)
: _database(std::move(database)), _db(db), _stmt(stmt),
_worker_thread(worker_thread) {};
#else
PreparedStatementHostObject(sqlite3 *db, sqlite3_stmt *stmt,
std::shared_ptr<ThreadPool> thread_pool)
: _db(db), _stmt(stmt), _thread_pool(thread_pool) {};
PreparedStatementHostObject(std::weak_ptr<OPDatabase> database, sqlite3 *db,
sqlite3_stmt *stmt,
std::shared_ptr<WorkerThread> worker_thread)
: _database(std::move(database)), _db(db), _stmt(stmt),
_worker_thread(worker_thread) {};
#endif

~PreparedStatementHostObject() override;
Expand All @@ -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<OPDatabase> _database;
#ifdef OP_SQLITE_USE_LIBSQL
DB _db;
libsql_stmt_t _stmt;
Expand All @@ -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<ThreadPool> _thread_pool;
std::shared_ptr<WorkerThread> _worker_thread;
};

} // namespace opsqlite
2 changes: 1 addition & 1 deletion cpp/OPSqlite.cpp
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Loading
Loading