From 6ad4065de4e3070a875a8784aaf2a522901c04a6 Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Fri, 12 Mar 2021 17:13:56 +0100 Subject: [PATCH] Fix a few db access crashes --- src/Cache.cpp | 6 ++++++ src/ChatPage.cpp | 13 ++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/Cache.cpp b/src/Cache.cpp index 87dbfb8c..0817a2d1 100644 --- a/src/Cache.cpp +++ b/src/Cache.cpp @@ -787,6 +787,9 @@ Cache::isInitialized() std::string Cache::nextBatchToken() { + if (!env_.handle()) + throw lmdb::error("Env already closed", MDB_INVALID); + auto txn = lmdb::txn::begin(env_, nullptr, MDB_RDONLY); std::string_view token; @@ -1816,6 +1819,9 @@ Cache::getTimelineRange(const std::string &room_id) std::optional Cache::getTimelineIndex(const std::string &room_id, std::string_view event_id) { + if (event_id.empty() || room_id.empty()) + return {}; + auto txn = lmdb::txn::begin(env_, nullptr, MDB_RDONLY); lmdb::dbi orderDb; diff --git a/src/ChatPage.cpp b/src/ChatPage.cpp index 9a823f17..d7ea4301 100644 --- a/src/ChatPage.cpp +++ b/src/ChatPage.cpp @@ -873,11 +873,14 @@ ChatPage::trySync() http::client()->sync( opts, - [this, since = cache::nextBatchToken()](const mtx::responses::Sync &res, - mtx::http::RequestErr err) { - if (since != cache::nextBatchToken()) { - nhlog::net()->warn("Duplicate sync, dropping"); - return; + [this, since = opts.since](const mtx::responses::Sync &res, mtx::http::RequestErr err) { + try { + if (since != cache::nextBatchToken()) { + nhlog::net()->warn("Duplicate sync, dropping"); + return; + } + } catch (const lmdb::error &e) { + nhlog::db()->warn("Logged out in the mean time, dropping sync"); } if (err) {