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

agents: fix OTLPAgent race conditions on cleanup #30

Merged
merged 1 commit into from
Nov 30, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions agents/otlp/src/otlp_agent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ static const size_t kTraceIdSize = 32;
static const size_t kSpanIdSize = 16;

static std::atomic<bool> is_running_ = { false };
nsuv::ns_rwlock exit_lock_;
static resource::ResourceAttributes resource_attributes_;

namespace node {
Expand Down Expand Up @@ -203,7 +204,7 @@ int OTLPAgent::config(const nlohmann::json& config) {
/*static*/ void OTLPAgent::config_agent_cb_(std::string config,
OTLPAgent* agent) {
// Check if the agent is already delete or if it's closing
nsuv::ns_rwlock::scoped_rdlock lock(agent->exit_lock_);
nsuv::ns_rwlock::scoped_rdlock lock(exit_lock_);
if (!is_running_) {
return;
}
Expand All @@ -217,6 +218,11 @@ int OTLPAgent::config(const nlohmann::json& config) {


/*static*/ void OTLPAgent::config_msg_cb_(nsuv::ns_async*, OTLPAgent* agent) {
nsuv::ns_rwlock::scoped_rdlock lock(exit_lock_);
if (!is_running_) {
return;
}

json config_msg;
while (agent->config_msg_q_.dequeue(config_msg)) {
agent->config(config_msg);
Expand All @@ -225,7 +231,7 @@ int OTLPAgent::config(const nlohmann::json& config) {


/*static*/ void OTLPAgent::env_msg_cb_(nsuv::ns_async*, OTLPAgent* agent) {
nsuv::ns_rwlock::scoped_rdlock lock(agent->exit_lock_);
nsuv::ns_rwlock::scoped_rdlock lock(exit_lock_);
if (!is_running_) {
return;
}
Expand All @@ -249,7 +255,7 @@ int OTLPAgent::config(const nlohmann::json& config) {

/*static*/ void OTLPAgent::on_thread_add_(SharedEnvInst envinst,
OTLPAgent* agent) {
nsuv::ns_rwlock::scoped_rdlock lock(agent->exit_lock_);
nsuv::ns_rwlock::scoped_rdlock lock(exit_lock_);
if (!is_running_) {
return;
}
Expand All @@ -261,7 +267,7 @@ int OTLPAgent::config(const nlohmann::json& config) {

/*static*/ void OTLPAgent::on_thread_remove_(SharedEnvInst envinst,
OTLPAgent* agent) {
nsuv::ns_rwlock::scoped_rdlock lock(agent->exit_lock_);
nsuv::ns_rwlock::scoped_rdlock lock(exit_lock_);
if (!is_running_) {
return;
}
Expand Down Expand Up @@ -309,7 +315,7 @@ void OTLPAgent::do_stop() {
void OTLPAgent::trace_hook_(Tracer* tracer,
const Tracer::SpanStor& stor,
OTLPAgent* agent) {
nsuv::ns_rwlock::scoped_rdlock lock(agent->exit_lock_);
nsuv::ns_rwlock::scoped_rdlock lock(exit_lock_);
if (!is_running_) {
return;
}
Expand All @@ -321,7 +327,7 @@ void OTLPAgent::trace_hook_(Tracer* tracer,

void OTLPAgent::span_msg_cb_(nsuv::ns_async*, OTLPAgent* agent) {
// Don't exit until all the pending spans are sent
nsuv::ns_rwlock::scoped_rdlock lock(agent->exit_lock_);
nsuv::ns_rwlock::scoped_rdlock lock(exit_lock_);
if (!is_running_) {
return;
}
Expand Down Expand Up @@ -396,7 +402,7 @@ void OTLPAgent::span_msg_cb_(nsuv::ns_async*, OTLPAgent* agent) {


/*static*/void OTLPAgent::metrics_timer_cb_(nsuv::ns_timer*, OTLPAgent* agent) {
nsuv::ns_rwlock::scoped_rdlock lock(agent->exit_lock_);
nsuv::ns_rwlock::scoped_rdlock lock(exit_lock_);
if (!is_running_) {
return;
}
Expand All @@ -411,7 +417,7 @@ void OTLPAgent::span_msg_cb_(nsuv::ns_async*, OTLPAgent* agent) {


/*static*/void OTLPAgent::metrics_msg_cb_(nsuv::ns_async*, OTLPAgent* agent) {
nsuv::ns_rwlock::scoped_rdlock lock(agent->exit_lock_);
nsuv::ns_rwlock::scoped_rdlock lock(exit_lock_);
if (!is_running_) {
return;
}
Expand All @@ -437,7 +443,7 @@ void OTLPAgent::span_msg_cb_(nsuv::ns_async*, OTLPAgent* agent) {

/*static*/void OTLPAgent::thr_metrics_cb_(ThreadMetrics* metrics,
OTLPAgent* agent) {
nsuv::ns_rwlock::scoped_rdlock lock(agent->exit_lock_);
nsuv::ns_rwlock::scoped_rdlock lock(exit_lock_);
if (!is_running_) {
return;
}
Expand Down
2 changes: 0 additions & 2 deletions agents/otlp/src/otlp_agent.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,6 @@ class OTLPAgent {
uv_cond_t start_cond_;
uv_mutex_t start_lock_;

nsuv::ns_rwlock exit_lock_;

bool hooks_init_;

nsuv::ns_async env_msg_;
Expand Down
Loading