Skip to content
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

A memory leak caused by the reset method not removing subscribers ultimately leads to an OutOfMemoryError #1847

Open
1534196496 opened this issue Feb 22, 2024 · 2 comments

Comments

@1534196496
Copy link

Describe the issue
A memory leak caused by the reset method not removing subscribers ultimately leads to an OutOfMemoryError

What you are trying to do
Periodic reloading of Expectations.

MockServer version
5.15.0
To Reproduce
ClientAndServer client = ClientAndServer.startClientAndServer(92);
while (true) {
client.when(
request().withPath("/aaaa")
).withId("888")
.respond(
r -> response().withBody("1234")
);
client.reset();
}

  1. How you are running MockServer (i.e maven plugin, docker, etc)
    java Client

  2. Code you used to create expectations
    ClientAndServer client = ClientAndServer.startClientAndServer(92);
    client
    .when(....)
    .respond(....)

  3. What error you saw
    oom

@1534196496
Copy link
Author

image

@lnevaril
Copy link

I was dealing with the same issue two weeks ago so I could provide more details. The problem occurs only when adding expectation of type ExpectationResponseCallback that leads to registration of WebSocketClient required for reading the request so it can be used for generating response. WebSocketClient is subscribed to MockServerEventBus to listen for STOP and RESET events. There is a single MockServerEventBus for each port stored in static final field EVENT_BUS_MAP within class MockServerClient. When calling org.mockserver.client.MockServerClient#reset, RESET event is propagated to each subscriber but the issue is that subscribers registered within MockServerEventBus are never cleared, therefore, WebSocketClients don't become eligible for garbage collection during the tests run.

In my case I was able to bypass the issue by using the method clear to clear logs and manually remove expectations by ids to avoid calling the reset method. Using org.mockserver.client.ForwardChainExpectation#respond(org.mockserver.mock.action.ExpectationResponseCallback) is necessary only for expectations that need the ability to read the request and by registering them only once it one can mitigate the problem.
On the other hand, this state is far from ideal and subscribers in MockServerEventBus could be cleared once the event was propagated to them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants