Skip to content

Fix gRPC server context leak when close follows onCancel - #850

Closed
ElliotXue-crypto wants to merge 1 commit into
apache:mainfrom
ElliotXue-crypto:fix-grpc-close-context-leak
Closed

ElliotXue-crypto wants to merge 1 commit into
apache:mainfrom
ElliotXue-crypto:fix-grpc-close-context-leak

Conversation

@ElliotXue-crypto

Copy link
Copy Markdown

Summary

Fixes apache/skywalking#14268.

TracingServerCall.close called parentEntrySpan.asyncFinish() before the try/finally that stops the local span. onCancel finishes that same entry span. The second asyncFinish() throws Can not do async finish for the span repeatedly., so stopSpan() never ran and the grpc-default-executor thread kept the trace context. TraceSegment.relatedGlobalTrace does not replace an id that is already propagated, so every later RPC on that thread was logged with the original trace id.

onCancel and close now share a one-shot flag and async-finish the entry span only once. close finishes it inside the try, so stopSpan() still runs and super.close() still runs.

Test plan

  • Client cancels an in-flight unary call, the server still closes the call, and the executor thread's next RPC logs a new trace id
  • A normal call that is only closed (no cancel) still finishes the entry span once
  • A call that is only cancelled still finishes the entry span once
  • onCancel and close racing on two threads finish the entry span only once and do not throw

The grpc-1.x plugin has no unit test for TracingServerCall. I did not run the full agent build.

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: Cursor <cursoragent@cursor.com>
@ElliotXue-crypto

Copy link
Copy Markdown
Author

Closing this without a code change. The patch sits on every gRPC server close and the grpc-1.x plugin has no test for TracingServerCall, so it is riskier than an untested agent change should be. The bug report remains at apache/skywalking#14268.

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.

[Bug] gRPC server plugin leaks the trace context when close runs after onCancel

1 participant