-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RemoveCachedBodyFilter does not release direct memory correctly resulting in a memory leak #2969
Comments
Can you give me some direction on the number of requests, how many parallel runners and rough times it takes? I'm unable to reproduce this with the latest supported Spring Boot and Cloud. |
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed. |
The problem arises when the user needs to read the requestBody data multiple times. The ServerWebExchangeUtils.cacheRequestBody method is used multiple times, as described in the issue. To simulate the user's behavior, two custom filters need to be added. Additionally, the -XX:MaxDirectMemorySize=24M parameter is included in the startup arguments to limit the size of direct memory. I used the JMeter tool with a thread count of 8, and the error occurs after approximately 10 seconds. |
Can you provide that? |
@spencergibb Please use branch gh2969 Instructions are in readme under "Test" label |
Using Spring Boot 3.2.3 and Spring Cloud 2023.0.1-SNAPSHOT the error goes away.
|
This comment was marked as outdated.
This comment was marked as outdated.
There is a mistake - in xml you provided Spring Boot version is 2.6.3 and this downgrade really fixes the issue. But if I set 3.2.3 - the error persists. FYI on the https://spring.io/projects/spring-cloud is written that Spring Cloud 2023.0.x is compatible with Spring Boot 3.2.x |
Thanks for noticing. |
Just the one time is all I see, correct? |
Not sure if I understood you, if it was question to me. |
Yes, sorry about the wording. I only see the error one time, but I'm on an M1 mac, which might make a difference. |
regarding the demo for two custom filters, it has been provided in the issue description. |
Can you try with 4.1.2-SNAPSHOT (after the build is done in 30 minutes or so)? I tried with the project supplied here and did not get the error any longer. |
I have tested it and this submission indeed solves the problem. However, I don't think it's sufficient because if users manually set CACHED_REQUEST_BODY_ATTR instead of using ServerWebExchangeUtils#cacheRequestBody method, there is still no guarantee of proper release. We can ensure that DataBuf is correctly released by using RemoveCachedBodyFilter. Please refer to the following PR for more details: |
Thanks for trying and pointing out the PR |
Describe the bug
This problem still exists in the latest 3.1.1 version,In RemoveCachedBodyFilter, the cached DataBuffer is removed and the release method is called. However, when the DataBuffer object is referenced multiple times, there may be a memory leak.
Sample
When the ServerWebExchangeUtils.cacheRequestBody method is called in multiple Filters, a ServerHttpRequest object decorated multiple times will be generated. When reading data, the retain method will be called multiple times, and eventually the release method in RemoveCachedBodyFilter cannot release the direct memory. However, we cannot restrict users from calling the ServerWebExchangeUtils.cacheRequestBody method multiple times, so we should ensure that the DataBuffer can be released correctly in the RemoveCachedBodyFilter class.
reproduce
Add these two filters to the project:
Then use the pressure measurement tool to test for a period of time, and an error will appear:
Adding this parameter can make errors appear faster:
The text was updated successfully, but these errors were encountered: