Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[trace] fix race when child and parent send is called at the same time #40

Merged
merged 2 commits into from
Nov 28, 2018

Conversation

tredman
Copy link
Contributor

@tredman tredman commented Nov 23, 2018

Fixes #38 - if Send is called for a span and its parent at the same time, a race can occur when accesing "isSent". This adds a lock to Send, which sets isSent and protects reads with read locks. This also effectively prevents Send from executing multiple times for the same span.

`CreateChild` adds a new span to the current span's list of children. `CreateAsyncChild` calls `CreateChild`, then sets the isAsync bool on the span. This can lead to a race caused by simultaneous read of the child span's `isAsync` bool by `Send` as it's being written by `CreateAsyncChild`. To fix this, I refactored  the code a bit to allow us to set `isAsync` before the span is added to the list of child spans.

This doesn't seem to be the same race reported in #38 but it should be fixed either way.
Fixes #38  - if `Send` is called for a span and its parent at the same time, a race can occur when accesing "isSent". This adds a lock to `Send`, which sets `isSent` and protects reads with read locks. This also effectively prevents `Send` from executing multiple times for the same span.
Copy link
Member

@martin308 martin308 left a comment

Choose a reason for hiding this comment

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

Seems to make sense to me. I have one question about the meta.sent_by_parent field though

child.sendLock.RLock()
isChildSent := child.isSent
child.sendLock.RUnlock()
if !isChildSent {
child.AddField("meta.sent_by_parent", true)
Copy link
Member

Choose a reason for hiding this comment

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

it seems like it could still be possible for this field to be set on a child which was not sent by a parent? I'm not sure how important that is if it possible though 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah this is not perfect, but holding the lock here will deadlock the Send method. I think it might require a new method that sets the field while under the send lock.

@tredman tredman merged commit 4c184f7 into master Nov 28, 2018
@tredman tredman deleted the tredman.async_span_race_2 branch November 28, 2018 21:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants