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 4.1: Avoid classloader leak via GlobalEventExecutor termina… · lltopk/netty@76faf2c

File tree

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

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

Copy file name to clipboardExpand all lines: common/src/main/java/io/netty/util/concurrent/GlobalEventExecutor.java
+22-3Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,8 @@ private GlobalEventExecutor() {
8888
threadFactory = ThreadExecutorMap.apply(new DefaultThreadFactory(
8989
DefaultThreadFactory.toPoolName(getClass()), false, Thread.NORM_PRIORITY, null), this);
9090

91-
UnsupportedOperationException terminationFailure = new UnsupportedOperationException();
92-
ThrowableUtil.unknownStackTrace(terminationFailure, GlobalEventExecutor.class, "terminationFuture");
93-
terminationFuture = new FailedFuture<Object>(this, terminationFailure);
91+
terminationFuture = new FailedFuture<Object>(this,
92+
StacklessUnsupportedOperationException.newInstance(GlobalEventExecutor.class, "terminationFuture"));
9493
}
9594

9695
/**
@@ -325,4 +324,24 @@ public void run() {
325324
}
326325
}
327326
}
327+
328+
private static final class StacklessUnsupportedOperationException extends UnsupportedOperationException {
329+
330+
private static final long serialVersionUID = -8060232216137960173L;
331+
332+
private StacklessUnsupportedOperationException() { }
333+
334+
// Override fillInStackTrace() so we not populate the backtrace via a native call and so leak the
335+
// Classloader. As the GlobalEventExecutor.INSTANCE is a singleton and holds on to this exception via its
336+
// terminationFuture, a populated backtrace would pin the Classloader of whatever thread happened to trigger
337+
// the lazy initialization of INSTANCE (see https://github.com/netty/netty/issues/17128).
338+
@Override
339+
public Throwable fillInStackTrace() {
340+
return this;
341+
}
342+
343+
static StacklessUnsupportedOperationException newInstance(Class<?> clazz, String method) {
344+
return ThrowableUtil.unknownStackTrace(new StacklessUnsupportedOperationException(), clazz, method);
345+
}
346+
}
328347
}
Collapse file

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

Copy file name to clipboardExpand all lines: common/src/test/java/io/netty/util/concurrent/GlobalEventExecutorTest.java
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.concurrent.atomic.AtomicBoolean;
2525
import java.util.concurrent.atomic.AtomicReference;
2626

27+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
2728
import static org.junit.jupiter.api.Assertions.assertEquals;
2829
import static org.junit.jupiter.api.Assertions.assertNotNull;
2930
import static org.junit.jupiter.api.Assertions.assertNotSame;
@@ -156,6 +157,26 @@ public void run() {
156157
assertTrue(t.ran.get());
157158
}
158159

160+
@Test
161+
public void testTerminationFutureFailureDoesNotFillInStackTrace() {
162+
// The GlobalEventExecutor.INSTANCE is a singleton that lives for the lifetime of the Classloader that
163+
// loaded it. It holds on to the failure of its terminationFuture forever, so that failure must not
164+
// populate a (native) backtrace: doing so would pin the Classloader of whatever thread happened to
165+
// trigger the lazy initialization of INSTANCE (see https://github.com/netty/netty/issues/17128).
166+
Throwable cause = e.terminationFuture().cause();
167+
assertNotNull(cause);
168+
assertTrue(cause instanceof UnsupportedOperationException);
169+
170+
StackTraceElement[] before = cause.getStackTrace();
171+
assertEquals(1, before.length);
172+
assertEquals(GlobalEventExecutor.class.getName(), before[0].getClassName());
173+
assertEquals("terminationFuture", before[0].getMethodName());
174+
175+
// fillInStackTrace() must be a no-op; otherwise it would repopulate the backtrace with native frames.
176+
cause.fillInStackTrace();
177+
assertArrayEquals(before, cause.getStackTrace());
178+
}
179+
159180
private static final class TestRunnable implements Runnable {
160181
final AtomicBoolean ran = new AtomicBoolean();
161182
final long delay;

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.