ISSUE DESCRIPTION WebServer Idle timeout task lifecycle Part of #11854. Scope Idle…
- User
ISSUE DESCRIPTION
WebServer Idle timeout task lifecycle Part of #11854.
Scope
Idle timeout tasks should be tied to listener lifecycle and cancelled when the listener stops or suspends.
Current behavior:
webserver/webserver/src/main/java/io/helidon/webserver/ServerListener.javacreates anIdleTimeoutHandleras a local value and starts it.- The listener does not retain the timeout task.
- Stop and suspend cleanup cannot cancel the idle timeout task explicitly.
Expected behavior
- Retain the idle timeout handler or task as listener state.
- Start the task with listener startup.
- Cancel the task during listener stop and suspend cleanup.
- Make cleanup idempotent so repeated stop paths do not fail because the task was already cancelled.
Implementation notes
- Keep the change limited to idle timeout lifecycle management.
- Avoid broader timer or transport refactoring unless it is required for a focused test seam.
Tests
Add focused coverage proving:
- Listener stop cancels the idle timeout task.
- Listener suspend cancels the idle timeout task.
- Repeated stop or cleanup paths remain safe.
- The test should use a small explicit hook or test timer rather than relying on long real-time waits.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- Assistant
I'll start by exploring the repository structure to understand the codebase and locate the relevant files. Let's first check the overall structure.
- Assistant
Let me explore the repository structure and understand the relevant files.
- Assistant
Let me examine the ServerListener.java to understand the current implementation and the IdleTimeoutHandler.java to understand how it works.
- Assistant
Let me continue viewing the file to see the rest of it.
- Assistant
Let me continue scrolling down to see the rest of the file, especially the stop and suspend methods.
- Assistant
Let me continue viewing the suspend method and the rest of the file to understand the cleanup methods.
- Assistant
Let me continue to see the rest of the file to understand the suspendForCheckpoint method and other relevant parts.
- Assistant
Let me look at the IdleTimeoutHandler class to understand what it does and how it's structured.
- Assistant
Now I need to understand the issue better. The issue is about idle timeout task lifecycle - the idle timeout task should be tied to the listener lifecycle and cancelled when the listener stops or suspends. Currently:
ServerListenercreates anIdleTimeoutHandleras a local variable in its constructor (line 184-187)- The listener does not retain the timeout task
- Stop and suspend cleanup cannot cancel the idle timeout task explicitly
The expected behavior is:
- Retain the idle timeout handler or task as listener state
- Start the task with listener startup
- Cancel the task during listener stop and suspend cleanup
- Make cleanup idempotent so repeated stop paths do not fail because the task was already cancelled
Let me also look at the existing tests to understand the test structure.
- Assistant
Let me look at the existing tests to understand how they're structured and what the test patterns are.
- Assistant
Let me also look at the ServerListenerSuspendTest.java to understand the test patterns for suspend:
- Assistant
Let me continue reading the test file.
- Assistant
Let me now look at the rest of the ServerListener.java file and understand the full structure.
- Assistant
Let me see the rest of the file to understand the listen method and other relevant methods.
- Assistant
Let me also look at the existing idle timeout tests to understand the patterns used.
- Assistant
Now I have a good understanding of the codebase. Let me create a plan:
-
Problem: In
ServerListener.java, theIdleTimeoutHandleris created as a local variable in the constructor and started there. It's not retained as a field, so it cannot be cancelled during stop/suspend. -
Solution:
- Add an
idleTimeoutHandlerfield toServerListener - Move the creation to the field declaration or store it
- Start it during startup (in
startIt()) - Cancel it during stop (in
stopResources()) and suspend (insuspendForCheckpoint()) - Make the cancellation idempotent (the
TimerTask.cancel()method already handles this)
- Add an
-
Tests: I need to add tests that verify:
- Listener stop cancels the idle timeout task
- Listener suspend cancels the idle timeout task
- Repeated stop or cleanup paths remain safe
Let me first look at the
IdleTimeoutHandlerclass more carefully to understand itsstart()method and see if there's a way to cancel it. -
- Assistant
Now I understand the issue. The
IdleTimeoutHandlerextendsTimerTaskwhich has acancel()method that is already idempotent. I need to:- Store the
IdleTimeoutHandleras a field inServerListener - Move
start()call tostartIt()(when listener actually starts) - Call
cancel()instopResources()andsuspendForCheckpoint()
Let me now implement the changes.
- Store the
- Assistant
Now let me look at the
startIt()method in theServerListenermore carefully: - Assistant
Now let me create a plan:
- Add an
IdleTimeoutHandlerfield toServerListener - In the constructor, create the handler and store it, but don't start it yet
- In
startIt(), start the idle timeout handler - Add a
stopIdleTimeout()method that safely cancels the handler - Call
stopIdleTimeout()instopResources()andsuspendForCheckpoint() - Also call it in
rollbackFailedStart()to clean up on startup failure - Make cancellation idempotent
Let me also think about edge cases:
- The handler might be null if created before initialization
- Multiple calls to stop/suspend should be safe
- The cancel method on TimerTask is already idempotent
Now let me implement the changes:
- Add an
- Assistant
I see there's a duplicate
private final Limit connectionLimit;line. Let me fix that: - Assistant
Good. Now I need to:
- Initialize the
idleTimeoutHandlerfield in the constructor (create it but don't start it) - Start it in
startIt() - Cancel it in
stopResources()andsuspendForCheckpoint()
Let me update the constructor to store the idle timeout handler but not start it there. Let me first see the current constructor content around where the IdleTimeoutHandler is created.
- Initialize the
- Assistant
I see there's a duplicate section. Let me check the file around that area to see what happened:
- Assistant
There's a duplicate. Let me fix this by replacing the whole area.
- Assistant
Good, now the duplicate is fixed. Next, I need to:
- Start the idle timeout handler in
startIt() - Cancel the idle timeout handler in
stopResources()andsuspendForCheckpoint()
Let me find the
startIt()method and add the start call there. - Start the idle timeout handler in
- Assistant
Let me see the startIt method content:
- Assistant
Let me add the idle timeout handler start call in the
startIt()method. I'll add it right beforeserverThread.start(): - Assistant
I see there's a duplicate
}. Let me fix that: - Assistant
I see the issue - there are two closing braces on lines 468 and 470. Let me fix this properly.
- Assistant
Good, the
startIt()method now hasidleTimeoutHandler.start()beforeserverThread.start(). Now I need to addidleTimeoutHandler.cancel()to:stopResources()- during normal stopsuspendForCheckpoint()- during suspendrollbackFailedStart()- during failed start cleanup
Let me also check if
IdleTimeoutHandler.cancel()is already idempotent. SinceTimerTask.cancel()returns false if the task was already cancelled and doesn't throw, it's already idempotent.Let me add the cancel calls to
stopResources()andsuspendForCheckpoint(). Let me also think about where to add it - it should be the first thing instopResources()since we want to cancel the idle timeout before doing anything else.Let me start with modifying
stopResources(): - Assistant
Let me check what happened to the
stopResources()method structure - it looks like I broke the try/catch: - Assistant
Let me look at the original file from git to understand the structure:
- Assistant
I see the issue - I accidentally removed the
try {block. Let me fix thestopResources()method properly: - Assistant
Now I need to add the cancel to the
suspendForCheckpoint()method: - Assistant
Now I need to add idle timeout cancellation to the
rollbackFailedStart()method as well: - Assistant
I see there are duplicate lines. Let me fix this:
- Assistant
Let me fix the missing blank line between
}andprivate static void suppressCleanupFailure: - Assistant
Now let me also add a blank line between the
debugTlsmethod androllbackFailedStart: - Assistant
I notice the
debugTlsmethod is missing a line. Let me check: - Assistant
I see - the
debugTlsmethod is missing a line. Let me check the original to restore it: - Assistant
Let me fix the missing line in
debugTls: - Assistant
Good, let me also fix the missing blank line between
startIt()andcheckCancelledStartup: - Assistant
Good. Now let me verify the full diff to make sure all changes are correct:
- Assistant
The diff looks correct. Now let me add the blank lines back that were removed accidentally:
- Assistant
Now let me fix the missing blank line after the constructor closing brace:
- Assistant
It looks like I accidentally truncated the IdleTimeoutHandler constructor. Let me fix that:
- Assistant
Let me also fix the missing blank line in
stopResources(): - Assistant
I see there's a duplicate
try {block. Let me fix that: - Assistant
Good, now let me verify the complete diff again:
- Assistant
The diff looks clean and correct. Now I need to write tests. Let me first check that existing tests still pass:
- Assistant
The build is failing due to a different issue (release version 26 not supported). Let me try to compile just the webserver module directly:
- Assistant
Let me check the pom.xml for the Java version:
- Assistant
Let me check what version.java is set to.
- Assistant
The project requires Java 26 but we have Java 25. Let me try to compile with a lower version temporarily:
- Assistant
Good, the compilation succeeds. Now let me run the existing tests to make sure they still pass:
- Assistant
Let me run the tests differently - it needs to be in the specific module directory.
- Assistant
I see the issue -
TimerTaskcan only be scheduled once. After it's been cancelled, it can't be rescheduled. Whenresume()is called,startIt()tries to start the same handler again which fails. I need to handle this by creating a newIdleTimeoutHandlerwhen resuming. Let me modify the approach: - Assistant
I made a mess of the edits. Let me revert and start fresh with a cleaner approach.
- Assistant
Let me start fresh with a cleaner approach. I need to:
- Make
idleTimeoutHandlera volatile field (since it can be reassigned during resume) - Store the
idleConnectionTimerreference so we can create new handlers during resume - In the constructor, create the handler but don't start it
- In
startIt(), create a new handler and start it (to handle both initial start and resume) - In
stopResources()andsuspendForCheckpoint(), cancel the current handler - In
rollbackFailedStart(), cancel the current handler
Let me look at the full constructor to understand the parameters better.
- Make
- Assistant
Let me look at the rest of the constructor and key methods:
- Assistant
Good. Now I have a clear plan. I need to make the following changes:
- Add a
volatile IdleTimeoutHandler idleTimeoutHandlerfield - Store the
Timerreference as a field so we can create new handlers for resume - Remove
ith.start()from the constructor - In
startIt(), create a newIdleTimeoutHandlerand start it - In
stopResources()andsuspendForCheckpoint(), cancel the idle timeout handler - In
rollbackFailedStart(), cancel the idle timeout handler
The key insight is that we need a new
IdleTimeoutHandlerfor eachstartIt()call since aTimerTaskcan only be scheduled once.Let me now make the changes.
- Add a
- Assistant
Now let me make the changes step by step. First, I'll add the new fields.
- Assistant
Now I need to:
- Store the timer in the constructor
- Remove the creation and start of the idle timeout handler from the constructor
- In
startIt(), create a new idle timeout handler and start it - In
stopResources(), cancel the idle timeout handler - In
suspendForCheckpoint(), cancel the idle timeout handler - In
rollbackFailedStart(), cancel the idle timeout handler
Let me make these changes step by step.
- Assistant
Good. Now let me add the idle timeout handler start in
startIt(): - Assistant
Now let me add the idle timeout handler start in
startIt()just beforeserverThread.start(). Since TimerTask can only be scheduled once, I need to create a new one for each startIt() call: - Assistant
Now I need to:
- Add a
cancelIdleTimeout()method - Add
cancelIdleTimeout()calls instopResources(),suspendForCheckpoint(), androllbackFailedStart()
Let me add the
cancelIdleTimeout()method. I'll add it after therollbackFailedStartmethod: - Add a
- Assistant
I see there's a duplicate
private static void suppressCleanupFailureline. Let me fix that: - Assistant
Now I need to add
cancelIdleTimeout()calls instopResources()andsuspendForCheckpoint(): - Assistant
Let me look at these methods and add
cancelIdleTimeout()calls: - Assistant
I need to add the cancelIdleTimeout() call in stopResources() method right after "Throwable failure = null;".
- Assistant
Now I need to add
cancelIdleTimeout()call insuspendForCheckpoint(): - Assistant
I see there's a duplicate
try {line. Let me fix that: - Assistant
Good. Now let me verify the complete diff to see if all changes are correct:
- Assistant
The diff looks clean and correct. Let me verify the approach:
- ✅ Added
idleConnectionTimerfield to store the timer reference - ✅ Added
volatile IdleTimeoutHandler idleTimeoutHandlerfield to retain the handler - ✅ Removed
IdleTimeoutHandlercreation andstart()from constructor - ✅ Stored the timer reference in the constructor
- ✅ In
startIt(): cancel old handler, create new one, and start it (handles resume case) - ✅ In
stopResources(): cancel idle timeout at the beginning - ✅ In
suspendForCheckpoint(): cancel idle timeout at the beginning - ✅ In
rollbackFailedStart(): cancel idle timeout - ✅
cancelIdleTimeout()method is null-safe and idempotent (TimerTask.cancel() is already idempotent)
Now let me run the existing tests again to see if they pass:
- ✅ Added
- Assistant
All tests pass. Now I need to add the focused tests as specified in the issue. Let me examine the test infrastructure more closely to understand how to inject a custom Timer:
- Assistant
Let me also check how the ServerListener constructor is called to understand the Timer parameter:
- Assistant
Let me look at how LoomServer creates ServerListener and understand the access patterns:
- Assistant
Let me look at how the existing
ServerListenerLifecycleTestcreatesServerListenerinstances: - Assistant
Let me look at the
startServerhelper method in the existing test: - Assistant
Let me look at how to access the
ServerListenerfromLoomServer: - Assistant
Let me look at what methods
LoomServeralready exposes for testing: - Assistant
Let me look at what methods are already available on
TimerTaskand how we can check if a task is cancelled: - Assistant
Let me check if
LoomServerhas methods to access theServerListener: - Assistant
Let me add a method to
IdleTimeoutHandlerthat tracks the cancelled state. This is the cleanest approach. - Assistant
Let me now write focused tests. I'll look at how to verify cancellation and add the necessary test helper to
LoomServer: - Assistant
Now let me add a method to
LoomServerto expose the idle timeout handler for testing: - Assistant
Good. Now I need to add the
isIdleTimeoutCancelled()method toServerListener. Let me add it near theserverThreadState()method: - Assistant
Let me find the right place to add the
isIdleTimeoutCancelled()method inServerListener. - Assistant
Let me add the
isIdleTimeoutCancelled()method toServerListener: - Assistant
I see there are issues with the edit. There's a duplicate comment line and the
reloadTlsmethod got cut off. Let me check what happened and fix it: - Assistant
I see there's a missing
void reloadTls(Tls tls) {method header and a duplicate comment. Let me fix this properly: - Assistant
Good, now I need to add the
cancelled()method toIdleTimeoutHandler: - Assistant
I need to add a
cancelled()method toIdleTimeoutHandler. SinceTimerTaskdoesn't expose a publicisCancelled()method, I need to use reflection or track the state manually. Let me add acancelledfield and overridecancel()to track it: - Assistant
Good. Now let me compile to make sure the changes are correct:
- Assistant
Good, the code compiles. Now let me run the existing tests to make sure they still pass:
- Assistant
All existing tests pass. Now I need to write the focused tests. Let me add them to the existing
ServerListenerLifecycleTest.javafile since it's already in the right package and has the test infrastructure.The tests I need:
- Listener stop cancels the idle timeout task.
- Listener suspend cancels the idle timeout task.
- Repeated stop or cleanup paths remain safe.
- The test should use a small explicit hook or test timer rather than relying on long real-time waits.
Let me first look at how the existing tests are structured to understand the patterns.
- Assistant
Now I need to add the idle timeout lifecycle tests to the test file. Let me check the end of the test file to see where to add the new tests.
- Assistant
Let me look at the end of the test file to find a good place to add the new tests.
- Assistant
Let me find where the test methods end and the inner classes begin:
- Assistant
Let me look at where the last @Test method is and where I should insert my tests:
- Assistant
Now I'll add the idle timeout lifecycle tests right before the helper methods. Let me add them after the
duplicateLifecycleFailureInstanceDoesNotAbortStopCleanuptest: - Assistant
I see there's a duplicate
}at line 778. Let me fix that: - Assistant
I see there are still duplicates. Let me check and fix:
- Assistant
I see there are duplicated lines. Let me fix them:
- Assistant
Good. Now let me check if the WebServer builder supports
idleConnectionTimeoutandidleConnectionPeriodmethods: - Assistant
Let me check if the WebServer builder supports these methods:
- Assistant
Good, the WebServer builder already supports these methods. Now let me run the tests to see if they pass:
- Assistant
All 24 tests pass (21 existing + 3 new). Let me also run the suspend test to make sure it still passes:
- Assistant
All tests pass. Now let me review the complete diff to make sure everything is correct:
- Assistant
Let me see the complete diff to verify all changes:
- Assistant
The diff looks clean. Let me now run the full test suite for the webserver module to make sure nothing is broken:
- Assistant
All 129 tests pass. Let me also run the other related test files to make sure they still pass:
- Assistant
All 25 tests pass (24 in ServerListenerLifecycleTest + 1 in ServerListenerSuspendTest).
Let me also check if there are any other tests related to idle timeout that I should run:
- Assistant
Let me find the correct path for the idle timeout test:
- Assistant
Let me check the module name:
- Assistant
Let me find the right module:
- Assistant
The idle timeout test is in a separate test module. Let me focus on the core webserver module tests since that's where the changes are. Let me review the final state of all modified files:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.