title: Getting a lot of `PrematureCloseException : Connection prematurely closed BEFORE response`
body: <p>We are receiving a lot of:</p>
  <pre><code>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)
  </code></pre>
  <p>The client is built like this:</p>
  <pre class="lang-java prettyprint-override"><code>        httpClient = HttpClient.newConnection().compress(true);
          return WebClient.builder()
              .exchangeStrategies(ExchangeStrategies.builder().codecs(configurer -&gt; configurer.defaultCodecs().maxInMemorySize(-1)).build())
              .baseUrl(url)
              .filter(errorHandlingFilter(platformService))
              .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
              .clientConnector(new ReactorClientHttpConnector(httpClient))
              .build();
  </code></pre>
  <p>and the filter is:</p>
  <pre class="lang-java prettyprint-override"><code>    private ExchangeFilterFunction errorHandlingFilter(final PlatformService service) {
          return ExchangeFilterFunction.ofResponseProcessor(clientResponse -&gt; {
              if (!isErrorStatusCode(clientResponse)) {
                  return Mono.just(clientResponse);
              }
  
              final Optional&lt;org.springframework.http.MediaType&gt; mediaType = clientResponse.headers().contentType();
              boolean jsonResponse = true;
  
              if (mediaType.isPresent()) {
                  final org.springframework.http.MediaType mt = mediaType.get();
  
                  if (!mt.getType().equals(&quot;application&quot;) &amp;&amp; !mt.getSubtype().equals(&quot;json&quot;)) {
                      jsonResponse = false;
                  }
              }
  
              if (!clientResponse.statusCode().is5xxServerError()) {
                  return clientResponse
                      .bodyToMono(String.class)
                      .flatMap(s -&gt; Mono.error(new UpstreamException(s, service)));
              }
  
              if (!jsonResponse) {
                  return clientResponse
                      .bodyToMono(String.class)
                      .flatMap(e -&gt; Mono.error(new UpstreamException(e, service)));
              }
  
              return clientResponse
                  .bodyToMono(RuntimeException.class)
                  .flatMap(e -&gt; Mono.error(new UpstreamException(e, service)));
          });
      }
  
  </code></pre>
  <p>Errors are happening only sometimes. We have a lot of services, so its hard to figure out why.</p>
  <p>Any idea?</p>