GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — IdleStateHandler: reset firstWriter/ReaderIdleEvent in resetWriteTimeout/resetReadTimeout by husseinvr97 · Pull Request #16982 · netty/netty

IdleStateHandler: reset firstWriter/ReaderIdleEvent in resetWriteTimeout/resetReadTimeout #16982

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
chrisvest merged 1 commit into netty:4.2 from husseinvr97:fix/idle-state-handler-reset-first-event-flag
Jun 24, 2026
+145 −0
Merged

IdleStateHandler: reset firstWriter/ReaderIdleEvent in resetWriteTimeout/resetReadTimeout #16982

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
1 commit
Select commit
8b48bd2
IdleStateHandler: reset firstWriter/ReaderIdleEvent in resetWriteTime…
husseinvr97 Jun 24, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

2 changes: 2 additions & 0 deletions 2 handler/src/main/java/io/netty/handler/timeout/IdleStateHandler.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ public void resetReadTimeout() {
if (readerIdleTimeNanos > 0 || allIdleTimeNanos > 0) {
lastReadTime = ticker.nanoTime();
reading = false;
firstReaderIdleEvent = firstAllIdleEvent = true;
}
}

Expand All @@ -323,6 +324,7 @@ public void resetReadTimeout() {
public void resetWriteTimeout() {
if (writerIdleTimeNanos > 0 || allIdleTimeNanos > 0) {
lastWriteTime = ticker.nanoTime();
firstWriterIdleEvent = firstAllIdleEvent = true;
}
}

Expand Down
143 changes: 143 additions & 0 deletions 143 handler/src/test/java/io/netty/handler/timeout/IdleStateHandlerResetFlagTest.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* Copyright 2026 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package io.netty.handler.timeout;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.embedded.EmbeddedChannel;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;

/**
* Tests that {@link IdleStateHandler#resetWriteTimeout()} and
* {@link IdleStateHandler#resetReadTimeout()} correctly restore the
* "first idle" semantics after a non-first event has already fired.
*
* The reset methods update the timestamp but don't touch the firstWriter/ReaderIdleEvent
* flags, so the handler keeps reporting non-first events even after a reset.
* The writeListener path doesn't have this problem because it resets both.
*/
public class IdleStateHandlerResetFlagTest {

/**
* If a WRITER_IDLE event has already fired as non-first and then
* resetWriteTimeout() is called, the next event should be first again —
* same as if an actual write had happened.
*/
@Test
public void testResetWriteTimeoutResetsFirstEventFlag() throws Exception {
final IdleStateHandler idleStateHandler = new IdleStateHandler(
false, 0L, 1L, 0L, TimeUnit.SECONDS);

final List<IdleStateEvent> events = new ArrayList<IdleStateEvent>();
EmbeddedChannel channel = new EmbeddedChannel(idleStateHandler,
new ChannelInboundHandlerAdapter() {
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
if (evt instanceof IdleStateEvent) {
events.add((IdleStateEvent) evt);
}
}
});
channel.freezeTime();

try {
// first idle — expected
channel.advanceTimeBy(1100L, TimeUnit.MILLISECONDS);
channel.runPendingTasks();
assertEquals(1, events.size());
assertSame(IdleStateEvent.FIRST_WRITER_IDLE_STATE_EVENT, events.get(0),
"First idle after connect should be FIRST_WRITER_IDLE_STATE_EVENT");

// second idle, no activity in between — non-first
channel.advanceTimeBy(1100L, TimeUnit.MILLISECONDS);
channel.runPendingTasks();
assertEquals(2, events.size());
assertSame(IdleStateEvent.WRITER_IDLE_STATE_EVENT, events.get(1),
"Second idle without reset should be WRITER_IDLE_STATE_EVENT (first=false)");

// reset: tells the handler to treat this moment as a fresh start
idleStateHandler.resetWriteTimeout();

// should fire as first again, but currently doesn't because
// resetWriteTimeout() only updates lastWriteTime, not firstWriterIdleEvent
channel.advanceTimeBy(1100L, TimeUnit.MILLISECONDS);
channel.runPendingTasks();
assertEquals(3, events.size());
assertSame(IdleStateEvent.FIRST_WRITER_IDLE_STATE_EVENT, events.get(2),
"After resetWriteTimeout(), next idle MUST be FIRST_WRITER_IDLE_STATE_EVENT. " +
"Bug: firstWriterIdleEvent is not reset by resetWriteTimeout().");
} finally {
channel.finishAndReleaseAll();
}
}

/**
* Same issue on the read side: resetReadTimeout() resets the clock but
* leaves firstReaderIdleEvent as-is, so the next event stays non-first.
*/
@Test
public void testResetReadTimeoutResetsFirstEventFlag() throws Exception {
final IdleStateHandler idleStateHandler = new IdleStateHandler(
false, 1L, 0L, 0L, TimeUnit.SECONDS);

final List<IdleStateEvent> events = new ArrayList<IdleStateEvent>();
EmbeddedChannel channel = new EmbeddedChannel(idleStateHandler,
new ChannelInboundHandlerAdapter() {
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
if (evt instanceof IdleStateEvent) {
events.add((IdleStateEvent) evt);
}
}
});
channel.freezeTime();

try {
// first idle
channel.advanceTimeBy(1100L, TimeUnit.MILLISECONDS);
channel.runPendingTasks();
assertEquals(1, events.size());
assertSame(IdleStateEvent.FIRST_READER_IDLE_STATE_EVENT, events.get(0),
"First idle after connect should be FIRST_READER_IDLE_STATE_EVENT");

// non-first idle
channel.advanceTimeBy(1100L, TimeUnit.MILLISECONDS);
channel.runPendingTasks();
assertEquals(2, events.size());
assertSame(IdleStateEvent.READER_IDLE_STATE_EVENT, events.get(1),
"Second idle without reset should be READER_IDLE_STATE_EVENT (first=false)");

idleStateHandler.resetReadTimeout();

// should be first again after the reset
channel.advanceTimeBy(1100L, TimeUnit.MILLISECONDS);
channel.runPendingTasks();
assertEquals(3, events.size());
assertSame(IdleStateEvent.FIRST_READER_IDLE_STATE_EVENT, events.get(2),
"After resetReadTimeout(), next idle MUST be FIRST_READER_IDLE_STATE_EVENT. " +
"Bug: firstReaderIdleEvent is not reset by resetReadTimeout().");
} finally {
channel.finishAndReleaseAll();
}
}
}
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.