mock-server / mockserver

MockServer enables easy mocking of any system you integrate with via HTTP or HTTPS with clients written in Java, JavaScript and Ruby. MockServer also includes a proxy that introspects all proxied traffic including encrypted SSL traffic and supports Port Forwarding, Web Proxying (i.e. HTTP proxy), HTTPS Tunneling Proxying (using HTTP CONNECT) and SOCKS Proxying (i.e. dynamic port forwarding).
http://mock-server.com
Apache License 2.0
4.61k stars 1.08k forks source link

MemoryLeak in LRUCache #1644

Open eric6iese opened 1 year ago

eric6iese commented 1 year ago

Describe the issue The field org.mockserver.cache.LRUCache.allCaches poses at least a memory leak, perhaps even a threadsafety issue.

What you are trying to do We have a long running testsuite where we create and stop multiple ClientAndServer Instances in Java. There is never more than two or three active instances around, but the memory was increasing. In VisualVM i could dected that this class holds even the stopped HttpState-Objects in the LRUCache, which held a lot of LogEntry instances, even though the EventLog in MockServer was cleared. The reason for this is the internal disruptor which keeps hold of these instances - this is by design.

The leak in LRUCache is the private static field allStates. It is used for nothing else than a static method LRUCache.clearAllCaches(), but it does not dected if those instances are even in use anywhere. Also I am not sure if this is even threadsafe, it isn't if multiple constructors of LRUCache could be called in parallel.

MockServer version 5.14, but I see no changes in the current implementation.

To Reproduce Simple - create multiple ClientAndServer instances, do some interaction, stop them and watch what happens in the LRUCache with the debugger

Fix The most simple fix would be to change the declaration to

private static final Set<LRUCache<?, ?>> allCaches = Collections.newSetFromMap(new WeakHashMap<>());

In addtion, synchronized blocks would be needed around the interactions.

asolntsev commented 9 months ago

I see it's fixed by https://github.com/mock-server/mockserver/pull/1648 Is the release date known?