Skip to content

Commit 3007ba9

Browse files
Use recommanded finalize chain pattern when override finalize() method (#16212)
According to the java docs of [Object.finalize()](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Object.html#finalize()), when override `finalize()` method, the `super.finalize()` should be placed in the `finally` block: > Finalizer invocations are not automatically chained, unlike constructors. If a subclass overrides finalize it must invoke the superclass finalizer explicitly. To guard against exceptions prematurely terminating the finalize chain, the subclass should use a try-finally block to ensure super.finalize() is always invoked. For example, ``` @OverRide protected void finalize() throws Throwable { try { ... // cleanup subclass state } finally { super.finalize(); } } ``` As we know, the [constructor chaining](https://docs.oracle.com/javase/tutorial/java/IandI/super.html) order is: First the superclass constructor, then the subclass constructor. The destruction order has not been explained well in java docs, but we can take C++ docs as reference: https://isocpp.org/wiki/faq/dtors#order-dtors-for-members So when doing finalize(), the order should be reversed: First the subclass, then the superclass. This pattern has also been stated in the book [<Effective Java, 2nd Edition>](https://www.oreilly.com/library/view/effective-java-2nd/9780137150021/)(Item 7): > You should finalize the subclass in a try block and invoke the superclass finalizer in the corresponding finally block. By doing so, it should make the code less error prone, for example: https://issues.apache.org/jira/browse/AXIS2-4163 Co-authored-by: lao <none>
1 parent b918042 commit 3007ba9

8 files changed

Lines changed: 28 additions & 16 deletions

File tree

buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,9 @@ long usedMemory() {
345345
@Override
346346
protected void finalize() throws Throwable {
347347
try {
348-
super.finalize();
349-
} finally {
350348
free();
349+
} finally {
350+
super.finalize();
351351
}
352352
}
353353

buffer/src/main/java/io/netty/buffer/PoolArena.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -640,10 +640,10 @@ private static void appendPoolSubPages(StringBuilder buf, PoolSubpage<?>[] subpa
640640
@Override
641641
protected final void finalize() throws Throwable {
642642
try {
643-
super.finalize();
644-
} finally {
645643
destroyPoolSubPages(smallSubpagePools);
646644
destroyPoolChunkLists(qInit, q000, q025, q050, q075, q100);
645+
} finally {
646+
super.finalize();
647647
}
648648
}
649649

buffer/src/main/java/io/netty/buffer/PoolThreadCache.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,15 +484,15 @@ private FreeOnFinalize(PoolThreadCache cache) {
484484
@Override
485485
protected void finalize() throws Throwable {
486486
try {
487-
super.finalize();
488-
} finally {
489487
PoolThreadCache cache = this.cache;
490488
// this can race with a non-finalizer thread calling free: regardless who wins, the cache will be
491489
// null out
492490
this.cache = null;
493491
if (cache != null) {
494492
cache.free(true);
495493
}
494+
} finally {
495+
super.finalize();
496496
}
497497
}
498498
}

common/src/main/java/io/netty/util/HashedWheelTimer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,13 +322,13 @@ public HashedWheelTimer(
322322
@Override
323323
protected void finalize() throws Throwable {
324324
try {
325-
super.finalize();
326-
} finally {
327325
// This object is going to be GCed and it is assumed the ship has sailed to do a proper shutdown. If
328326
// we have not yet shutdown then we want to make sure we decrement the active instance count.
329327
if (WORKER_STATE_UPDATER.getAndSet(this, WORKER_STATE_SHUTDOWN) != WORKER_STATE_SHUTDOWN) {
330328
INSTANCE_COUNTER.decrementAndGet();
331329
}
330+
} finally {
331+
super.finalize();
332332
}
333333
}
334334

common/src/test/java/io/netty/util/RecyclerFastThreadLocalTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,11 @@ public void run() {
5656
}) {
5757
@Override
5858
protected void finalize() throws Throwable {
59-
super.finalize();
60-
collected.set(true);
59+
try {
60+
collected.set(true);
61+
} finally {
62+
super.finalize();
63+
}
6164
}
6265
};
6366
assertFalse(collected.get());

common/src/test/java/io/netty/util/RecyclerTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,11 @@ public void run() {
185185
}) {
186186
@Override
187187
protected void finalize() throws Throwable {
188-
super.finalize();
189-
collected.set(true);
188+
try {
189+
collected.set(true);
190+
} finally {
191+
super.finalize();
192+
}
190193
}
191194
};
192195
assertFalse(collected.get());

handler/src/main/java/io/netty/handler/ssl/OpenSslContext.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ final SSLEngine newEngine0(ByteBufAllocator alloc, String peerHost, int peerPort
6060
@Override
6161
@SuppressWarnings("FinalizeDeclaration")
6262
protected final void finalize() throws Throwable {
63-
super.finalize();
64-
OpenSsl.releaseIfNeeded(this);
63+
try {
64+
OpenSsl.releaseIfNeeded(this);
65+
} finally {
66+
super.finalize();
67+
}
6568
}
6669
}

handler/src/main/java/io/netty/handler/ssl/OpenSslEngine.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ public final class OpenSslEngine extends ReferenceCountedOpenSslEngine {
3939
@Override
4040
@SuppressWarnings("FinalizeDeclaration")
4141
protected void finalize() throws Throwable {
42-
super.finalize();
43-
OpenSsl.releaseIfNeeded(this);
42+
try {
43+
OpenSsl.releaseIfNeeded(this);
44+
} finally {
45+
super.finalize();
46+
}
4447
}
4548
}

0 commit comments

Comments
 (0)