Skip to content

[Bug] after apache/skywalking-java#457, cross-thread grpc streaming is broken. #12901

@h0cheung

Description

@h0cheung

Search before asking

  • I had searched in the issues and found no similar issues.

Apache SkyWalking Component

Java Agent (apache/skywalking-java)

What happened

If StreamObserver#onNext is called in another thread, we will see exception: IllegalArgumentException("ContextSnapshot can't be null.")

After apache/skywalking-java#457, we use a io.grpc.Context to pass the ContextSnapshot. But by default, io.grpc.Context uses ThreadLocal as storage. So it won't work across threads. Then the contextSnapshot will be null, which causes the exception above.

I think we should partially revert the PR. Using the constructor to pass contexts is good.

What you expected to happen

It should work normally

How to reproduce

the simplest way is write a handler like this:

    public StreamObserver<Request> connect(StreamObserver<Response> responseObserver) {
        new Thread(()-> {
            responseObserver.onNext(Response.newBuilder().build());
        }).start();
        return new StreamObserver<>() {
            @Override
            public void onNext(Request value) {
                
            }

            @Override
            public void onError(Throwable t) {

            }

            @Override
            public void onCompleted() {

            }
        }

Then call it with a client, such as grpcurl.

Anything else

No response

Are you willing to submit a pull request to fix on your own?

  • Yes I am willing to submit a pull request on my own!

Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    agentLanguage agent related.bugSomething isn't working and you are sure it's a bug!enhancementEnhancement on performance or codesjavaJava agent related

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions