[AMQ-7452] Fix "Timer already cancelled" on new connections when shared inactivity timer dies#1963
Open
jbonofre wants to merge 1 commit intoapache:activemq-6.2.xfrom
Open
Conversation
…ed inactivity timer dies
The static READ_CHECK_TIMER and WRITE_CHECK_TIMER in AbstractInactivityMonitor are
shared across all transport instances. Java's Timer permanently marks itself cancelled
when its internal thread exits due to an unhandled Error thrown by a TimerTask.
SchedulerTimerTask rethrows Error subclasses, so an OutOfMemoryError in any timer task
kills the shared timer thread while the static field remains non-null. All subsequent
schedule() calls on new connections then throw IllegalStateException ("Timer already
cancelled"), which propagates to TransportConnector.onAcceptError() and is logged as
WARN "Could not accept connection: Timer already cancelled."
Fix: wrap the schedule() calls in startConnectCheckTask() and startMonitorThreads()
in try/catch(IllegalStateException). On catch, the dead timer is cancelled (to let its
thread exit), replaced with a fresh instance, and the task is rescheduled. The guard
condition in startConnectCheckTask() is also extended with || READ_CHECK_TIMER == null
to cover the case where the field is unexpectedly null when CHECKER_COUNTER > 0.
Add three regression tests to InactivityMonitorTest covering:
- READ_CHECK_TIMER cancelled with CHECKER_COUNTER == 0 (startConnectCheckTask path)
- READ_CHECK_TIMER cancelled with CHECKER_COUNTER > 0 (production scenario)
- WRITE_CHECK_TIMER cancelled (startMonitorThreads path)
Tests use Wait.waitFor() instead of Thread.sleep() for reliability on slow CI.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The static
READ_CHECK_TIMERandWRITE_CHECK_TIMERinAbstractInactivityMonitorare shared across all transport instances. Java's Timer permanently marks itself cancelled when its internal thread exits due to an unhandled Error thrown by a TimerTask.SchedulerTimerTaskrethrows Error subclasses, so anOutOfMemoryErrorin any timer task kills the shared timer thread while the static field remains non-null. All subsequentschedule()calls on new connections then throwIllegalStateException ("Timer already cancelled"), which propagates toTransportConnector.onAcceptError()and is logged asWARN "Could not accept connection: Timer already cancelled."Here's my fix proposal: wrap the
schedule()calls instartConnectCheckTask()andstartMonitorThreads()intry/catch(IllegalStateException). On catch, the dead timer is cancelled (to let its thread exit), replaced with a fresh instance, and the task is rescheduled. The guard condition instartConnectCheckTask()is also extended with|| READ_CHECK_TIMER == nullto cover the case where the field is unexpectedlynullwhenCHECKER_COUNTER > 0.I added three regression tests to InactivityMonitorTest covering:
Also, tests use Wait.waitFor() instead of Thread.sleep() for reliability on slow CI.