Skip to content

Commit 0e2855d

Browse files
authored
Ensure scope is accessed sync (#29)
1 parent ba0d91f commit 0e2855d

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

src/OpenTracing.Contrib.Grpc/GrpcTraceLogger.cs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,16 @@ internal class GrpcTraceLogger<TRequest, TResponse>
1515
private IScope _scope;
1616
private bool _isFinished;
1717

18-
private ISpan ScopeSpan => _scope?.Span ?? _span;
18+
private ISpan ScopeSpan
19+
{
20+
get
21+
{
22+
lock (this)
23+
{
24+
return _scope?.Span ?? _span;
25+
}
26+
}
27+
}
1928

2029
public GrpcTraceLogger(ISpan span, TracingConfiguration configuration)
2130
{
@@ -55,11 +64,14 @@ public void BeginOutputScope(string operationName)
5564

5665
private void BeginScope(string operationName)
5766
{
58-
if (_scope != null) EndScope();
67+
lock (this)
68+
{
69+
if (_scope != null) EndScope();
5970

60-
_scope = _configuration.Tracer.BuildSpan(operationName)
61-
.AsChildOf(_span.Context)
62-
.StartActive(false);
71+
_scope = _configuration.Tracer.BuildSpan(operationName)
72+
.AsChildOf(_span.Context)
73+
.StartActive(false);
74+
}
6375
}
6476

6577
public void EndInputScope()
@@ -78,11 +90,14 @@ public void EndOutputScope()
7890

7991
private void EndScope()
8092
{
81-
if (_scope == null) return;
93+
lock (this)
94+
{
95+
if (_scope == null) return;
8296

83-
_scope.Span.Finish();
84-
_scope.Dispose();
85-
_scope = null;
97+
_scope.Span.Finish();
98+
_scope.Dispose();
99+
_scope = null;
100+
}
86101
}
87102

88103
public void Request(TRequest req)

0 commit comments

Comments
 (0)