diff --git a/cpp/src/plasma/store.cc b/cpp/src/plasma/store.cc index 061b7ad13efc..171f062225e3 100644 --- a/cpp/src/plasma/store.cc +++ b/cpp/src/plasma/store.cc @@ -568,12 +568,17 @@ 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 client_fd The client to send the notification to. -void PlasmaStore::send_notifications(int client_fd) { - auto& notifications = pending_notifications_[client_fd].object_notifications; +/// @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; + auto& notifications = it->second.object_notifications; int num_processed = 0; bool closed = false; @@ -599,7 +604,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 +627,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 +662,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..9b3850b5a1fc 100644 --- a/cpp/src/plasma/store.h +++ b/cpp/src/plasma/store.h @@ -54,6 +54,8 @@ struct Client { class PlasmaStore { public: + using NotificationMap = std::unordered_map; + // 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