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

Commit 6d1b6b8

Browse files
authored
Close a race by checking for repeated End() calls in SpanImpl. (#116)
Inline EndWithTime() into its one caller.
1 parent f2b1b83 commit 6d1b6b8

3 files changed

Lines changed: 16 additions & 16 deletions

File tree

opencensus/trace/internal/span.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,10 @@ void Span::SetStatus(StatusCode canonical_code, absl::string_view message) {
202202

203203
void Span::End() {
204204
if (IsRecording()) {
205-
if (span_impl_->HasEnded()) {
206-
assert(false && "Invalid attempt to End() the same Span more than once.");
205+
if (!span_impl_->End()) {
207206
// In non-debug builds, ignore the second End().
208207
return;
209208
}
210-
span_impl_->End();
211209
exporter::RunningSpanStoreImpl::Get()->RemoveSpan(span_impl_);
212210
exporter::LocalSpanStoreImpl::Get()->AddSpan(span_impl_);
213211
exporter::SpanExporterImpl::Get()->AddSpan(span_impl_);

opencensus/trace/internal/span_impl.cc

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,19 +137,21 @@ void SpanImpl::SetStatus(exporter::Status&& status) {
137137
}
138138
}
139139

140-
void SpanImpl::End() { EndWithTime(absl::Now()); }
141-
142-
bool SpanImpl::HasEnded() const {
140+
bool SpanImpl::End() {
143141
absl::MutexLock l(&mu_);
144-
return has_ended_;
142+
if (has_ended_) {
143+
assert(false && "Invalid attempt to End() the same Span more than once.");
144+
// In non-debug builds, just ignore the second End().
145+
return false;
146+
}
147+
has_ended_ = true;
148+
end_time_ = absl::Now();
149+
return true;
145150
}
146151

147-
void SpanImpl::EndWithTime(absl::Time end_time) {
152+
bool SpanImpl::HasEnded() const {
148153
absl::MutexLock l(&mu_);
149-
if (!has_ended_) {
150-
has_ended_ = true;
151-
end_time_ = end_time;
152-
}
154+
return has_ended_;
153155
}
154156

155157
exporter::SpanData SpanImpl::ToSpanData() const {

opencensus/trace/internal/span_impl.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,10 @@ class SpanImpl final {
8585

8686
void SetStatus(exporter::Status&& status) LOCKS_EXCLUDED(mu_);
8787

88-
// Marks the end of the Span and sets its end_time_.
89-
void End() LOCKS_EXCLUDED(mu_);
88+
// Returns true on success (if this is the first time the Span has ended) and
89+
// also marks the end of the Span and sets its end_time_.
90+
bool End() LOCKS_EXCLUDED(mu_);
91+
9092
// Returns true if the span has ended.
9193
bool HasEnded() const LOCKS_EXCLUDED(mu_);
9294

@@ -106,8 +108,6 @@ class SpanImpl final {
106108
friend class ::opencensus::trace::exporter::SpanExporterImpl;
107109
friend class ::opencensus::trace::SpanTestPeer;
108110

109-
void EndWithTime(absl::Time end_time) LOCKS_EXCLUDED(mu_);
110-
111111
// Makes a deep copy of span contents and returns copied data in SpanData.
112112
exporter::SpanData ToSpanData() const LOCKS_EXCLUDED(mu_);
113113

0 commit comments

Comments
 (0)