rabbitmq/rabbitmq-java-client

Memory leak in RpcClient

JHahnHRO opened this issue · 2 comments

Describe the bug

RpcClient must be given a Channel upon construction and if used in mandatory-mode, the constructor adds a ReturnListener to this channel. However, this listener is never removed. It should be removed in the close-method.

Reproduction steps

  1. Instantiate RpcClient in mandatory-mode.
  2. Publish an unrouteable RPC message with the client and catch the UnrouteableRpcRequestException.
  3. Close the RpcClient.
  4. Repeat 1.-3. using the same channel.

Observe that first there will be an exception, then there will be an exception and a log-warning, then an exception and two log-warnings, then an exception and three log-warnings and so on. The reason is that each new RpcClient adds its own ReturnListener. One of them throws the exception, the other log warnings, because they look up the returned message in the previous RpcClient's continuation maps.

The log messages are annoying, but more importantly, the listeners accumulate over time and live for as long as the channel stays open.

Expected behavior

Closing an RpcClient should clean up thoroughly, including the removal of the ReturnListener so that the channel may be reused.

Additional context

No response

@JHahnHRO please submit a PR.

Also note that this class is an example you can and should modify/build upon. But maybe that's exactly why we should make it clean any ReturnListeners it adds.