From 66a927c843aaed1c69125f4d1755c924bff3036d Mon Sep 17 00:00:00 2001 From: Philipp Moritz Date: Tue, 22 May 2018 01:40:06 -0700 Subject: [PATCH 1/4] fix iterator invalidation --- cpp/src/plasma/store.cc | 21 +++++++++++++-------- cpp/src/plasma/store.h | 6 ++++-- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/cpp/src/plasma/store.cc b/cpp/src/plasma/store.cc index 061b7ad13efc..94755b2d70f8 100644 --- a/cpp/src/plasma/store.cc +++ b/cpp/src/plasma/store.cc @@ -572,8 +572,10 @@ void PlasmaStore::disconnect_client(int client_fd) { /// buffered, and this will be called again when the send buffer has room. /// /// @param client_fd The client to send the notification to. -void PlasmaStore::send_notifications(int client_fd) { - auto& notifications = pending_notifications_[client_fd].object_notifications; +PlasmaStore::NotificationMap::iterator PlasmaStore::send_notifications( + PlasmaStore::NotificationMap::iterator it) { + int client_fd = it->first; + auto& notifications = it->second.object_notifications; int num_processed = 0; bool closed = false; @@ -599,7 +601,7 @@ void PlasmaStore::send_notifications(int client_fd) { // TODO(pcm): Introduce status codes and check in case the file descriptor // is added twice. loop_->AddFileEvent(client_fd, kEventLoopWrite, [this, client_fd](int events) { - send_notifications(client_fd); + send_notifications(pending_notifications_.find(client_fd)); }); break; } else { @@ -622,15 +624,18 @@ void PlasmaStore::send_notifications(int client_fd) { // Stop sending notifications if the pipe was broken. if (closed) { close(client_fd); - pending_notifications_.erase(client_fd); + return pending_notifications_.erase(it); + } else { + return ++it; } } void PlasmaStore::push_notification(ObjectInfoT* object_info) { - for (auto& element : pending_notifications_) { + auto it = pending_notifications_.begin(); + while (it != pending_notifications_.end()) { auto notification = create_object_info_buffer(object_info); - element.second.object_notifications.emplace_back(std::move(notification)); - send_notifications(element.first); + it->second.object_notifications.emplace_back(std::move(notification)); + it = send_notifications(it); } } @@ -654,7 +659,7 @@ void PlasmaStore::subscribe_to_updates(Client* client) { for (const auto& entry : store_info_.objects) { push_notification(&entry.second->info); } - send_notifications(fd); + send_notifications(pending_notifications_.find(fd)); } Status PlasmaStore::process_message(Client* client) { diff --git a/cpp/src/plasma/store.h b/cpp/src/plasma/store.h index 64c524909c7c..da7e23310340 100644 --- a/cpp/src/plasma/store.h +++ b/cpp/src/plasma/store.h @@ -54,6 +54,8 @@ struct Client { class PlasmaStore { public: + typedef std::unordered_map NotificationMap; + // TODO: PascalCase PlasmaStore methods. PlasmaStore(EventLoop* loop, int64_t system_memory, std::string directory, bool hugetlbfs_enabled); @@ -161,7 +163,7 @@ class PlasmaStore { /// @param client_fd The client file descriptor that is disconnected. void disconnect_client(int client_fd); - void send_notifications(int client_fd); + NotificationMap::iterator send_notifications(NotificationMap::iterator it); Status process_message(Client* client); @@ -194,7 +196,7 @@ class PlasmaStore { /// descriptor to an array of object_ids to send to that client. /// TODO(pcm): Consider putting this into the Client data structure and /// reorganize the code slightly. - std::unordered_map pending_notifications_; + NotificationMap pending_notifications_; std::unordered_map> connected_clients_; #ifdef PLASMA_GPU From 6b80ddcbb6be101219962f7c8b9ba8fff667dae9 Mon Sep 17 00:00:00 2001 From: Philipp Moritz Date: Tue, 22 May 2018 01:44:57 -0700 Subject: [PATCH 2/4] docs --- cpp/src/plasma/store.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cpp/src/plasma/store.cc b/cpp/src/plasma/store.cc index 94755b2d70f8..600c61ae0c4b 100644 --- a/cpp/src/plasma/store.cc +++ b/cpp/src/plasma/store.cc @@ -571,7 +571,8 @@ void PlasmaStore::disconnect_client(int client_fd) { /// be /// buffered, and this will be called again when the send buffer has room. /// -/// @param client_fd The client to send the notification to. +/// @param it Iterator that points to the client to send the notification to. +/// @return Iterator pointing to the next client. PlasmaStore::NotificationMap::iterator PlasmaStore::send_notifications( PlasmaStore::NotificationMap::iterator it) { int client_fd = it->first; From 820f9d6d7ee3969342fe2d0fea18ddb6aae90650 Mon Sep 17 00:00:00 2001 From: Philipp Moritz Date: Tue, 22 May 2018 17:36:20 -0700 Subject: [PATCH 3/4] update --- cpp/src/plasma/store.cc | 6 ++++-- cpp/src/plasma/store.h | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/cpp/src/plasma/store.cc b/cpp/src/plasma/store.cc index 600c61ae0c4b..b569d516449f 100644 --- a/cpp/src/plasma/store.cc +++ b/cpp/src/plasma/store.cc @@ -568,8 +568,10 @@ void PlasmaStore::disconnect_client(int client_fd) { /// Send notifications about sealed objects to the subscribers. This is called /// in seal_object. If the socket's send buffer is full, the notification will -/// be -/// buffered, and this will be called again when the send buffer has room. +/// be buffered, and this will be called again when the send buffer has room. +/// Since we call erase on pending_notifications_, all iterators get +/// invalidated, which is why we return a valid iterator to the next client to +/// be used in push_notification. /// /// @param it Iterator that points to the client to send the notification to. /// @return Iterator pointing to the next client. diff --git a/cpp/src/plasma/store.h b/cpp/src/plasma/store.h index da7e23310340..9b3850b5a1fc 100644 --- a/cpp/src/plasma/store.h +++ b/cpp/src/plasma/store.h @@ -54,7 +54,7 @@ struct Client { class PlasmaStore { public: - typedef std::unordered_map NotificationMap; + using NotificationMap = std::unordered_map; // TODO: PascalCase PlasmaStore methods. PlasmaStore(EventLoop* loop, int64_t system_memory, std::string directory, From bec37f0d49c4f3bbfb64302858996a3165d91ba3 Mon Sep 17 00:00:00 2001 From: Philipp Moritz Date: Tue, 22 May 2018 17:42:22 -0700 Subject: [PATCH 4/4] linting --- cpp/src/plasma/store.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpp/src/plasma/store.cc b/cpp/src/plasma/store.cc index b569d516449f..171f062225e3 100644 --- a/cpp/src/plasma/store.cc +++ b/cpp/src/plasma/store.cc @@ -576,7 +576,7 @@ void PlasmaStore::disconnect_client(int client_fd) { /// @param it Iterator that points to the client to send the notification to. /// @return Iterator pointing to the next client. PlasmaStore::NotificationMap::iterator PlasmaStore::send_notifications( - PlasmaStore::NotificationMap::iterator it) { + PlasmaStore::NotificationMap::iterator it) { int client_fd = it->first; auto& notifications = it->second.object_notifications;