Skip to content
Closed
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ Release Notes.
9.9.0
------------------

* Fix the gRPC server plugin leaking the trace context on an executor thread when `close` runs after
`onCancel`. Both paths async-finish the same entry span, and the second `asyncFinish` threw before
`stopSpan` (apache/skywalking#14268).
* Fix the `NullPointerException` thrown by the `postgresql-8.x` plugin when executing statements prepared with
`Statement.RETURN_GENERATED_KEYS` or column names (apache/skywalking#14138).
* Fix the `NullPointerException` thrown by the Lettuce plugin and the lost trace context of reactive commands when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

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

import java.util.concurrent.atomic.AtomicBoolean;

import io.grpc.Context;
import io.grpc.Contexts;
import io.grpc.Metadata;
Expand Down Expand Up @@ -55,16 +57,19 @@ public <REQUEST, RESPONSE> ServerCall.Listener<REQUEST> interceptCall(ServerCall
span.setLayer(SpanLayer.RPC_FRAMEWORK);
ContextSnapshot contextSnapshot = ContextManager.capture();
AbstractSpan asyncSpan = span.prepareForAsync();
// onCancel and close can both finish this entry span. Only the first one should call asyncFinish.
final AtomicBoolean entrySpanFinished = new AtomicBoolean();

ServerCall.Listener<REQUEST> listener = Contexts.interceptCall(
Context.current(),
new TracingServerCall<>(call, contextSnapshot, asyncSpan),
new TracingServerCall<>(call, contextSnapshot, asyncSpan, entrySpanFinished),
headers,
(serverCall, metadata) -> new TracingServerCallListener<>(
handler.startCall(serverCall, metadata),
serverCall.getMethodDescriptor(),
contextSnapshot,
asyncSpan
asyncSpan,
entrySpanFinished
)
);
ContextManager.stopSpan(asyncSpan);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

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

import java.util.concurrent.atomic.AtomicBoolean;

import io.grpc.ForwardingServerCall;
import io.grpc.Metadata;
import io.grpc.ServerCall;
Expand All @@ -39,14 +41,27 @@ public class TracingServerCall<REQUEST, RESPONSE> extends ForwardingServerCall.S
private final String operationPrefix;
private final ContextSnapshot contextSnapshot;
private final AbstractSpan parentEntrySpan;
private final AtomicBoolean entrySpanFinished;

protected TracingServerCall(ServerCall<REQUEST, RESPONSE> delegate,
final ContextSnapshot contextSnapshot,
final AbstractSpan parentEntrySpan) {
final AbstractSpan parentEntrySpan,
final AtomicBoolean entrySpanFinished) {
super(delegate);
this.operationPrefix = OperationNameFormatUtil.formatOperationName(delegate.getMethodDescriptor()) + SERVER;
this.contextSnapshot = contextSnapshot;
this.parentEntrySpan = parentEntrySpan;
this.entrySpanFinished = entrySpanFinished;
}

/**
* {@code onCancel} and {@code close} can both finish the async entry span. The second
* {@link AbstractSpan#asyncFinish()} throws and, in {@code close}, used to skip {@code stopSpan}.
*/
static void finishParentEntrySpan(AbstractSpan parentEntrySpan, AtomicBoolean entrySpanFinished) {
if (entrySpanFinished.compareAndSet(false, true)) {
parentEntrySpan.asyncFinish();
}
}

@Override
Expand Down Expand Up @@ -102,9 +117,9 @@ public void close(Status status, Metadata trailers) {
break;
}
Tags.RPC_RESPONSE_STATUS_CODE.set(span, status.getCode().name());
parentEntrySpan.asyncFinish();

try {
finishParentEntrySpan(parentEntrySpan, entrySpanFinished);
super.close(status, trailers);
} catch (Throwable t) {
ContextManager.activeSpan().log(t);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

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

import java.util.concurrent.atomic.AtomicBoolean;

import io.grpc.ForwardingServerCallListener;
import io.grpc.MethodDescriptor;
import io.grpc.ServerCall;
Expand All @@ -37,15 +39,18 @@ public class TracingServerCallListener<REQUEST> extends ForwardingServerCallList
private final String operationPrefix;
private final ContextSnapshot contextSnapshot;
private final AbstractSpan parentEntrySpan;
private final AtomicBoolean entrySpanFinished;

protected TracingServerCallListener(ServerCall.Listener<REQUEST> delegate, MethodDescriptor<REQUEST, ?> descriptor,
final ContextSnapshot contextSnapshot,
final AbstractSpan parentEntrySpan) {
final AbstractSpan parentEntrySpan,
final AtomicBoolean entrySpanFinished) {
super(delegate);
this.methodType = descriptor.getType();
this.operationPrefix = OperationNameFormatUtil.formatOperationName(descriptor) + SERVER;
this.contextSnapshot = contextSnapshot;
this.parentEntrySpan = parentEntrySpan;
this.entrySpanFinished = entrySpanFinished;
}

@Override
Expand Down Expand Up @@ -82,7 +87,7 @@ public void onCancel() {
throw t;
} finally {
ContextManager.stopSpan();
parentEntrySpan.asyncFinish();
TracingServerCall.finishParentEntrySpan(parentEntrySpan, entrySpanFinished);
}
}

Expand Down