Affects: Spring Framework 6.1.1


Using RestClient in a multithread context (e.g.: a REST Controller) with a request interceptor or request initializer cause a ConcurrentModificationException.

In the following example, try to uncomment the requestInterceptor or requestInitializer method call to get the exception.

package com.example.restclient;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestClient;

import java.util.stream.IntStream;

@Slf4j
@RestController
@SpringBootApplication
public class RestClientApplication implements ApplicationRunner {

    @Autowired
    RestClient.Builder builder;

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

    @GetMapping("/test/{value}")
    String hello(@PathVariable String value) {
        return "Hello " + value;
    }

    @Override
    public void run(ApplicationArguments args) {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);
        executor.initialize();

        IntStream.rangeClosed(0, 200).forEach(value -> executor.execute(() -> {
            try {
                builder
                        //.requestInterceptor((request, body, execution) -> execution.execute(request, body))
                        //.requestInitializer(request -> {})
                        .baseUrl("http://localhost:8080")
                        .build()
                        .get()
                        .uri("/test/" + value)
                        .retrieve()
                        .body(String.class);
            } catch (Exception exception) {
                log.error(String.valueOf(value), exception);
            }
        }));
    }
}

With requestInterceptor:

java.util.ConcurrentModificationException: null
    at java.base/java.util.ArrayList$Itr.checkForComodification(ArrayList.java:1095) ~[na:na]
    at java.base/java.util.ArrayList$Itr.next(ArrayList.java:1049) ~[na:na]
    at org.springframework.http.client.InterceptingClientHttpRequest$InterceptingRequestExecution.execute(InterceptingClientHttpRequest.java:86) ~[spring-web-6.1.1.jar:6.1.1]
    at com.example.restclient.RestClientApplication.lambda$run$0(RestClientApplication.java:43) ~[classes/:na]
    at org.springframework.http.client.InterceptingClientHttpRequest$InterceptingRequestExecution.execute(InterceptingClientHttpRequest.java:87) ~[spring-web-6.1.1.jar:6.1.1]
    at org.springframework.http.client.InterceptingClientHttpRequest.executeInternal(InterceptingClientHttpRequest.java:71) ~[spring-web-6.1.1.jar:6.1.1]
    at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48) ~[spring-web-6.1.1.jar:6.1.1]
    at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:66) ~[spring-web-6.1.1.jar:6.1.1]
    at org.springframework.web.client.DefaultRestClient$DefaultRequestBodyUriSpec.exchangeInternal(DefaultRestClient.java:453) ~[spring-web-6.1.1.jar:6.1.1]
    at org.springframework.web.client.DefaultRestClient$DefaultRequestBodyUriSpec.retrieve(DefaultRestClient.java:424) ~[spring-web-6.1.1.jar:6.1.1]
    at com.example.restclient.RestClientApplication.lambda$run$1(RestClientApplication.java:49) ~[classes/:na]
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144) ~[na:na]
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642) ~[na:na]
    at java.base/java.lang.Thread.run(Thread.java:1583) ~[na:na]

With requestInitializer:

java.util.ConcurrentModificationException: null
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1598) ~[na:na]
    at org.springframework.web.client.DefaultRestClient$DefaultRequestBodyUriSpec.createRequest(DefaultRestClient.java:515) ~[spring-web-6.1.1.jar:6.1.1]
    at org.springframework.web.client.DefaultRestClient$DefaultRequestBodyUriSpec.exchangeInternal(DefaultRestClient.java:441) ~[spring-web-6.1.1.jar:6.1.1]
    at org.springframework.web.client.DefaultRestClient$DefaultRequestBodyUriSpec.retrieve(DefaultRestClient.java:424) ~[spring-web-6.1.1.jar:6.1.1]
    at com.example.restclient.RestClientApplication.lambda$run$1(RestClientApplication.java:49) ~[classes/:na]
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144) ~[na:na]
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642) ~[na:na]
    at java.base/java.lang.Thread.run(Thread.java:1583) ~[na:na]

Here is the pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.0</version>
        <relativePath/>
    </parent>
    <groupId>com.example</groupId>
    <artifactId>restclient</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <java.version>21</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Comment From: poutsma

The RestClient is thread-safe once built, so it is not unexpected that a ConcurrentModification occurs in the RestClient.Builder if you use in in multiple threads.