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

Conversation

isaachier
Copy link
Contributor

Signed-off-by: Isaac Hier isaachier@gmail.com

Signed-off-by: Isaac Hier <isaachier@gmail.com>
@codecov
Copy link

codecov bot commented Apr 15, 2018

Codecov Report

Merging #83 into master will increase coverage by 0.03%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #83      +/-   ##
==========================================
+ Coverage   88.52%   88.55%   +0.03%     
==========================================
  Files          96       96              
  Lines        2291     2298       +7     
==========================================
+ Hits         2028     2035       +7     
  Misses        263      263
Impacted Files Coverage Δ
src/jaegertracing/SpanContext.h 98.46% <100%> (+0.18%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update bcd3d3f...dab24b7. Read the comment docs.

@isaachier
Copy link
Contributor Author

@rnburn could you please take a look at this and see if it looks OK? @yurishkuro this is the change for C++ client to lock baggage in span context to avoid thread-unsafe return-by-reference semantics in OpenTracing C++ (opentracing/opentracing-cpp#74).

@isaachier
Copy link
Contributor Author

@black-adder or @vprithvi do you mind glancing over this? The background isn't so important just I had to make SpanContext protect baggage with a mutex.

@@ -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.

@isaachier isaachier merged commit df7c373 into jaegertracing:master Apr 18, 2018
@isaachier isaachier deleted the span-context-race-fix branch April 18, 2018 00:32
@isaachier
Copy link
Contributor Author

Thanks @black-adder

# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants