Skip to content

Commit 1399678

Browse files
committed
Store ids of all running actors in the registry
This is useful for debugging shutdown hangs, when an actor that does not terminate blocks the destructor of the actor system itself.
1 parent 4ff66bf commit 1399678

File tree

4 files changed

+26
-12
lines changed

4 files changed

+26
-12
lines changed

libcaf_core/caf/actor_registry.hpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <string>
1212
#include <thread>
1313
#include <unordered_map>
14+
#include <unordered_set>
1415

1516
#include "caf/abstract_actor.hpp"
1617
#include "caf/actor.hpp"
@@ -53,15 +54,18 @@ class CAF_CORE_EXPORT actor_registry {
5354

5455
/// Increases running-actors-count by one.
5556
/// @returns the increased count.
56-
size_t inc_running();
57+
size_t inc_running(actor_id key);
5758

5859
/// Decreases running-actors-count by one.
5960
/// @returns the decreased count.
60-
size_t dec_running();
61+
size_t dec_running(actor_id key);
6162

6263
/// Returns the number of currently running actors.
6364
size_t running() const;
6465

66+
/// Returns the the actor ids of all currently running actors.
67+
const std::unordered_set<actor_id>& running_ids() const;
68+
6569
/// Blocks the caller until running-actors-count becomes `expected`
6670
/// (must be either 0 or 1).
6771
void await_running_count_equal(size_t expected) const;
@@ -112,6 +116,7 @@ class CAF_CORE_EXPORT actor_registry {
112116

113117
mutable std::mutex running_mtx_;
114118
mutable std::condition_variable running_cv_;
119+
std::unordered_set<actor_id> running_;
115120

116121
mutable detail::shared_spinlock instances_mtx_;
117122
entries entries_;

libcaf_core/src/abstract_actor.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,15 @@ void abstract_actor::register_at_system() {
8080
if (getf(is_registered_flag))
8181
return;
8282
setf(is_registered_flag);
83-
[[maybe_unused]] auto count = home_system().registry().inc_running();
83+
[[maybe_unused]] auto count = home_system().registry().inc_running(id());
8484
CAF_LOG_DEBUG("actor" << id() << "increased running count to" << count);
8585
}
8686

8787
void abstract_actor::unregister_from_system() {
8888
if (!getf(is_registered_flag))
8989
return;
9090
unsetf(is_registered_flag);
91-
[[maybe_unused]] auto count = home_system().registry().dec_running();
91+
[[maybe_unused]] auto count = home_system().registry().dec_running(id());
9292
CAF_LOG_DEBUG("actor" << id() << "decreased running count to" << count);
9393
}
9494

libcaf_core/src/actor_registry.cpp

+15-6
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,27 @@ void actor_registry::erase(actor_id key) {
8080
}
8181
}
8282

83-
size_t actor_registry::inc_running() {
84-
return ++*system_.base_metrics().running_actors;
83+
size_t actor_registry::inc_running(actor_id key) {
84+
std::unique_lock<std::mutex> guard(running_mtx_);
85+
running_.emplace(key);
86+
return running_.size();
8587
}
8688

8789
size_t actor_registry::running() const {
88-
return static_cast<size_t>(system_.base_metrics().running_actors->value());
90+
std::unique_lock<std::mutex> guard(running_mtx_);
91+
return running_.size();
8992
}
9093

91-
size_t actor_registry::dec_running() {
92-
size_t new_val = --*system_.base_metrics().running_actors;
94+
const std::unordered_set<actor_id>& actor_registry::running_ids() const {
95+
std::unique_lock<std::mutex> guard(running_mtx_);
96+
return running_;
97+
}
98+
99+
size_t actor_registry::dec_running(actor_id key) {
100+
std::unique_lock<std::mutex> guard(running_mtx_);
101+
running_.erase(key);
102+
size_t new_val = running_.size();
93103
if (new_val <= 1) {
94-
std::unique_lock<std::mutex> guard(running_mtx_);
95104
running_cv_.notify_all();
96105
}
97106
return new_val;

libcaf_core/src/blocking_actor.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ class blocking_actor_runner : public resumable {
132132
auto& sys = ctx->system();
133133
sys.release_private_thread(thread_);
134134
if (!hidden_) {
135-
[[maybe_unused]] auto count = sys.registry().dec_running();
135+
[[maybe_unused]] auto count = sys.registry().dec_running(self_->id());
136136
CAF_LOG_DEBUG("actor" << self_->id() << "decreased running count to"
137137
<< count);
138138
}
@@ -166,7 +166,7 @@ void blocking_actor::launch(execution_unit*, bool, bool hide) {
166166
// Note: must *not* call register_at_system() to stop actor cleanup from
167167
// decrementing the count before releasing the thread.
168168
if (!hide) {
169-
[[maybe_unused]] auto count = sys.registry().inc_running();
169+
[[maybe_unused]] auto count = sys.registry().inc_running(id());
170170
CAF_LOG_DEBUG("actor" << id() << "increased running count to" << count);
171171
}
172172
thread->resume(new blocking_actor_runner(this, thread, hide));

0 commit comments

Comments
 (0)