fix(sqlite): eliminate lock-order inversion between progress trampoline and interrupt_mu
This commit is contained in:
1 parent
fdf0da5ca8
commit
84e6fe81ed
1 file changed
+37
-27
+37
-27
@@ -425,23 +425,26 @@ namespace Void::Sqlite
|
|||||||
~ConnectionState()
|
~ConnectionState()
|
||||||
{
|
{
|
||||||
if (db != nullptr) {
|
if (db != nullptr) {
|
||||||
|
// Unregister SQLite hooks outside interrupt_mu so db->mutex never
|
||||||
|
// nests inside interrupt_mu (lock order is strictly interrupt_mu -> db->mutex).
|
||||||
|
sqlite3_progress_handler(db, 0, nullptr, nullptr);
|
||||||
|
sqlite3_set_authorizer(db, nullptr, nullptr);
|
||||||
|
sqlite3* const db_to_close = db;
|
||||||
{
|
{
|
||||||
std::lock_guard lk{interrupt_mu};
|
std::lock_guard lk{interrupt_mu};
|
||||||
active_op_gen = 0;
|
active_op_gen = 0;
|
||||||
cancel_requested.store(false, std::memory_order_release);
|
cancel_requested.store(false, std::memory_order_release);
|
||||||
progress_predicate.reset();
|
db = nullptr;
|
||||||
sqlite3_progress_handler(db, 0, nullptr, nullptr);
|
|
||||||
sqlite3_set_authorizer(db, nullptr, nullptr);
|
|
||||||
}
|
}
|
||||||
const int rc = sqlite3_close(db);
|
progress_predicate.reset();
|
||||||
|
const int rc = sqlite3_close(db_to_close);
|
||||||
if (rc != SQLITE_OK) {
|
if (rc != SQLITE_OK) {
|
||||||
emit_diagnostic(std::format(
|
emit_diagnostic(std::format(
|
||||||
"Sqlite: deferred close failed (rc={}, msg={})",
|
"Sqlite: deferred close failed (rc={}, msg={})",
|
||||||
rc,
|
rc,
|
||||||
sqlite3_errmsg(db) ? sqlite3_errmsg(db) : "unknown"));
|
sqlite3_errmsg(db_to_close) ? sqlite3_errmsg(db_to_close) : "unknown"));
|
||||||
(void)sqlite3_close_v2(db);
|
(void)sqlite3_close_v2(db_to_close);
|
||||||
}
|
}
|
||||||
db = nullptr;
|
|
||||||
}
|
}
|
||||||
scalar_functions.clear();
|
scalar_functions.clear();
|
||||||
authorizer.reset();
|
authorizer.reset();
|
||||||
@@ -454,19 +457,17 @@ namespace Void::Sqlite
|
|||||||
if (conn == nullptr) {
|
if (conn == nullptr) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
// Invoked synchronously on the connection's worker thread while SQLite
|
||||||
|
// holds db->mutex; never acquire interrupt_mu here to avoid lock inversion.
|
||||||
if (conn->cancel_requested.load(std::memory_order_acquire)) {
|
if (conn->cancel_requested.load(std::memory_order_acquire)) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
std::shared_ptr<const std::function<bool()>> pred;
|
const auto pred = conn->progress_predicate;
|
||||||
{
|
if (!pred || !(*pred)) {
|
||||||
std::lock_guard lk{conn->interrupt_mu};
|
return 0;
|
||||||
if (conn->active_op_gen == 0 || !conn->progress_predicate) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
pred = conn->progress_predicate;
|
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
if (pred && (*pred) && (*pred)()) {
|
if ((*pred)()) {
|
||||||
conn->cancel_requested.store(true, std::memory_order_release);
|
conn->cancel_requested.store(true, std::memory_order_release);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -1874,20 +1875,23 @@ namespace Void::Sqlite
|
|||||||
injected));
|
injected));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sqlite3_progress_handler(m_conn->db, 0, nullptr, nullptr);
|
||||||
|
sqlite3_set_authorizer(m_conn->db, nullptr, nullptr);
|
||||||
{
|
{
|
||||||
std::lock_guard lk{m_conn->interrupt_mu};
|
std::lock_guard lk{m_conn->interrupt_mu};
|
||||||
m_conn->active_op_gen = 0;
|
m_conn->active_op_gen = 0;
|
||||||
m_conn->cancel_requested.store(false, std::memory_order_release);
|
m_conn->cancel_requested.store(false, std::memory_order_release);
|
||||||
m_conn->progress_predicate.reset();
|
|
||||||
sqlite3_progress_handler(m_conn->db, 0, nullptr, nullptr);
|
|
||||||
sqlite3_set_authorizer(m_conn->db, nullptr, nullptr);
|
|
||||||
}
|
}
|
||||||
|
m_conn->progress_predicate.reset();
|
||||||
|
|
||||||
const int rc = sqlite3_close(m_conn->db);
|
const int rc = sqlite3_close(m_conn->db);
|
||||||
if (rc != SQLITE_OK) {
|
if (rc != SQLITE_OK) {
|
||||||
return Void::Result::fail(sqlite_error_from_db(m_conn->db, rc, "close"));
|
return Void::Result::fail(sqlite_error_from_db(m_conn->db, rc, "close"));
|
||||||
}
|
}
|
||||||
m_conn->db = nullptr;
|
{
|
||||||
|
std::lock_guard lk{m_conn->interrupt_mu};
|
||||||
|
m_conn->db = nullptr;
|
||||||
|
}
|
||||||
m_conn->closed = true;
|
m_conn->closed = true;
|
||||||
m_conn->scalar_functions.clear();
|
m_conn->scalar_functions.clear();
|
||||||
m_conn->authorizer.reset();
|
m_conn->authorizer.reset();
|
||||||
@@ -2466,9 +2470,6 @@ namespace Void::Sqlite
|
|||||||
std::function<bool()> should_interrupt)
|
std::function<bool()> should_interrupt)
|
||||||
{
|
{
|
||||||
if (!m_conn || m_conn->db == nullptr) return;
|
if (!m_conn || m_conn->db == nullptr) return;
|
||||||
std::lock_guard lk{m_conn->interrupt_mu};
|
|
||||||
m_conn->active_op_gen = generation;
|
|
||||||
m_conn->cancel_requested.store(false, std::memory_order_release);
|
|
||||||
m_conn->progress_predicate =
|
m_conn->progress_predicate =
|
||||||
should_interrupt
|
should_interrupt
|
||||||
? std::make_shared<const std::function<bool()>>(std::move(should_interrupt))
|
? std::make_shared<const std::function<bool()>>(std::move(should_interrupt))
|
||||||
@@ -2482,6 +2483,9 @@ namespace Void::Sqlite
|
|||||||
} else {
|
} else {
|
||||||
sqlite3_progress_handler(m_conn->db, 0, nullptr, nullptr);
|
sqlite3_progress_handler(m_conn->db, 0, nullptr, nullptr);
|
||||||
}
|
}
|
||||||
|
std::lock_guard lk{m_conn->interrupt_mu};
|
||||||
|
m_conn->active_op_gen = generation;
|
||||||
|
m_conn->cancel_requested.store(false, std::memory_order_release);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Database::interrupt_leased_operation(std::uint64_t generation) noexcept
|
void Database::interrupt_leased_operation(std::uint64_t generation) noexcept
|
||||||
@@ -2497,14 +2501,20 @@ namespace Void::Sqlite
|
|||||||
void Database::end_leased_operation(std::uint64_t generation) noexcept
|
void Database::end_leased_operation(std::uint64_t generation) noexcept
|
||||||
{
|
{
|
||||||
if (!m_conn) return;
|
if (!m_conn) return;
|
||||||
std::lock_guard lk{m_conn->interrupt_mu};
|
bool ended = false;
|
||||||
if (m_conn->active_op_gen == generation) {
|
{
|
||||||
m_conn->active_op_gen = 0;
|
std::lock_guard lk{m_conn->interrupt_mu};
|
||||||
m_conn->cancel_requested.store(false, std::memory_order_release);
|
if (m_conn->active_op_gen == generation) {
|
||||||
m_conn->progress_predicate.reset();
|
m_conn->active_op_gen = 0;
|
||||||
|
m_conn->cancel_requested.store(false, std::memory_order_release);
|
||||||
|
ended = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (ended) {
|
||||||
if (m_conn->db != nullptr) {
|
if (m_conn->db != nullptr) {
|
||||||
sqlite3_progress_handler(m_conn->db, 0, nullptr, nullptr);
|
sqlite3_progress_handler(m_conn->db, 0, nullptr, nullptr);
|
||||||
}
|
}
|
||||||
|
m_conn->progress_predicate.reset();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user