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 — Auto-port 5.0: IdleStateHandler: reset firstWriter/ReaderIdleEvent in… · netty/netty@7922bdd

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • handler/src
    • main/java/io/netty/handler/timeout
      • IdleStateHandler.java
    • test/java/io/netty/handler/timeout
      • IdleStateHandlerResetFlagTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎handler/src/main/java/io/netty/handler/timeout/IdleStateHandler.java‎

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/timeout/IdleStateHandler.java
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ public void resetReadTimeout() {
300300
if (readerIdleTimeNanos > 0 || allIdleTimeNanos > 0) {
301301
lastReadTime = ticker.nanoTime();
302302
reading = false;
303+
firstReaderIdleEvent = firstAllIdleEvent = true;
303304
}
304305
}
305306

@@ -309,6 +310,7 @@ public void resetReadTimeout() {
309310
public void resetWriteTimeout() {
310311
if (writerIdleTimeNanos > 0 || allIdleTimeNanos > 0) {
311312
lastWriteTime = ticker.nanoTime();
313+
firstWriterIdleEvent = firstAllIdleEvent = true;
312314
}
313315
}
314316

Collapse file

‎handler/src/test/java/io/netty/handler/timeout/IdleStateHandlerResetFlagTest.java‎

Copy file name to clipboard
+141Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/*
2+
* Copyright 2026 The Netty Project
3+
*
4+
* The Netty Project licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in
6+
* compliance with the License. You may obtain a copy of the License at:
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13+
* implied. See the License for the specific language governing
14+
* permissions and limitations under the License.
15+
*/
16+
package io.netty.handler.timeout;
17+
18+
import io.netty.channel.ChannelHandlerContext;
19+
import io.netty.channel.ChannelInboundHandler;
20+
import io.netty.channel.embedded.EmbeddedChannel;
21+
import org.junit.jupiter.api.Test;
22+
23+
import java.util.ArrayList;
24+
import java.util.List;
25+
import java.util.concurrent.TimeUnit;
26+
27+
import static org.junit.jupiter.api.Assertions.assertEquals;
28+
import static org.junit.jupiter.api.Assertions.assertSame;
29+
30+
/**
31+
* Tests that {@link IdleStateHandler#resetWriteTimeout()} and
32+
* {@link IdleStateHandler#resetReadTimeout()} correctly restore the
33+
* "first idle" semantics after a non-first event has already fired.
34+
*
35+
* The reset methods update the timestamp but don't touch the firstWriter/ReaderIdleEvent
36+
* flags, so the handler keeps reporting non-first events even after a reset.
37+
* The writeListener path doesn't have this problem because it resets both.
38+
*/
39+
public class IdleStateHandlerResetFlagTest {
40+
41+
/**
42+
* If a WRITER_IDLE event has already fired as non-first and then
43+
* resetWriteTimeout() is called, the next event should be first again —
44+
* same as if an actual write had happened.
45+
*/
46+
@Test
47+
public void testResetWriteTimeoutResetsFirstEventFlag() throws Exception {
48+
final IdleStateHandler idleStateHandler = new IdleStateHandler(0L, 1L, 0L, TimeUnit.SECONDS);
49+
50+
final List<IdleStateEvent> events = new ArrayList<IdleStateEvent>();
51+
EmbeddedChannel channel = new EmbeddedChannel(idleStateHandler,
52+
new ChannelInboundHandler() {
53+
@Override
54+
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
55+
if (evt instanceof IdleStateEvent) {
56+
events.add((IdleStateEvent) evt);
57+
}
58+
}
59+
});
60+
channel.freezeTime();
61+
62+
try {
63+
// first idle — expected
64+
channel.advanceTimeBy(1100L, TimeUnit.MILLISECONDS);
65+
channel.runPendingTasks();
66+
assertEquals(1, events.size());
67+
assertSame(IdleStateEvent.FIRST_WRITER_IDLE_STATE_EVENT, events.get(0),
68+
"First idle after connect should be FIRST_WRITER_IDLE_STATE_EVENT");
69+
70+
// second idle, no activity in between — non-first
71+
channel.advanceTimeBy(1100L, TimeUnit.MILLISECONDS);
72+
channel.runPendingTasks();
73+
assertEquals(2, events.size());
74+
assertSame(IdleStateEvent.WRITER_IDLE_STATE_EVENT, events.get(1),
75+
"Second idle without reset should be WRITER_IDLE_STATE_EVENT (first=false)");
76+
77+
// reset: tells the handler to treat this moment as a fresh start
78+
idleStateHandler.resetWriteTimeout();
79+
80+
// should fire as first again, but currently doesn't because
81+
// resetWriteTimeout() only updates lastWriteTime, not firstWriterIdleEvent
82+
channel.advanceTimeBy(1100L, TimeUnit.MILLISECONDS);
83+
channel.runPendingTasks();
84+
assertEquals(3, events.size());
85+
assertSame(IdleStateEvent.FIRST_WRITER_IDLE_STATE_EVENT, events.get(2),
86+
"After resetWriteTimeout(), next idle MUST be FIRST_WRITER_IDLE_STATE_EVENT. " +
87+
"Bug: firstWriterIdleEvent is not reset by resetWriteTimeout().");
88+
} finally {
89+
channel.finishAndReleaseAll();
90+
}
91+
}
92+
93+
/**
94+
* Same issue on the read side: resetReadTimeout() resets the clock but
95+
* leaves firstReaderIdleEvent as-is, so the next event stays non-first.
96+
*/
97+
@Test
98+
public void testResetReadTimeoutResetsFirstEventFlag() throws Exception {
99+
final IdleStateHandler idleStateHandler = new IdleStateHandler(1L, 0L, 0L, TimeUnit.SECONDS);
100+
101+
final List<IdleStateEvent> events = new ArrayList<IdleStateEvent>();
102+
EmbeddedChannel channel = new EmbeddedChannel(idleStateHandler,
103+
new ChannelInboundHandler() {
104+
@Override
105+
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
106+
if (evt instanceof IdleStateEvent) {
107+
events.add((IdleStateEvent) evt);
108+
}
109+
}
110+
});
111+
channel.freezeTime();
112+
113+
try {
114+
// first idle
115+
channel.advanceTimeBy(1100L, TimeUnit.MILLISECONDS);
116+
channel.runPendingTasks();
117+
assertEquals(1, events.size());
118+
assertSame(IdleStateEvent.FIRST_READER_IDLE_STATE_EVENT, events.get(0),
119+
"First idle after connect should be FIRST_READER_IDLE_STATE_EVENT");
120+
121+
// non-first idle
122+
channel.advanceTimeBy(1100L, TimeUnit.MILLISECONDS);
123+
channel.runPendingTasks();
124+
assertEquals(2, events.size());
125+
assertSame(IdleStateEvent.READER_IDLE_STATE_EVENT, events.get(1),
126+
"Second idle without reset should be READER_IDLE_STATE_EVENT (first=false)");
127+
128+
idleStateHandler.resetReadTimeout();
129+
130+
// should be first again after the reset
131+
channel.advanceTimeBy(1100L, TimeUnit.MILLISECONDS);
132+
channel.runPendingTasks();
133+
assertEquals(3, events.size());
134+
assertSame(IdleStateEvent.FIRST_READER_IDLE_STATE_EVENT, events.get(2),
135+
"After resetReadTimeout(), next idle MUST be FIRST_READER_IDLE_STATE_EVENT. " +
136+
"Bug: firstReaderIdleEvent is not reset by resetReadTimeout().");
137+
} finally {
138+
channel.finishAndReleaseAll();
139+
}
140+
}
141+
}

0 commit comments

Comments
0 (0)
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.