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 resetWriteTime… · netty/netty@2a66bf0

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
@@ -314,6 +314,7 @@ public void resetReadTimeout() {
314314
if (readerIdleTimeNanos > 0 || allIdleTimeNanos > 0) {
315315
lastReadTime = ticker.nanoTime();
316316
reading = false;
317+
firstReaderIdleEvent = firstAllIdleEvent = true;
317318
}
318319
}
319320

@@ -323,6 +324,7 @@ public void resetReadTimeout() {
323324
public void resetWriteTimeout() {
324325
if (writerIdleTimeNanos > 0 || allIdleTimeNanos > 0) {
325326
lastWriteTime = ticker.nanoTime();
327+
firstWriterIdleEvent = firstAllIdleEvent = true;
326328
}
327329
}
328330

Collapse file

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

Copy file name to clipboard
+143Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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.ChannelInboundHandlerAdapter;
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(
49+
false, 0L, 1L, 0L, TimeUnit.SECONDS);
50+
51+
final List<IdleStateEvent> events = new ArrayList<IdleStateEvent>();
52+
EmbeddedChannel channel = new EmbeddedChannel(idleStateHandler,
53+
new ChannelInboundHandlerAdapter() {
54+
@Override
55+
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
56+
if (evt instanceof IdleStateEvent) {
57+
events.add((IdleStateEvent) evt);
58+
}
59+
}
60+
});
61+
channel.freezeTime();
62+
63+
try {
64+
// first idle — expected
65+
channel.advanceTimeBy(1100L, TimeUnit.MILLISECONDS);
66+
channel.runPendingTasks();
67+
assertEquals(1, events.size());
68+
assertSame(IdleStateEvent.FIRST_WRITER_IDLE_STATE_EVENT, events.get(0),
69+
"First idle after connect should be FIRST_WRITER_IDLE_STATE_EVENT");
70+
71+
// second idle, no activity in between — non-first
72+
channel.advanceTimeBy(1100L, TimeUnit.MILLISECONDS);
73+
channel.runPendingTasks();
74+
assertEquals(2, events.size());
75+
assertSame(IdleStateEvent.WRITER_IDLE_STATE_EVENT, events.get(1),
76+
"Second idle without reset should be WRITER_IDLE_STATE_EVENT (first=false)");
77+
78+
// reset: tells the handler to treat this moment as a fresh start
79+
idleStateHandler.resetWriteTimeout();
80+
81+
// should fire as first again, but currently doesn't because
82+
// resetWriteTimeout() only updates lastWriteTime, not firstWriterIdleEvent
83+
channel.advanceTimeBy(1100L, TimeUnit.MILLISECONDS);
84+
channel.runPendingTasks();
85+
assertEquals(3, events.size());
86+
assertSame(IdleStateEvent.FIRST_WRITER_IDLE_STATE_EVENT, events.get(2),
87+
"After resetWriteTimeout(), next idle MUST be FIRST_WRITER_IDLE_STATE_EVENT. " +
88+
"Bug: firstWriterIdleEvent is not reset by resetWriteTimeout().");
89+
} finally {
90+
channel.finishAndReleaseAll();
91+
}
92+
}
93+
94+
/**
95+
* Same issue on the read side: resetReadTimeout() resets the clock but
96+
* leaves firstReaderIdleEvent as-is, so the next event stays non-first.
97+
*/
98+
@Test
99+
public void testResetReadTimeoutResetsFirstEventFlag() throws Exception {
100+
final IdleStateHandler idleStateHandler = new IdleStateHandler(
101+
false, 1L, 0L, 0L, TimeUnit.SECONDS);
102+
103+
final List<IdleStateEvent> events = new ArrayList<IdleStateEvent>();
104+
EmbeddedChannel channel = new EmbeddedChannel(idleStateHandler,
105+
new ChannelInboundHandlerAdapter() {
106+
@Override
107+
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
108+
if (evt instanceof IdleStateEvent) {
109+
events.add((IdleStateEvent) evt);
110+
}
111+
}
112+
});
113+
channel.freezeTime();
114+
115+
try {
116+
// first idle
117+
channel.advanceTimeBy(1100L, TimeUnit.MILLISECONDS);
118+
channel.runPendingTasks();
119+
assertEquals(1, events.size());
120+
assertSame(IdleStateEvent.FIRST_READER_IDLE_STATE_EVENT, events.get(0),
121+
"First idle after connect should be FIRST_READER_IDLE_STATE_EVENT");
122+
123+
// non-first idle
124+
channel.advanceTimeBy(1100L, TimeUnit.MILLISECONDS);
125+
channel.runPendingTasks();
126+
assertEquals(2, events.size());
127+
assertSame(IdleStateEvent.READER_IDLE_STATE_EVENT, events.get(1),
128+
"Second idle without reset should be READER_IDLE_STATE_EVENT (first=false)");
129+
130+
idleStateHandler.resetReadTimeout();
131+
132+
// should be first again after the reset
133+
channel.advanceTimeBy(1100L, TimeUnit.MILLISECONDS);
134+
channel.runPendingTasks();
135+
assertEquals(3, events.size());
136+
assertSame(IdleStateEvent.FIRST_READER_IDLE_STATE_EVENT, events.get(2),
137+
"After resetReadTimeout(), next idle MUST be FIRST_READER_IDLE_STATE_EVENT. " +
138+
"Bug: firstReaderIdleEvent is not reset by resetReadTimeout().");
139+
} finally {
140+
channel.finishAndReleaseAll();
141+
}
142+
}
143+
}

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.