reactive-grpc: [reactor-grpc] Reactor context does not propagate if stub is used

Description

Whenever a stub is used in a chain together with Reactor’s Context API the provided context does not propagate correctly. My guess is, that using subscribe() within ClientCalls is preventing the context from propagating up the chain.

Example

package com.salesforce.reactorgrpc;

import com.salesforce.grpc.testing.contrib.NettyGrpcServerRule;
import org.junit.Rule;
import org.junit.Test;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import reactor.util.context.Context;

import java.util.stream.Collectors;

import static org.assertj.core.api.Assertions.assertThat;

public class ReactorContextPropagationTest {

    @Rule
    public NettyGrpcServerRule serverRule = new NettyGrpcServerRule();

    private static class SimpleGreeter extends ReactorGreeterGrpc.GreeterImplBase {
        @Override
        public Mono<HelloResponse> sayHello(Mono<HelloRequest> request) {
            return request.map(HelloRequest::getName)
                    .map(name -> HelloResponse.newBuilder().setMessage("Hello " + name).build());
        }

        @Override
        public Mono<HelloResponse> sayHelloReqStream(Flux<HelloRequest> request) {
            return request.map(HelloRequest::getName)
                    .collect(Collectors.joining("and"))
                    .map(names -> HelloResponse.newBuilder().setMessage("Hello " + names).build());
        }
    }

    @Test
    public void contextDoesNotPropagate() {
        serverRule.getServiceRegistry().addService(new SimpleGreeter());

        ReactorGreeterGrpc.ReactorGreeterStub stub = ReactorGreeterGrpc.newReactorStub(serverRule.getChannel());
        Mono<HelloRequest> req = Mono.just(HelloRequest.newBuilder().setName("reactor").build());

        Mono<HelloResponse> resp = req
                // This assertion will fail 
                .doOnEach(signal -> assertThat(signal.getContext().getOrEmpty("key")).isNotEmpty())
                .compose(stub::sayHello)
                // This assertion won't fail
                .doOnEach(signal -> assertThat(signal.getContext().getOrEmpty("key")).isNotEmpty())
                .subscriberContext(Context.of("key", "dummy"));

        StepVerifier.create(resp)
                .expectNextMatches(response -> response.getMessage().equals("Hello reactor"))
                .verifyComplete();
    }
}

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Comments: 18 (5 by maintainers)

Most upvoted comments

Hello, we’re having the same issue here. Activating enableContextLossTracking() show that the context was lost at:

.transform(Operators.lift(new SubscribeOnlyOnceLifter<TResponse>()));
	at reactor.core.publisher.ContextTrackingFunctionWrapper.lambda$apply$0(ContextTrackingFunctionWrapper.java:50)
	Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 
Assembly trace from producer [reactor.core.publisher.MonoLift] :
	reactor.core.publisher.Mono.transform(Mono.java:4705)
	com.salesforce.reactorgrpc.stub.ClientCalls.oneToOne(ClientCalls.java:57)
Error has been observed at the following site(s):
	|_     Mono.transform ⇢ at com.salesforce.reactorgrpc.stub.ClientCalls.oneToOne(ClientCalls.java:57)

@rmichela Also reactor now supports ThreadLocal propagation magic you can use that aswell to read the values in from ThreadLocal in grpc layer.