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 — Fix race condition in NonStickyEventExecutorGroup causing incorrect i… · netty/netty@6771ac8

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • common/src
    • main/java/io/netty/util/concurrent
      • NonStickyEventExecutorGroup.java
    • test/java/io/netty/util/concurrent
      • NonStickyEventExecutorGroupTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎common/src/main/java/io/netty/util/concurrent/NonStickyEventExecutorGroup.java‎

Copy file name to clipboardExpand all lines: common/src/main/java/io/netty/util/concurrent/NonStickyEventExecutorGroup.java
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,8 @@ public void run() {
258258
executor.execute(this);
259259
return; // done
260260
} catch (Throwable ignore) {
261+
// Restore executingThread since we're continuing to execute tasks.
262+
executingThread.set(current);
261263
// Reset the state back to running as we will keep on executing tasks.
262264
state.set(RUNNING);
263265
// if an error happened we should just ignore it and let the loop run again as there is not
Collapse file

‎common/src/test/java/io/netty/util/concurrent/NonStickyEventExecutorGroupTest.java‎

Copy file name to clipboardExpand all lines: common/src/test/java/io/netty/util/concurrent/NonStickyEventExecutorGroupTest.java
+151Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,17 @@
2424

2525
import java.util.ArrayList;
2626
import java.util.Collection;
27+
import java.util.Collections;
28+
import java.util.Iterator;
2729
import java.util.List;
2830
import java.util.concurrent.CountDownLatch;
31+
import java.util.concurrent.RejectedExecutionException;
2932
import java.util.concurrent.TimeUnit;
3033
import java.util.concurrent.atomic.AtomicInteger;
3134
import java.util.concurrent.atomic.AtomicReference;
3235

3336
import static org.junit.jupiter.api.Assertions.assertFalse;
37+
import static org.junit.jupiter.api.Assertions.assertNotNull;
3438
import static org.junit.jupiter.api.Assertions.assertThrows;
3539
import static org.junit.jupiter.api.Assertions.assertTrue;
3640

@@ -129,6 +133,153 @@ public void run() {
129133
}
130134
}
131135

136+
@Test
137+
public void testInEventLoopAfterReschedulingFailure() throws Exception {
138+
final UnorderedThreadPoolEventExecutor underlying = new UnorderedThreadPoolEventExecutor(1);
139+
final AtomicInteger executeCount = new AtomicInteger();
140+
141+
final EventExecutorGroup wrapper = new AbstractEventExecutorGroup() {
142+
@Override
143+
public void shutdown() {
144+
shutdownGracefully();
145+
}
146+
147+
private final EventExecutor executor = new AbstractEventExecutor(this) {
148+
@Override
149+
public boolean inEventLoop(Thread thread) {
150+
return underlying.inEventLoop(thread);
151+
}
152+
153+
@Override
154+
public void shutdown() {
155+
shutdownGracefully();
156+
}
157+
158+
@Override
159+
public void execute(Runnable command) {
160+
// Reject the 2nd execute() call (the reschedule attempt)
161+
// 1st call: initial task submission
162+
// 2nd call: reschedule after maxTaskExecutePerRun
163+
if (executeCount.incrementAndGet() == 2) {
164+
throw new RejectedExecutionException("Simulated queue full");
165+
}
166+
underlying.execute(command);
167+
}
168+
169+
@Override
170+
public boolean isShuttingDown() {
171+
return underlying.isShuttingDown();
172+
}
173+
174+
@Override
175+
public Future<?> shutdownGracefully(long quietPeriod, long timeout, TimeUnit unit) {
176+
return underlying.shutdownGracefully(quietPeriod, timeout, unit);
177+
}
178+
179+
@Override
180+
public Future<?> terminationFuture() {
181+
return underlying.terminationFuture();
182+
}
183+
184+
@Override
185+
public boolean isShutdown() {
186+
return underlying.isShutdown();
187+
}
188+
189+
@Override
190+
public boolean isTerminated() {
191+
return underlying.isTerminated();
192+
}
193+
194+
@Override
195+
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
196+
return underlying.awaitTermination(timeout, unit);
197+
}
198+
};
199+
200+
@Override
201+
public EventExecutor next() {
202+
return executor;
203+
}
204+
205+
@Override
206+
public Iterator<EventExecutor> iterator() {
207+
return Collections.singletonList(executor).iterator();
208+
}
209+
210+
@Override
211+
public boolean isShuttingDown() {
212+
return underlying.isShuttingDown();
213+
}
214+
215+
@Override
216+
public Future<?> shutdownGracefully(long quietPeriod, long timeout, TimeUnit unit) {
217+
return underlying.shutdownGracefully(quietPeriod, timeout, unit);
218+
}
219+
220+
@Override
221+
public Future<?> terminationFuture() {
222+
return underlying.terminationFuture();
223+
}
224+
225+
@Override
226+
public boolean isShutdown() {
227+
return underlying.isShutdown();
228+
}
229+
230+
@Override
231+
public boolean isTerminated() {
232+
return underlying.isTerminated();
233+
}
234+
235+
@Override
236+
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
237+
return underlying.awaitTermination(timeout, unit);
238+
}
239+
};
240+
241+
// Use maxTaskExecutePerRun=1 so reschedule happens after first task
242+
NonStickyEventExecutorGroup nonStickyGroup = new NonStickyEventExecutorGroup(wrapper, 1);
243+
244+
try {
245+
final EventExecutor executor = nonStickyGroup.next();
246+
247+
final CountDownLatch latch = new CountDownLatch(1);
248+
final AtomicReference<Boolean> inEventLoopResult = new AtomicReference<Boolean>();
249+
250+
// Submit 2 tasks:
251+
// Task 1: completes, triggers reschedule which will be rejected
252+
// Task 2: verifies inEventLoop() still works after failed reschedule
253+
executor.execute(new Runnable() {
254+
@Override
255+
public void run() {
256+
// First task - will trigger reschedule attempt that fails
257+
}
258+
});
259+
260+
executor.execute(new Runnable() {
261+
@Override
262+
public void run() {
263+
// This runs AFTER the failed rescheduling
264+
// WITHOUT line 262 fix: executingThread is null, inEventLoop() returns false
265+
// WITH line 262 fix: executingThread restored, inEventLoop() returns true
266+
inEventLoopResult.set(executor.inEventLoop());
267+
latch.countDown();
268+
}
269+
});
270+
271+
assertTrue(latch.await(5, TimeUnit.SECONDS), "Tasks should complete");
272+
Boolean result = inEventLoopResult.get();
273+
assertNotNull(result, "inEventLoop() should have been called");
274+
assertTrue(result,
275+
"inEventLoop() should return true even after failed reschedule attempt. " +
276+
"This indicates executingThread was properly restored in the exception handler.");
277+
} finally {
278+
nonStickyGroup.shutdownGracefully();
279+
underlying.shutdownGracefully();
280+
}
281+
}
282+
132283
private static void execute(EventExecutorGroup group, CountDownLatch startLatch) throws Throwable {
133284
final EventExecutor executor = group.next();
134285
assertTrue(executor instanceof OrderedEventExecutor);

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.