Skip to content
This repository has been archived by the owner on Aug 30, 2022. It is now read-only.

Add lock to SpanContext to protect baggage #83

Merged
merged 1 commit into from
Apr 18, 2018
Merged
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
17 changes: 16 additions & 1 deletion src/jaegertracing/SpanContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <iomanip>
#include <iostream>
#include <mutex>
#include <string>
#include <unordered_map>

Expand All @@ -41,6 +42,7 @@ class SpanContext : public opentracing::SpanContext {
, _spanID(0)
, _parentID(0)
, _flags(0)
, _mutex()
{
}

Expand All @@ -56,6 +58,7 @@ class SpanContext : public opentracing::SpanContext {
, _flags(flags)
, _baggage(baggage)
, _debugID(debugID)
, _mutex()
{
}

Expand Down Expand Up @@ -98,13 +101,15 @@ class SpanContext : public opentracing::SpanContext {

SpanContext withBaggage(const StrMap& baggage) const
{
std::lock_guard<std::mutex> lock(_mutex);
return SpanContext(
_traceID, _spanID, _parentID, _flags, baggage, _debugID);
}

template <typename Function>
void forEachBaggageItem(Function f) const
{
std::lock_guard<std::mutex> lock(_mutex);
for (auto&& pair : _baggage) {
if (!f(pair.first, pair.second)) {
break;
Expand All @@ -115,6 +120,7 @@ class SpanContext : public opentracing::SpanContext {
template <typename Function>
void forEachBaggageItem(Function f)
{
std::lock_guard<std::mutex> lock(_mutex);
for (auto&& pair : _baggage) {
if (!f(pair.first, pair.second)) {
break;
Expand Down Expand Up @@ -160,9 +166,17 @@ class SpanContext : public opentracing::SpanContext {

bool operator==(const SpanContext& rhs) const
{
{
std::lock(_mutex, rhs._mutex);
std::lock_guard<std::mutex> lock(_mutex, std::adopt_lock);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this deadlock? this grabs its own lock and rhs grabs its own lock and then we're stuck?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was hoping you wouldn't pay too much attention to this. std::lock is a cool trick in C++11 that locks two mutexes without causing deadlock (essentially a loop of try_lock under the hood):

Locks the given Lockable objects lock1, lock2, ..., lockn using a deadlock avoidance algorithm to avoid deadlock.
http://en.cppreference.com/w/cpp/thread/lock

The lock guard stuff is just to make sure we unlock, doesn't acquire the lock again. Serves same purpose as defer mutex.Unlock() would in Go.

std::lock_guard<std::mutex> rhsLock(rhs._mutex, std::adopt_lock);
if (_baggage != rhs._baggage) {
return false;
}
}
return _traceID == rhs._traceID && _spanID == rhs._spanID &&
_parentID == rhs._parentID && _flags == rhs._flags &&
_baggage == rhs._baggage && _debugID == rhs._debugID;
_debugID == rhs._debugID;
}

private:
Expand All @@ -172,6 +186,7 @@ class SpanContext : public opentracing::SpanContext {
unsigned char _flags;
StrMap _baggage;
std::string _debugID;
mutable std::mutex _mutex; // Protects _baggage.
};

} // namespace jaegertracing
Expand Down