Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: make BaseObject iteration order deterministic #48702

Merged
merged 1 commit into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/cleanup_queue-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ void CleanupQueue::Remove(Callback cb, void* arg) {

template <typename T>
void CleanupQueue::ForEachBaseObject(T&& iterator) const {
for (const auto& hook : cleanup_hooks_) {
std::vector<CleanupHookCallback> callbacks = GetOrdered();

for (const auto& hook : callbacks) {
BaseObject* obj = GetBaseObject(hook);
if (obj != nullptr) iterator(obj);
}
Expand Down
9 changes: 8 additions & 1 deletion src/cleanup_queue.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

namespace node {

void CleanupQueue::Drain() {
std::vector<CleanupQueue::CleanupHookCallback> CleanupQueue::GetOrdered()
const {
// Copy into a vector, since we can't sort an unordered_set in-place.
std::vector<CleanupHookCallback> callbacks(cleanup_hooks_.begin(),
cleanup_hooks_.end());
Expand All @@ -20,6 +21,12 @@ void CleanupQueue::Drain() {
return a.insertion_order_counter_ > b.insertion_order_counter_;
});

return callbacks;
}

void CleanupQueue::Drain() {
std::vector<CleanupHookCallback> callbacks = GetOrdered();

for (const CleanupHookCallback& cb : callbacks) {
if (cleanup_hooks_.count(cb) == 0) {
// This hook was removed from the `cleanup_hooks_` set during another
Expand Down
2 changes: 2 additions & 0 deletions src/cleanup_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <cstddef>
#include <cstdint>
#include <unordered_set>
#include <vector>

#include "memory_tracker.h"

Expand Down Expand Up @@ -66,6 +67,7 @@ class CleanupQueue : public MemoryRetainer {
uint64_t insertion_order_counter_;
};

std::vector<CleanupHookCallback> GetOrdered() const;
inline BaseObject* GetBaseObject(const CleanupHookCallback& callback) const;

// Use an unordered_set, so that we have efficient insertion and removal.
Expand Down