Skip to content

Commit cb9c5da

Browse files
GuestPlatform Teamcopybara-github
GuestPlatform Team
authored andcommitted
Make the random number generation and connection id thread-safe.
Given the random number generation and connection id are both closely tied up with write logic and creation of reactor_, so it would make sense to re-use `reactor_mtx_` instead of generating another dedicated lock. Tested with 5000 runs with this CL with TSAN finding nothing. PiperOrigin-RevId: 692219060
1 parent c4bdf55 commit cb9c5da

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

cpp/acs_agent_client.cc

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
#include <algorithm>
44
#include <chrono>
5+
#include <cstdint>
56
#include <future>
7+
#include <limits>
68
#include <memory>
79
#include <queue>
810
#include <string>
@@ -15,6 +17,7 @@
1517
#include "third_party/absl/functional/bind_front.h"
1618
#include "third_party/absl/log/absl_log.h"
1719
#include "third_party/absl/memory/memory.h"
20+
#include "third_party/absl/random/distributions.h"
1821
#include "third_party/absl/status/status.h"
1922
#include "third_party/absl/status/statusor.h"
2023
#include "third_party/absl/strings/str_cat.h"
@@ -83,7 +86,10 @@ absl::Status AcsAgentClient::AddRequest(Request& request) {
8386
// TODO: Make the retry parameters configurable.
8487
for (int i = 0; i < 5; ++i) {
8588
// Generate a new message id for each attempt.
86-
request.set_message_id(CreateMessageUuid());
89+
{
90+
absl::MutexLock lock(&reactor_mtx_);
91+
request.set_message_id(CreateMessageUuid());
92+
}
8793
latest_send_request_status = AddRequestAndWaitForResponse(request);
8894
if (latest_send_request_status.ok()) {
8995
return absl::OkStatus();
@@ -454,10 +460,9 @@ void AcsAgentClient::Shutdown() {
454460
}
455461

456462
std::string AcsAgentClient::CreateMessageUuid() {
457-
// TODO: b/376530555 - Make the random number generation thread-safe. The gen_
458-
// is not thread-safe. int64_t random =
459-
// absl::Uniform<int64_t>(gen_, 0, std::numeric_limits<int64_t>::max());
460-
return absl::StrCat(absl::ToUnixMicros(absl::Now()));
463+
int64_t random =
464+
absl::Uniform<int64_t>(gen_, 0, std::numeric_limits<int64_t>::max());
465+
return absl::StrCat(random, "-", absl::ToUnixMicros(absl::Now()));
461466
}
462467

463468
void AcsAgentClient::SetValueAndRemovePromise(const std::string& message_id,

cpp/acs_agent_client.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,13 @@ class AcsAgentClient {
158158
// the restart_client_thread_.
159159
std::unique_ptr<
160160
google::cloud::agentcommunication::v1::AgentCommunication::Stub>
161-
GenerateConnectionIdAndStub();
161+
GenerateConnectionIdAndStub() ABSL_EXCLUSIVE_LOCKS_REQUIRED(reactor_mtx_);
162162

163163
// Creates a unique message id. Currently, it is "{random
164164
// number}-{current_timestamp}". The requirement of the uniqueness from ACS
165165
// server is not very strict: We just need to make sure the message id is
166166
// unique within a couple of seconds for each agent.
167-
std::string CreateMessageUuid();
167+
std::string CreateMessageUuid() ABSL_EXCLUSIVE_LOCKS_REQUIRED(reactor_mtx_);
168168

169169
// Set the value of the promise and remove the promise from the map. This
170170
// function is used to ensure:
@@ -176,17 +176,12 @@ class AcsAgentClient {
176176
absl::Status status)
177177
ABSL_EXCLUSIVE_LOCKS_REQUIRED(request_delivery_status_mtx_);
178178

179-
// TODO: b/376530555 - Make the random number generation thread-safe. The gen_
180-
// is not thread-safe, needs to be protected by a mutex. Random number
181-
// generator for creating message id.
182-
absl::BitGen gen_;
179+
absl::BitGen gen_ ABSL_GUARDED_BY(reactor_mtx_);
183180

184181
// Dedicated to reading the Response from the server.
185182
std::thread read_response_thread_;
186183

187-
// TODO: b/376530555 - Make the connection id thread-safe.
188-
// Connection id of the agent.
189-
AgentConnectionId connection_id_;
184+
AgentConnectionId connection_id_ ABSL_GUARDED_BY(reactor_mtx_);
190185

191186
// Mutex to protect the reactor_ and other variables that are only accessed
192187
// by the reactor_ for write operations.

0 commit comments

Comments
 (0)