Skip to content

Commit 636ae68

Browse files
ElliotXue-cryptoxuexuxucursoragent
authored
Fix gRPC server context leak when close follows onCancel (#850)
onCancel and close both async-finish the entry span. The second asyncFinish threw before stopSpan, so the executor thread kept that trace id for every later RPC. Co-authored-by: xuexuxu <xuexuxu@tp-link.com.hk> Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 6840d74 commit 636ae68

4 files changed

Lines changed: 34 additions & 6 deletions

File tree

‎CHANGES.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ Release Notes.
55
9.9.0
66
------------------
77

8+
* Fix the gRPC server plugin leaking the trace context on an executor thread when `close` runs after
9+
`onCancel`. Both paths async-finish the same entry span, and the second `asyncFinish` threw before
10+
`stopSpan` (apache/skywalking#14268).
811
* Fix the `NullPointerException` thrown by the `postgresql-8.x` plugin when executing statements prepared with
912
`Statement.RETURN_GENERATED_KEYS` or column names (apache/skywalking#14138).
1013
* Fix the `NullPointerException` thrown by the Lettuce plugin and the lost trace context of reactive commands when

‎apm-sniffer/apm-sdk-plugin/grpc-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/grpc/v1/server/ServerInterceptor.java‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
package org.apache.skywalking.apm.plugin.grpc.v1.server;
2020

21+
import java.util.concurrent.atomic.AtomicBoolean;
22+
2123
import io.grpc.Context;
2224
import io.grpc.Contexts;
2325
import io.grpc.Metadata;
@@ -55,16 +57,19 @@ public <REQUEST, RESPONSE> ServerCall.Listener<REQUEST> interceptCall(ServerCall
5557
span.setLayer(SpanLayer.RPC_FRAMEWORK);
5658
ContextSnapshot contextSnapshot = ContextManager.capture();
5759
AbstractSpan asyncSpan = span.prepareForAsync();
60+
// onCancel and close can both finish this entry span. Only the first one should call asyncFinish.
61+
final AtomicBoolean entrySpanFinished = new AtomicBoolean();
5862

5963
ServerCall.Listener<REQUEST> listener = Contexts.interceptCall(
6064
Context.current(),
61-
new TracingServerCall<>(call, contextSnapshot, asyncSpan),
65+
new TracingServerCall<>(call, contextSnapshot, asyncSpan, entrySpanFinished),
6266
headers,
6367
(serverCall, metadata) -> new TracingServerCallListener<>(
6468
handler.startCall(serverCall, metadata),
6569
serverCall.getMethodDescriptor(),
6670
contextSnapshot,
67-
asyncSpan
71+
asyncSpan,
72+
entrySpanFinished
6873
)
6974
);
7075
ContextManager.stopSpan(asyncSpan);

‎apm-sniffer/apm-sdk-plugin/grpc-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/grpc/v1/server/TracingServerCall.java‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
package org.apache.skywalking.apm.plugin.grpc.v1.server;
2020

21+
import java.util.concurrent.atomic.AtomicBoolean;
22+
2123
import io.grpc.ForwardingServerCall;
2224
import io.grpc.Metadata;
2325
import io.grpc.ServerCall;
@@ -39,14 +41,27 @@ public class TracingServerCall<REQUEST, RESPONSE> extends ForwardingServerCall.S
3941
private final String operationPrefix;
4042
private final ContextSnapshot contextSnapshot;
4143
private final AbstractSpan parentEntrySpan;
44+
private final AtomicBoolean entrySpanFinished;
4245

4346
protected TracingServerCall(ServerCall<REQUEST, RESPONSE> delegate,
4447
final ContextSnapshot contextSnapshot,
45-
final AbstractSpan parentEntrySpan) {
48+
final AbstractSpan parentEntrySpan,
49+
final AtomicBoolean entrySpanFinished) {
4650
super(delegate);
4751
this.operationPrefix = OperationNameFormatUtil.formatOperationName(delegate.getMethodDescriptor()) + SERVER;
4852
this.contextSnapshot = contextSnapshot;
4953
this.parentEntrySpan = parentEntrySpan;
54+
this.entrySpanFinished = entrySpanFinished;
55+
}
56+
57+
/**
58+
* {@code onCancel} and {@code close} can both finish the async entry span. The second
59+
* {@link AbstractSpan#asyncFinish()} throws and, in {@code close}, used to skip {@code stopSpan}.
60+
*/
61+
static void finishParentEntrySpan(AbstractSpan parentEntrySpan, AtomicBoolean entrySpanFinished) {
62+
if (entrySpanFinished.compareAndSet(false, true)) {
63+
parentEntrySpan.asyncFinish();
64+
}
5065
}
5166

5267
@Override
@@ -102,9 +117,9 @@ public void close(Status status, Metadata trailers) {
102117
break;
103118
}
104119
Tags.RPC_RESPONSE_STATUS_CODE.set(span, status.getCode().name());
105-
parentEntrySpan.asyncFinish();
106120

107121
try {
122+
finishParentEntrySpan(parentEntrySpan, entrySpanFinished);
108123
super.close(status, trailers);
109124
} catch (Throwable t) {
110125
ContextManager.activeSpan().log(t);

‎apm-sniffer/apm-sdk-plugin/grpc-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/grpc/v1/server/TracingServerCallListener.java‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
package org.apache.skywalking.apm.plugin.grpc.v1.server;
2020

21+
import java.util.concurrent.atomic.AtomicBoolean;
22+
2123
import io.grpc.ForwardingServerCallListener;
2224
import io.grpc.MethodDescriptor;
2325
import io.grpc.ServerCall;
@@ -37,15 +39,18 @@ public class TracingServerCallListener<REQUEST> extends ForwardingServerCallList
3739
private final String operationPrefix;
3840
private final ContextSnapshot contextSnapshot;
3941
private final AbstractSpan parentEntrySpan;
42+
private final AtomicBoolean entrySpanFinished;
4043

4144
protected TracingServerCallListener(ServerCall.Listener<REQUEST> delegate, MethodDescriptor<REQUEST, ?> descriptor,
4245
final ContextSnapshot contextSnapshot,
43-
final AbstractSpan parentEntrySpan) {
46+
final AbstractSpan parentEntrySpan,
47+
final AtomicBoolean entrySpanFinished) {
4448
super(delegate);
4549
this.methodType = descriptor.getType();
4650
this.operationPrefix = OperationNameFormatUtil.formatOperationName(descriptor) + SERVER;
4751
this.contextSnapshot = contextSnapshot;
4852
this.parentEntrySpan = parentEntrySpan;
53+
this.entrySpanFinished = entrySpanFinished;
4954
}
5055

5156
@Override
@@ -82,7 +87,7 @@ public void onCancel() {
8287
throw t;
8388
} finally {
8489
ContextManager.stopSpan();
85-
parentEntrySpan.asyncFinish();
90+
TracingServerCall.finishParentEntrySpan(parentEntrySpan, entrySpanFinished);
8691
}
8792
}
8893

0 commit comments

Comments
 (0)