OutOfMemoryError when uploading large file with RestTemplate and RestClient #32879
Labels
in: web
Issues in web modules (web, webmvc, webflux, websocket)
status: invalid
An issue that we don't feel is valid
Affects: 6.1.8
Edit: Originally reproduced in 6.1.6, but present in 6.1.8 which is latest version.
When uploading large file using multipart upload using either RestTemplate or RestClient, virtual machine will crash with out of memory error when InterceptingClientHttpRequestFactory wraps the Client's request factory.
I have narrowed issue to FormHttpMessageConverter::writeMultipart
When running the code with debugger, and no interceptor exists in RestTemplate, the execution is as follows
As soon as one interceptor is added, the execution is as follows
The InterceptingClientHttpRequest wrapped by InterceptingClientHttpRequestFactory does not implement StreamingHttpOutputMessage, so the second path is used - which in turn ends up using FastByteArrayOutputStream.
However, the InterceptingClientHttpRequest does seem to have code to handle StreamingHttpOutputMessage itself.
The resulting error is:
I can circumvent this issue for RestTemplate by clearing any interceptors. However, we utilize spring cloud load balancer, which is written as a Request Interceptor, which makes RestTemplate's requestFactory wrapped as InterceptingClientHttpRequestFactory.
I am not sure how to do this for rest client.
I have prepared a reproducer, by generating a file of ~360 MB. It is important to run the test with small heap size, i.e. -Xmx256m is enough. I have updated maven-surefire-plugin to set this - so as long as you use "./mvnw clean verify" the issue will be obvious. My IntelliJ IDEA test configuration is as follows
Examine the MultipartReproducerApplicationTests and comments on how to reproduce the wrong and right scenarios.
https://github.com/ZIRAKrezovic/spring-multipart-reproducer
The text was updated successfully, but these errors were encountered: