Spring boot + webflux: context lost when running some steps in parallel

Arnaud Villevieille :

Spring boot: 2.1.3.RELEASE

Hello there,

I am trying to use the context feature of spring webflux to carry around a simple variable. I have a WebFilter setting a context with such variable and I try to use it in my controller at different stages of my flux/stream. At some point, I lose it after calling the method "parallel()" of the Flux class.

  • The filter:
public class TestFilter implements WebFilter {

    private Logger LOG = LoggerFactory.getLogger(TestFilter.class);

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
        return chain.filter(exchange)
            .doOnEach(voidSignal -> System.out.println("filter:"+voidSignal.getContext().getOrEmpty("blob"))).subscriberContext(Context.of("blob", "kapoue"));
    }

}
  • The controller:
@RestController
@RequestMapping(TestControllerWebFlux.ROOT)
public class TestControllerWebFlux {

    static final String ROOT = "/flux";
    static final String TEST = "/test";

    private WebClient webClient = WebClient.create();

    @GetMapping(
            value = TEST,
            produces = {MediaType.APPLICATION_JSON_VALUE})
    public Mono<String> test() {
        System.out.println("controller1:"+Thread.currentThread());

        Flux<String> call = webClient.get().uri("http://localhost:" + 8080 + ROOT + "/test2").retrieve().bodyToFlux(Result.class).map(Result::getValue);

        return call.map(s -> s+"0")
            .doOnEach(stringSignal -> System.out.println("controller2:"+stringSignal.getContext().getOrEmpty("blob")))
            .parallel()
            .doOnEach(stringSignal -> System.out.println("controller3:"+stringSignal.getContext().getOrEmpty("blob")))
            .map(s -> s+"0")
            .doOnEach(stringSignal -> System.out.println("controller4:"+stringSignal.getContext().getOrEmpty("blob")))
            .reduce((s, s2) -> s+s2)
            .doOnEach(stringSignal -> System.out.println("controller5:"+stringSignal.getContext().getOrEmpty("blob")))
            .map(s -> {
                System.out.println("controller6:"+Thread.currentThread());
                return s;
            });
    }

    @GetMapping(
        value = "test2",
        produces = {MediaType.APPLICATION_JSON_VALUE})
    public Flux<Result> test2() {
        return Flux.just(new Result("0"), new Result("0"), new Result("0"));
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class Result {
        private String value;
    }
}

All I do is calling the http://localhost:8080/flux/test/ endpoint and i get that:

controller1:Thread[reactor-http-nio-2,5,main] controller2:Optional[kapoue] controller3:Optional.empty controller4:Optional.empty controller2:Optional[kapoue] controller3:Optional.empty controller4:Optional.empty controller2:Optional[kapoue] controller3:Optional.empty controller4:Optional.empty controller2:Optional[kapoue] controller3:Optional.empty controller4:Optional.empty controller3:Optional.empty controller4:Optional.empty controller3:Optional.empty controller4:Optional.empty controller3:Optional.empty controller4:Optional.empty controller5:Optional[kapoue] controller6:Thread[reactor-http-nio-2,5,main] filter:Optional[kapoue]

As you can see, the context is lost right after the 'parallel' method and somehow comes back after reducing.

Is that a bug or am I not supposed to try to run things in parallel after a call such as this one ?

Thank you in advance for the help.

bsideup :

This looks like a bug in Reactor. I reported it: https://github.com/reactor/reactor-core/issues/1656

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=83642&siteId=1