Tag Archives: reactor.netty.http.client.PrematureCloseException: Connection prematurely closed BEFORE response

[Solved] reactor.netty.http.client.PrematureCloseException: Connection prematurely closed BEFORE response

Jetty service used by backend

report errors

2022-01-11 17:20:24.362  WARN 4784 --- [ctor-http-nio-3] r.netty.http.client.HttpClientConnect    : [id: 0x3eaaea4e, L:0.0.0.0/0.0.0.0:58790 ! R:localhost/127.0.0.1:8090] The connection observed an error

reactor.netty.http.client.PrematureCloseException: Connection prematurely closed BEFORE response

2022-01-11 17:20:24.362 ERROR 4784 --- [ctor-http-nio-3] a.w.r.e.AbstractErrorWebExceptionHandler : [31e53fe7-21772]  500 Server Error for HTTP GET "/status"

reactor.netty.http.client.PrematureCloseException: Connection prematurely closed BEFORE response
	Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 
Error has been observed at the following site(s):
	|_ checkpoint ⇢ org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter [DefaultWebFilterChain]
	|_ checkpoint ⇢ HTTP GET "/status" [ExceptionHandlingWebHandler]
Stack trace:

explain

Call the back-end service jetty through the gateway service spring cloud gateway and report an exception

reason

The back-end service automatically disconnects after the socket timeout, and the gateway service takes the disconnected connection from the httpclient connection pool to request

Reappearance

Spring Cloud GateWay : 2.2.1.RELEASE

Spring Cloud : Hoxton.SR1

Spring Boot : 2.3.4.RELEASE

Server uses springboot emulation

GateWay

spring:
  application:
    name: gateway
  cloud:
    gateway:
      httpclient:
        pool:
          max-idle-time: 60000ms
      routes:
      - id: dev
        uri: ${service-url.dev-service}/status
        predicates:
        - Path=/status
service-url:
  dev-service: http://localhost:8080
server:
  port: 8098

Server

spring:
  application:
    name: dev
server:
  tomcat:
    connection-timeout: 100ms

Jmeter

Threads:300
period seconds:5
Loop Count:20

explain:

Max idle time: maximum idle connection time

Connection timeout: timeout of socket read().

Gateway’s max idle time: the connection is released after 60000ms, and the connection timeout of the server is disconnected after 100ms. At this time, gateway will take the disconnected connection request from the connection pool, resulting in an error

 

Solution:

Because the server is the provider and the gateway is the consumer, try to ensure that the consumer disconnects before the provider, and the time for setting Max idle time is not greater than connection timeout

Jetty services

Jetty modified jetty.http.Idletimeout or http.timeout. Default 30000ms