4

We are receiving a lot of:

reactor.core.ReactiveException: reactor.netty.http.client.PrematureCloseException: Connection prematurely closed BEFORE response
    at reactor.core.Exceptions.propagate(Exceptions.java:393)
    at reactor.core.publisher.BlockingSingleSubscriber.blockingGet(BlockingSingleSubscriber.java:97)
    at reactor.core.publisher.Mono.block(Mono.java:1678)

The client is built like this:

        httpClient = HttpClient.newConnection().compress(true);
        return WebClient.builder()
            .exchangeStrategies(ExchangeStrategies.builder().codecs(configurer -> configurer.defaultCodecs().maxInMemorySize(-1)).build())
            .baseUrl(url)
            .filter(errorHandlingFilter(platformService))
            .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
            .clientConnector(new ReactorClientHttpConnector(httpClient))
            .build();

and the filter is:

    private ExchangeFilterFunction errorHandlingFilter(final PlatformService service) {
        return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> {
            if (!isErrorStatusCode(clientResponse)) {
                return Mono.just(clientResponse);
            }

            final Optional<org.springframework.http.MediaType> mediaType = clientResponse.headers().contentType();
            boolean jsonResponse = true;

            if (mediaType.isPresent()) {
                final org.springframework.http.MediaType mt = mediaType.get();

                if (!mt.getType().equals("application") && !mt.getSubtype().equals("json")) {
                    jsonResponse = false;
                }
            }

            if (!clientResponse.statusCode().is5xxServerError()) {
                return clientResponse
                    .bodyToMono(String.class)
                    .flatMap(s -> Mono.error(new UpstreamException(s, service)));
            }

            if (!jsonResponse) {
                return clientResponse
                    .bodyToMono(String.class)
                    .flatMap(e -> Mono.error(new UpstreamException(e, service)));
            }

            return clientResponse
                .bodyToMono(RuntimeException.class)
                .flatMap(e -> Mono.error(new UpstreamException(e, service)));
        });
    }

Errors are happening only sometimes. We have a lot of services, so its hard to figure out why.

Any idea?

igr
  • 10,199
  • 13
  • 65
  • 111
  • Have you checked this thread: https://github.com/reactor/reactor-netty/issues/796 ? Looks like although the issue is closed, users are still facing the same problem.. – Abhinaba Chakraborty Jul 10 '20 at 16:51
  • @AbhinabaChakraborty yes, I think I read all I could find:))... but still a mistery to me how to fix. – igr Jul 10 '20 at 19:18
  • Which version of the dependencies you are using? Please ensure that you are using latest version (probably it is fixed ). Also once try increasing the webclient timeout.. – Abhinaba Chakraborty Jul 11 '20 at 11:51
  • @AbhinabaChakraborty almost latest (4.1.49). Will do – igr Jul 13 '20 at 10:42
  • please do and post your observations here. – Abhinaba Chakraborty Jul 13 '20 at 10:55
  • @AbhinabaChakraborty Nothing that I could conclude: https://github.com/reactor/reactor-netty/issues/1217 – igr Jul 20 '20 at 21:33
  • 1
    I had the same issue with reactor-netty 0.9.5 and could not figure out why it was happening. The workaround I used is to define a Retry policy for when PrematureCloseException happens. So it still happens sometimes, but after a retry it works ok. – Tara Delari Jul 29 '20 at 12:48
  • Yes @TaraDelari we are trying to do the same. Actually, we switched to RestTemplate, and now we are getting 504, but much less, I would say. Thanx! – igr Jul 30 '20 at 20:56

1 Answers1

0

We were not able to conclude much in my case. https://github.com/reactor/reactor-netty/issues/1217

igr
  • 10,199
  • 13
  • 65
  • 111