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.
Interface
No new interfaces are introduced.
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:
Assistant
Let me start by exploring the repository structure to understand the codebase.
Assistant
Let me see the middle of the file and the IdleTimeoutHandler:
Assistant
Let me view the relevant portions of
ServerListener.java:Assistant
Let me look at the tests directory:
Assistant
Let me see the full file structure to understand all test cases:
Assistant
Now let me look at the LoomServer and WebServer:
Assistant
Now let me look at the rest of the ServerListener.java to understand the start method:
Assistant
Let me look at the issue again carefully. The issue says:
- Idle timeout tasks should be tied to listener lifecycle and cancelled when the listener stops or suspends.
- The listener does not retain the timeout task.
- Stop and suspend cleanup cannot cancel the idle timeout task explicitly.
Let me also look at the test file to understand the structure for new tests:
Assistant
Let me look at the WebServerConfig / ListenerConfig to understand how to set up tests:
Assistant
Let me search for any existing IdleTimeoutHandler tests:
Assistant
Now let me check the ServerListenerSuspendTest:
Assistant
Now let me understand how the ServerListener is created. Specifically, looking at where the timer is created:
Assistant
Let me look at WebServerConfigBlueprint and other listeners config:
Assistant
Let me see the ConnectionHandler class to understand closeIfIdle:
Assistant
Now let me think about the plan. The issue asks for:
- Retain the idle timeout handler or task as listener state (i.e., make it a field).
- Start the task with listener startup (currently done in constructor).
- 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 check the Timer / TimerTask usage:
Assistant
Let me see how LoomServer exposes listeners:
Assistant
Let me look at the existing LoomServer test patterns more:
Assistant
Let me check the LoomServer to see how to expose listeners, and look at the IdleTimeoutHandler more carefully:
Assistant
Let me look at the structure of the tests once more to see how they create test scenarios:
Assistant
Now let me create my reproduction script to understand the current behavior:
Assistant
Now let me check the WebServer interface to find Resumable:
Assistant
Let me see what tests are available for ServerListener class itself:
Assistant
Let me check the ConnectionHandlerTest to see the patterns:
Assistant
Now let me look at how I'd write a test. Let me first make the changes to the source code, then write a test:
Assistant
Now I have all the context I need. Let me plan and implement the changes:
Assistant
Let me look at the fields of ServerListener:
Assistant
Now I have a complete understanding. Let me start implementing the changes:
Assistant
Now I'll start implementing. First, let me modify the IdleTimeoutHandler:
Assistant
Now let me modify the ServerListener. First, let me update the field declaration:
Assistant
Now let me update the constructor to remove the start call:
Assistant
I see I accidentally duplicated
this.router = router;. Let me fix this:Assistant
Let me look at the file in the area to see what's wrong:
Assistant
The
public MediaContext mediaContext() {declaration got removed. Let me restore it:Assistant
The file is in a bad state. Let me reset it and try again with a cleaner approach:
Assistant
Good. Now let me view the original ServerListener to start fresh:
Assistant
Now let me make the change carefully:
Assistant
Now I'll add a field for the Timer and the IdleTimeoutHandler. Let me also add the idleConnectionTimer as a field:
Assistant
Let me fix the duplicate
closeFutureline:Assistant
The file is getting messy with my edits. Let me reset and use a different approach:
Assistant
Now let me make a backup of the original ServerListener.java and work on a modified copy:
Assistant
Let me look at the original file to make a comprehensive plan:
Assistant
Let me write a Python script to make all the changes to ServerListener.java at once:
Assistant
Let me use perl or just write sed commands:
Assistant
Let me make the changes one by one using the edit commands carefully:
Assistant
Let me edit each block carefully. First, let me add the new fields:
Assistant
Now let me modify the constructor to store the timer and not start the task:
Assistant
Now let me update the start method to start the idle timeout task:
Assistant
Let me check what happened to the file:
Assistant
Let me revert and use a different approach:
Assistant
Now let me apply changes very carefully, one at a time:
Assistant
Now let me update the constructor end. I need to replace lines 186-190:
Assistant
Let me reset and use a different approach with a more precise script:
Assistant
Now let me apply all my changes using perl with single-line replacements:
Assistant
Let me reset and use a different approach with the edit command:
Assistant
Let me write a Perl script that does the replacement in one pass:
Assistant
All patches applied. Let me verify the changes:
Assistant
All changes are applied correctly. Let me also verify the IdleTimeoutHandler change:
Assistant
Now let me compile the code to make sure it builds:
Assistant
Java 26 is needed but only Java 25 is available. Let me see if Ican use an earlier release:
Assistant
The compilation succeeds. Now let me also verify the file looks correct:
Assistant
Now let me run the existing tests to make sure they still pass:
Assistant
All 21 existing tests pass. Now let me also run the ServerListenerSuspendTest:
Assistant
Let me also run all the tests in the package:
Assistant
All 66 tests pass. Now let me write the tests for the new behavior:
Assistant
Let me look at the test at the end:
Assistant
Now let me write the new tests for the idle timeout task lifecycle. I'll add them before the helper methods (around line 779):
Assistant
Let me write the new tests in a separate file, then merge them into the existing file:
Assistant
Now let me insert these tests into the existing test file. I need to insert before line 779 (where
private static boolean containsMessagestarts):Assistant
Now I need to add a
listener()method toLoomServerso the tests can access the listener:Assistant
Now let me compile and run the tests:
Assistant
Now let me run the tests:
Assistant
Let me look at the test more carefully - the issue is that we're checking the old handler reference:
Assistant
I see I duplicated the @Test annotation. Let me fix this:
Assistant
I lost the
containsMessagemethod header. Let me restore it:Assistant
Let me reset the test file and start fresh:
Assistant
Now let me update the
resumeRestartsIdleTimeoutTaskAfterSuspendCancellationtest to check the current handler:Assistant
The line replacement didn't work. Let me look at the actualline:
Assistant
Now let me run the tests:
Assistant
All 26 tests pass. Let me also run the full test suite for the webserver module:
Assistant
All 131 tests pass. Now let me verify my changes are complete and check that they match what was requested:
Assistant
All changes look correct. Let me also clean up the extra blank line:
Assistant
Let me now also create a reproduction script to confirm the original bug exists (the IdleTimeoutHandler was never cancelled), and then verify my fix resolves it:
Assistant
Let me verify the bug existed before my fix. Let me create a simple test that doesn't require my changes to confirm the original behavior:
Assistant
Let me temporarily revert just the source changes to verify the original bug:
Assistant
Now let me run the test - the original code should fail to compile because IdleTimeoutHandler doesn't have the
cancelled()method:Assistant
Good - the test fails to compile because the accessors don't exist in the original code. Let me restore my source changes:
Assistant
Let me write a more focused reproduction test that doesn't rely on the new accessor (which is part of my fix):
Assistant
The reproduction test using reflection passes. Let me also verify the test fails on the original code:
Assistant
The original code doesn't have
idleTimeoutHandleras a field. This confirms the bug existed. Let me restore my changes:Assistant
Now let me clean up the reproduction test file and run all the tests one final time:
Assistant
All 131 tests pass. Let me also check if there are any related test modules that might be affected by this change:
Assistant
Only my test file uses
IdleTimeoutHandler. Let me also look at whether other modules use these classes:Assistant
Let me also check if there are any other tests in the webserver module that might be affected by this change:
Assistant
Let me run those tests too to make sure they all pass:
Assistant
All tests pass. Let me now also verify by looking at the tests one more time to make sure they're well-structured:
Assistant
Let me verify the final state of all my changes:
Assistant
Let me run the entire webserver test suite one more time to make sure everything is fine:
Assistant
All 131 tests pass. Let me also do a final verification by looking at the full diff one more time:
Assistant
Now let me also check if there are any leftover files or scripts I created that need to be cleaned up:
Assistant
Good - all temporary files are cleaned up. Let me now go through the checklist:
Checklist Review
Let me review the requirements again:
Assistant
Let me verify my changes by viewing the final ServerListener:
Assistant
The implementation looks complete. Now let me think about edge cases:
-
What if the test for
idleConnectionPeriodset to a small value causes real-time waits? Looking at the issue, it says: "The test should use a small explicit hook or test timer rather than relying on long real-time waits." My tests don't rely on real-time waits; they directly inspect the state of theIdleTimeoutHandler(handler.cancelled()). -
What if the WebServer builder is used with custom idleConnectionTimeout/Period? My tests work with the default config (30 seconds timeout, 10 seconds period, but that's irrelevant since we directly check the cancelled state).
-
What about thread safety? The field is volatile, and is idempotent. The volatile read in should see the updated value.
-
Assistant
Let me run the tests one final time to confirm everything works:
Assistant
All 131 tests pass. Let me provide my final summary:
Summary of Changes
Files Modified:
-
webserver/webserver/src/main/java/io/helidon/webserver/IdleTimeoutHandler.java- Added a
volatile boolean cancelledfield to track cancellation state. - Overrode
cancel()to track state and call super. - Added package-private
cancelled()method for tests.
- Added a
-
Assistant
Checklist Review
1. If an error was reported in the issue description, I have successfully reproduced it.
- DONE: I created a reproduction test using reflection that confirmed the original
ServerListenerdid not retain theIdleTimeoutHandleras a field. The original code created the handler as a local variable in the constructor, with no way to access or cancel it from or .
- DONE: I created a reproduction test using reflection that confirmed the original
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.