Cleanup after sending to avoid memory leak #334
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There is a potential memory leak after sending a request using Server#send() or Client#send():
When a request is sent, the
Requestobject and the associatedCompletableFutureare stored in maps (in classQueueandPromiseRepositoryrespectively).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:
PromiseRepositorybut not theQueueis cleaned up.This pull request tries to solve these problems:
QueueandPromiseRepositoryare cleaned up whenever theCompletableFutureresolves, whether successful or not.This gives users of the
send()method the chance to add a timeout to the returnedCompletableFuture, e.g. by using:server.send(sessionIndex, request).orTimeout(10, TimeUnit.SECONDS)(refer to ServerTest#send_aMessage_promiseCompletesWithTimeout())
As the returned
CompletableFutureis identical to one insidesend(), after a timeout occurs, the future is completed with aTimeoutExceptionand 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