spring-cloud / spring-cloud/spring-cloud-gateway

Error when using ModifyRequestBody and Retry filters

Open
#3,775 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feedback-provided
Dominant language
Java
Stars
4.9k
Forks
3.5k
Avg merge
20h 57m
Merged PRs (30d)
8

Description

Describe the bug

Using the ModifyRequestBody and Retry filters causes an IllegalReferenceCountException on technical error of the target (e.g. premature closed error):

io.netty.util.IllegalReferenceCountException: refCnt: 0, decrement: 1

I witnessed this on 2.7 & 3.4 versions.

Sample

Using this code, creating a route that uses the ModifyRequestBody and Retry filters, it will trigger the exception:

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
       SpringApplication.run(DemoApplication.class, args);
    }

    @Bean
    public RouteLocator rewrittenAndRetriedRoute(RouteLocatorBuilder builder) {
        return builder.routes()
            .route(p -> p
                .path("/route/*")
                .filters(f -> f
                    .modifyRequestBody(config -> {
                        config.setInClass(String.class);
                        config.setOutClass(String.class);
                        config.setRewriteFunction((exchange, body) -> Mono.just("{}"));
                    })
                    .retry(config -> {
                        config.setRetries(5);
                        config.setSeries();
                        config.setStatuses(HttpStatus.BAD_GATEWAY, HttpStatus.GATEWAY_TIMEOUT);
                        config.setExceptions(IOException.class);
                        config.setMethods(HttpMethod.GET, HttpMethod.POST, HttpMethod.PUT, HttpMethod.DELETE, HttpMethod.PATCH);
                        config.setBackoff(new RetryGatewayFilterFactory.BackoffConfig(Duration.ofMillis(5), Duration.ofMillis(10), 2, false));
                    })
                )
                .uri("http://localhost:9880/"))
            .build();
    }
}

I am using this simple NodeJS server to reproduce a technical error:

const net = require("net");
const server = net.createServer(function(socket) {
    console.log('opening connection');
    setTimeout(function() { socket.destroy(); }, 20);
});
server.listen(9880);

And curl:

$ curl localhost:8080/route/fail -XPOST -H 'Content-Type: application/json' -d '{"Hello": "World"}'

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the ModifyRequestBody and Retry filter implementations and reproduce the failure using the supplied RouteLocator configuration, Node.js server, and curl command. Trace the request body through a premature target-connection failure and retries; done means the reproduction no longer raises IllegalReferenceCountException while retries still operate as configured.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, node.js, spring, spring-boot
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.