Java-OCA-OCPP
Java-OCA-OCPP copied to clipboard
Cleanup after sending to avoid memory leak
There is a potential memory leak after sending a request using Server#send() or Client#send():
When a request is sent, the Request object and the associated CompletableFuture are stored in maps (in class Queue and PromiseRepository respectively).
While both are cleaned up when a successful confirmation is received from the communication partner, this is not the case if either an error or no confirmation at all is received:
- If an error is received only the
PromiseRepositorybut not theQueueis cleaned up. - If no message is received there is no way to clean up any of the maps.
This pull request tries to solve these problems:
Queue and PromiseRepository are cleaned up whenever the CompletableFuture resolves, whether successful or not.
This gives users of the send() method the chance to add a timeout to the returned CompletableFuture, e.g. by using:
server.send(sessionIndex, request).orTimeout(10, TimeUnit.SECONDS)
(refer to ServerTest#send_aMessage_promiseCompletesWithTimeout())
As the returned CompletableFuture is identical to one inside send(), after a timeout occurs, the future is completed with a TimeoutException and both maps will be cleaned up.
I'm very interested in your feedback!
By the way, thank you very much for your work on this nice and elegant library!
Best regards, Martin
Hi everyone, is there any feedback about the bug and my proposed solution?
Best regards, Martin
Hi @martinfkaeser,
Sorry for the very slow response. This is an important finding and an elegant solution. Thank you for this contribution.