From 7a863daa50e3bcd84621b0d3ea07f26b0d1aa819 Mon Sep 17 00:00:00 2001 From: Arseny Smirnov Date: Fri, 9 Aug 2019 21:30:01 +0300 Subject: [PATCH] DialogDb, MessagesDb: bugfix - call promises only after transaction is commited GitOrigin-RevId: 650db002f60fc4af3131935973d8f2b0d3040d1b --- td/telegram/DialogDb.cpp | 14 ++++++++++++-- td/telegram/MessagesDb.cpp | 19 +++++++++++++++---- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/td/telegram/DialogDb.cpp b/td/telegram/DialogDb.cpp index a966aa76..1277caac 100644 --- a/td/telegram/DialogDb.cpp +++ b/td/telegram/DialogDb.cpp @@ -321,10 +321,13 @@ class DialogDbAsync : public DialogDbAsyncInterface { Promise<> promise) { add_write_query([=, promise = std::move(promise), data = std::move(data), notification_groups = std::move(notification_groups)](Unit) mutable { - promise.set_result(sync_db_->add_dialog(dialog_id, order, std::move(data), std::move(notification_groups))); + this->on_write_result(std::move(promise), + sync_db_->add_dialog(dialog_id, order, std::move(data), std::move(notification_groups))); }); } - + void on_write_result(Promise<> promise, Status status) { + pending_write_results_.emplace_back(std::move(promise), std::move(status)); + } void get_notification_groups_by_last_notification_date(NotificationGroupKey notification_group_key, int32 limit, Promise> promise) { add_read_query(); @@ -357,6 +360,9 @@ class DialogDbAsync : public DialogDbAsyncInterface { static constexpr size_t MAX_PENDING_QUERIES_COUNT{50}; static constexpr double MAX_PENDING_QUERIES_DELAY{0.01}; + + //NB: order is important, destructor of pending_writes_ will change pending_write_results_ + std::vector, Status>> pending_write_results_; vector> pending_writes_; double wakeup_at_ = 0; template @@ -385,6 +391,10 @@ class DialogDbAsync : public DialogDbAsyncInterface { } sync_db_->commit_transaction().ensure(); pending_writes_.clear(); + for (auto &p : pending_write_results_) { + p.first.set_result(std::move(p.second)); + } + pending_write_results_.clear(); cancel_timeout(); } void timeout_expired() override { diff --git a/td/telegram/MessagesDb.cpp b/td/telegram/MessagesDb.cpp index 6b38ea87..521fa653 100644 --- a/td/telegram/MessagesDb.cpp +++ b/td/telegram/MessagesDb.cpp @@ -947,17 +947,21 @@ class MessagesDbAsync : public MessagesDbAsyncInterface { int64 random_id, int32 ttl_expires_at, int32 index_mask, int64 search_id, string text, NotificationId notification_id, BufferSlice data, Promise<> promise) { add_write_query([=, promise = std::move(promise), data = std::move(data), text = std::move(text)](Unit) mutable { - promise.set_result(sync_db_->add_message(full_message_id, unique_message_id, sender_user_id, random_id, - ttl_expires_at, index_mask, search_id, std::move(text), - notification_id, std::move(data))); + this->on_write_result( + std::move(promise), + sync_db_->add_message(full_message_id, unique_message_id, sender_user_id, random_id, ttl_expires_at, + index_mask, search_id, std::move(text), notification_id, std::move(data))); }); } void delete_message(FullMessageId full_message_id, Promise<> promise) { add_write_query([=, promise = std::move(promise)](Unit) mutable { - promise.set_result(sync_db_->delete_message(full_message_id)); + this->on_write_result(std::move(promise), sync_db_->delete_message(full_message_id)); }); } + void on_write_result(Promise<> promise, Status status) { + pending_write_results_.emplace_back(std::move(promise), std::move(status)); + } void delete_all_dialog_messages(DialogId dialog_id, MessageId from_message_id, Promise<> promise) { add_read_query(); promise.set_result(sync_db_->delete_all_dialog_messages(dialog_id, from_message_id)); @@ -1028,6 +1032,9 @@ class MessagesDbAsync : public MessagesDbAsyncInterface { static constexpr size_t MAX_PENDING_QUERIES_COUNT{50}; static constexpr double MAX_PENDING_QUERIES_DELAY{0.01}; + + //NB: order is important, destructor of pending_writes_ will change pending_write_results_ + std::vector, Status>> pending_write_results_; std::vector> pending_writes_; double wakeup_at_ = 0; template @@ -1056,6 +1063,10 @@ class MessagesDbAsync : public MessagesDbAsyncInterface { } sync_db_->commit_transaction().ensure(); pending_writes_.clear(); + for (auto &p : pending_write_results_) { + p.first.set_result(std::move(p.second)); + } + pending_write_results_.clear(); cancel_timeout(); } void timeout_expired() override {