Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit 7063ccb

Browse files
authored
SpanImpl::name() has to grab a lock and copy the name. (#369)
Because SetName() could change the name at any time. This isn't a bug yet because only running_span_store calls into name(), in a code path we're not using yet. This error can be caught by -Wthread-safety-reference with clang-8 and later.
1 parent 36d8b0d commit 7063ccb

5 files changed

Lines changed: 11 additions & 7 deletions

File tree

opencensus/trace/internal/running_span_store_impl.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ RunningSpanStore::Summary RunningSpanStoreImpl::GetSummary() const {
6767
RunningSpanStore::Summary summary;
6868
absl::MutexLock l(&mu_);
6969
for (const auto& addr_span : spans_) {
70-
const std::string& name = addr_span.second->name_constref();
70+
const std::string name = addr_span.second->name();
7171
auto it = summary.per_span_name_summary.find(name);
7272
if (it != summary.per_span_name_summary.end()) {
7373
it->second.num_running_spans++;

opencensus/trace/internal/span_impl.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,11 @@ bool SpanImpl::HasEnded() const {
161161
return has_ended_;
162162
}
163163

164+
std::string SpanImpl::name() const {
165+
absl::MutexLock l(&mu_);
166+
return name_;
167+
}
168+
164169
exporter::SpanData SpanImpl::ToSpanData() const {
165170
absl::MutexLock l(&mu_);
166171
// Make a deep copy of attributes.

opencensus/trace/internal/span_impl.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,9 @@ class SpanImpl final {
9494
// Returns true if the span has ended.
9595
bool HasEnded() const LOCKS_EXCLUDED(mu_);
9696

97-
absl::string_view name() const { return name_; }
98-
99-
// Returns the name of the span as a constref string.
100-
const std::string& name_constref() const { return name_; }
97+
// Returns a copy of the current name of the Span, since SetName can be used
98+
// to change it.
99+
std::string name() const LOCKS_EXCLUDED(mu_);
101100

102101
// Returns the SpanContext associated with this Span.
103102
SpanContext context() const { return context_; }

opencensus/trace/internal/span_options_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class SpanTestPeer {
4141
return span->span_impl_for_test()->status_;
4242
}
4343

44-
static absl::string_view GetName(Span* span) {
44+
static std::string GetName(Span* span) {
4545
return span->span_impl_for_test()->name();
4646
}
4747

tools/travis/build_bazel.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export BAZEL_OPTIONS="$BAZEL_OPTIONS --experimental_ui_actions_shown=1"
2424

2525
# Enable thread safety analysis (only works with clang).
2626
if [[ "$TRAVIS_COMPILER" = "clang" ]]; then
27-
export BAZEL_OPTIONS="$BAZEL_OPTIONS --copt=-Werror=thread-safety"
27+
export BAZEL_OPTIONS="$BAZEL_OPTIONS --copt=-Werror=thread-safety --copt=-Werror=thread-safety-reference"
2828
fi
2929

3030
export BAZEL_VERSION="0.24.1"

0 commit comments

Comments
 (0)