Skip to content

Fix thread leak when calling createDynamicSession() multiple times#1103

Merged
chrjohn merged 3 commits into
masterfrom
copilot/fix-thread-leak-in-sessions
Jan 7, 2026
Merged

Fix thread leak when calling createDynamicSession() multiple times#1103
chrjohn merged 3 commits into
masterfrom
copilot/fix-thread-leak-in-sessions

Conversation

Copilot AI commented Dec 16, 2025

Copy link
Copy Markdown
Contributor

Thread Leak Fix in AbstractSocketInitiator::createDynamicSession

Plan:

  • Understand the issue: Multiple calls to createDynamicSession create multiple session timers but only the last one is cancelled
  • Create a unit test that reproduces the thread leak issue
  • Fix the startSessionTimer() method to check if a timer is already running before creating a new one
  • Run the new test to verify the fix
  • Run existing tests to ensure no regression (SessionConnectorTest, SocketInitiatorTest pass)
  • Address code review feedback: Use existing checkSessionTimerRunning() method and remove debug logging
  • Request code review
  • Run security scan

Summary:

Fixed thread leak in AbstractSocketInitiator::createDynamicSession by refactoring the check in SessionConnector.startSessionTimer() to use the existing checkSessionTimerRunning() method instead of duplicating the logic. Removed debug logging as requested.

Original prompt

This section details on the original issue you should resolve

<issue_title>Calling AbstractSocketInitiator::createDynamicSession multiple times will result in thread leak</issue_title>
<issue_description>Describe the bug
AbstractSocketInitiator::createDynamicSession calls AbstractSocketInitiator::startInitiators which then calls SessionConnector::startSessionTimer. SessionConnector::startSessionTimer then creates a new scheduled SessionTimerTask check-in and overwrites the SessionConnector.sessionTimerFuture field with the ScheduledFuture<?> representing the session. When the AbstractSocketInitiator is eventually stopped cancel is called on that field. But if multiple dynamic sessions were added then only the last scheduled session timer is cancelled while all the others continue running.

To Reproduce
Create a SocketInitiator or ThreadedSocketInitiator and then call createDynamicSession multiple times.

Expected behavior
Calling AbstractSocketInitiator::createDynamicSession should not register a new session timer if one is already registered.

system information:

  • OS: any
  • Java version: any
  • QFJ Version: I'm using 2.3.2 but this has likely been around for a while.

Additional context
Add any other context about the problem here.
</issue_description>

<agent_instructions>Please create a PR with a unit test that reproduces this issue and please provide a fix for the issue.</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@chrjohn @Jallibad , thanks for the bug report. Looking briefly at the code it seems you suggest that creating `SessionTimerTask` here should only be done once:

protected void startSessionTimer() {
Runnable timerTask = new SessionTimerTask();
if (shortLivedExecutor != null) {
timerTask = new DelegatingTask(timerTask, shortLivedExecutor);
}
sessionTimerFuture = SCHEDULED_EXECUTOR.scheduleAtFixedRate(timerTask, 0, 1000L,
TimeUnit.MILLISECONDS);
log.info("SessionTimer started");
}


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: chrjohn <6644028+chrjohn@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix thread leak in createDynamicSession method Fix thread leak when calling createDynamicSession multiple times Dec 16, 2025
Copilot AI requested a review from chrjohn December 16, 2025 18:18
Comment thread quickfixj-core/src/main/java/quickfix/mina/SessionConnector.java Outdated
Co-authored-by: chrjohn <6644028+chrjohn@users.noreply.github.com>
Copilot AI requested a review from chrjohn December 17, 2025 11:33
@chrjohn chrjohn changed the title Fix thread leak when calling createDynamicSession multiple times Fix thread leak when calling createDynamicSession() multiple times Dec 18, 2025
@chrjohn chrjohn added this to the QFJ 3.0.0 milestone Dec 18, 2025
@chrjohn chrjohn marked this pull request as ready for review December 18, 2025 15:39
@chrjohn chrjohn merged commit 8b0418b into master Jan 7, 2026
31 checks passed
@chrjohn chrjohn deleted the copilot/fix-thread-leak-in-sessions branch January 7, 2026 11:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SessionConnector::startSessionTimer causes thread leak on dynamic sessions

2 participants