[AMQ-9392] Prevent InactivityMonitor read check Timer leak when TCP connection fails #1119
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.
Context
Whenever the
InactivityMonitor
TransportFilter
is used in a scenario that would returnfalse
fromAbstractInactivityMonitor#configuredOk
, then the read check Timer is created but the call to startMonitorThreads exits prematurely, and themonitorStarted
flag is never set.Consequence
The class
AbstractInactivityMonitor
retains a strong static reference to the read checkTimer
created in startConnectCheckTast.This reference cannot be removed even by calling
stop
on theTransportFilter
, becausestopMonitorThreads
checks the monitorStarted flag before doing anything.Also, stopConnectCheckTask does not cancel the
Timer
nor it removes the strong reference to it.This reference to the
Timer
instance also implies a reference to the timerThread
, which will also retain the Thread Context ClassLoader (which may or may not be the sameClassLoader
that loadedAbstractInactivityMonitor
).Expectation
If the
AbstractInactivityMonitor
fires up aTimer
, there should be a way to dispose of it when it is no longer needed. It looks like it should happen when callingAbstractInactivityMonitor#stop
, but it is not the case for this particular scenario.Ideally there should be no
Timer
created if the connection is not properly configured (following the same logic as for the "monitor threads").About this PR
This PR includes a simple test that allows for reproducing the issue to help maintainers with troubleshooting it.
Additionally there is a very naive solution proposed by someone who is a complete stranger with this library, so it should be examined very carefully (if even at all).