Skip to content

Commit

Permalink
agents: fix ExitEvent condition handling
Browse files Browse the repository at this point in the history
A couple of fixes here:
- Both the `uv_mutex_t` and `uv_cond_t` should be initialized before
  defining the lambda.
- Add a boolean variable to make sure we're not waiting forever in the
  case the lambda code is run before reaching the `uv_cond_wait()`.

PR-URL: #222
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
  • Loading branch information
santigimeno committed Nov 21, 2024
1 parent fef5776 commit 39a6fb9
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions agents/grpc/src/grpc_agent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1592,24 +1592,29 @@ void GrpcAgent::send_exit() {
auto context = GrpcClient::MakeClientContext(agent_id_, saas_);
uv_cond_t cond;
uv_mutex_t lock;
bool signaled = false;

uv_mutex_init(&lock);
uv_cond_init(&cond);
GrpcClient::DelegateAsyncExport(
nsolid_service_stub_.get(), std::move(context), std::move(arena),
std::move(*exit_event),
[&lock, &cond](::grpc::Status,
[&lock, &cond, &signaled](::grpc::Status,
std::unique_ptr<Arena> &&,
const grpcagent::ExitEvent& event,
grpcagent::EventResponse*) {
uv_mutex_lock(&lock);
signaled = true;
uv_cond_signal(&cond);
uv_mutex_unlock(&lock);
return true;
});

// wait for the exit event to be sent
uv_mutex_init(&lock);
uv_cond_init(&cond);
uv_mutex_lock(&lock);
uv_cond_wait(&cond, &lock);
while (!signaled) {
uv_cond_wait(&cond, &lock);
}
uv_mutex_unlock(&lock);
uv_cond_destroy(&cond);
uv_mutex_destroy(&lock);
Expand Down

0 comments on commit 39a6fb9

Please sign in to comment.