From c31b9e27939a2f756056a818d83c64c9e43b721d Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Wed, 25 Nov 2020 23:43:31 +0100 Subject: [PATCH] Fix race when reading members from db --- src/Cache.cpp | 27 ++++++++++++++++++++++----- src/ChatPage.cpp | 15 ++++++++------- src/Utils.cpp | 30 +++++++++++++++--------------- src/Utils.h | 4 +++- src/timeline/TimelineModel.cpp | 5 ++++- 5 files changed, 52 insertions(+), 29 deletions(-) diff --git a/src/Cache.cpp b/src/Cache.cpp index bd0f3733..be9af8ee 100644 --- a/src/Cache.cpp +++ b/src/Cache.cpp @@ -1162,10 +1162,10 @@ Cache::saveState(const mtx::responses::Sync &res) saveTimelineMessages(txn, room.first, room.second.timeline); RoomInfo updatedInfo; - updatedInfo.name = getRoomName(txn, statesdb, membersdb).toStdString(); - updatedInfo.topic = getRoomTopic(txn, statesdb).toStdString(); + updatedInfo.name = getRoomName(txn, statesdb, membersdb).toStdString(); + updatedInfo.topic = getRoomTopic(txn, statesdb).toStdString(); updatedInfo.avatar_url = getRoomAvatarUrl(txn, statesdb, membersdb).toStdString(); - updatedInfo.version = getRoomVersion(txn, statesdb).toStdString(); + updatedInfo.version = getRoomVersion(txn, statesdb).toStdString(); // Process the account_data associated with this room if (!room.second.account_data.events.empty()) { @@ -1808,6 +1808,17 @@ Cache::getLastMessageInfo(lmdb::txn &txn, const std::string &room_id) e.what()); return {}; } + auto membersdb{0}; + + try { + membersdb = getMembersDb(txn, room_id); + } catch (lmdb::runtime_error &e) { + nhlog::db()->error("Can't open db for room '{}', probably doesn't exist yet. ({})", + room_id, + e.what()); + return {}; + } + if (orderDb.size(txn) == 0) return DescInfo{}; @@ -1850,9 +1861,16 @@ Cache::getLastMessageInfo(lmdb::txn &txn, const std::string &room_id) mtx::events::collections::TimelineEvent te; mtx::events::collections::from_json(obj, te); + lmdb::val info; + MemberInfo m; + if (lmdb::dbi_get( + txn, membersdb, lmdb::val(obj["sender"].get()), info)) { + m = json::parse(std::string_view(info.data(), info.size())); + } + cursor.close(); return utils::getMessageDescription( - te.data, local_user, QString::fromStdString(room_id)); + te.data, local_user, QString::fromStdString(m.name)); } cursor.close(); @@ -1911,7 +1929,6 @@ Cache::getRoomAvatarUrl(lmdb::txn &txn, lmdb::dbi &statesdb, lmdb::dbi &membersd // Resolve avatar for 1-1 chats. while (cursor.get(user_id, member_data, MDB_NEXT)) { - try { MemberInfo m = json::parse(member_data); if (user_id == localUserId_.toStdString()) { diff --git a/src/ChatPage.cpp b/src/ChatPage.cpp index 1fd9b9bc..6dfa62ef 100644 --- a/src/ChatPage.cpp +++ b/src/ChatPage.cpp @@ -20,7 +20,6 @@ #include #include #include -#include #include @@ -281,10 +280,12 @@ ChatPage::ChatPage(QSharedPointer userSettings, QWidget *parent) &ChatPage::setGroupViewState); connect(this, &ChatPage::initializeRoomList, room_list_, &RoomList::initialize); - connect(this, - &ChatPage::initializeViews, - view_manager_, - [this](const mtx::responses::Rooms &rooms) { view_manager_->sync(rooms); }); + connect( + this, + &ChatPage::initializeViews, + view_manager_, + [this](const mtx::responses::Rooms &rooms) { view_manager_->sync(rooms); }, + Qt::QueuedConnection); connect(this, &ChatPage::initializeEmptyViews, view_manager_, @@ -522,8 +523,6 @@ ChatPage::bootstrap(QString userid, QString homeserver, QString token) void ChatPage::loadStateFromCache() { - emit contentLoaded(); - nhlog::db()->info("restoring state from cache"); try { @@ -555,6 +554,8 @@ ChatPage::loadStateFromCache() getProfileInfo(); + emit contentLoaded(); + // Start receiving events. emit trySyncCb(); } diff --git a/src/Utils.cpp b/src/Utils.cpp index 2896e773..743c393f 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -27,12 +27,12 @@ QHash authorColors_; template static DescInfo -createDescriptionInfo(const Event &event, const QString &localUser, const QString &room_id) +createDescriptionInfo(const Event &event, const QString &localUser, const QString &displayName) { const auto msg = std::get(event); const auto sender = QString::fromStdString(msg.sender); - const auto username = cache::displayName(room_id, sender); + const auto username = displayName; const auto ts = QDateTime::fromMSecsSinceEpoch(msg.origin_server_ts); return DescInfo{QString::fromStdString(msg.event_id), @@ -153,7 +153,7 @@ utils::descriptiveTime(const QDateTime &then) DescInfo utils::getMessageDescription(const TimelineEvent &event, const QString &localUser, - const QString &room_id) + const QString &displayName) { using Audio = mtx::events::RoomEvent; using Emote = mtx::events::RoomEvent; @@ -168,31 +168,31 @@ utils::getMessageDescription(const TimelineEvent &event, using Encrypted = mtx::events::EncryptedEvent; if (std::holds_alternative