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 — Test cleanup list deallocation on thread death and finalization · netty/netty@64369d4

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • buffer/src
    • main/java/io/netty/buffer
      • AdaptivePoolingAllocator.java
    • test/java/io/netty/buffer
      • AdaptiveByteBufAllocatorTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java
+20-8Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
* This allows the allocator to quickly respond to changes in the application workload,
7878
* without suffering undue overhead from maintaining its statistics.
7979
* <p>
80-
* Since magazines are "relatively thread-local", the allocator has a central chunk cache that allow excess chunks
80+
* Since magazines are "relatively thread-local", the allocator has a central chunk cache that allows excess chunks
8181
* from any magazine to be shared with other magazines.
8282
*/
8383
@UnstableApi
@@ -477,11 +477,11 @@ private void free() {
477477
}
478478
}
479479

480-
private interface ChunkCache {
481-
Chunk pollChunk(int size);
482-
boolean offerChunk(Chunk chunk);
483-
void freeChunks();
484-
default void notifyReady(Chunk chunk) { }
480+
private abstract static class ChunkCache {
481+
abstract Chunk pollChunk(int size);
482+
abstract boolean offerChunk(Chunk chunk);
483+
abstract void freeChunks();
484+
void notifyReady(Chunk chunk) { }
485485
}
486486

487487
/**
@@ -498,7 +498,7 @@ default void notifyReady(Chunk chunk) { }
498498
* using a sentinel terminal node. Tracks all chunks for ownerThread/finalize cleanup.
499499
* Push-once per chunk lifetime, guarded by the {@link SizeClassedChunk#nextFree} link pointer.
500500
*/
501-
private static final class ParkingLotChunkCache implements ChunkCache {
501+
private static final class ParkingLotChunkCache extends ChunkCache {
502502
@SuppressWarnings("rawtypes")
503503
private static final AtomicReferenceFieldUpdater<ParkingLotChunkCache, SizeClassedChunk> READY_HEAD =
504504
AtomicReferenceFieldUpdater.newUpdater(
@@ -539,6 +539,9 @@ public boolean offerChunk(Chunk chunk) {
539539
return true;
540540
}
541541

542+
// Push-once: the intrusive nextFree link pointer is the insertion guard.
543+
// No concurrent registerToFree on the same chunk — magazine ownership
544+
// guarantees single-writer per chunk per lifecycle.
542545
private void registerToFree(SizeClassedChunk chunk) {
543546
if (chunk.nextFree != null) {
544547
return;
@@ -563,6 +566,11 @@ public void freeChunks() {
563566
}
564567
}
565568

569+
// Called exactly once from the dying owner thread (FastThreadLocal removal).
570+
// No concurrent registerToFree — only the owner thread calls offerChunk.
571+
// The first-segment-return vs markToDeallocate race is safe: markToDeallocate
572+
// uses CAS (handles both AVAILABLE and AVAILABLE_ARMED), and notification CAS
573+
// on the same state variable ensures only one wins.
566574
private void freeChunksThreadLocal() {
567575
SizeClassedChunk chunk = cleanupHead;
568576
while (chunk != SizeClassedChunk.CLEANUP_SENTINEL) {
@@ -588,6 +596,9 @@ private SizeClassedChunk drainReadyThreadLocal() {
588596
return chunk;
589597
}
590598

599+
// freeChunksShared is called exactly once, from finalize(). The allocator must be
600+
// unreachable for finalize to run, so no concurrent offerChunk/registerToFree calls.
601+
// No null guard needed on the getAndSet result.
591602
private void freeChunksShared() {
592603
SizeClassedChunk chunk = CLEANUP_HEAD.getAndSet(this, null);
593604
while (chunk != SizeClassedChunk.CLEANUP_SENTINEL) {
@@ -618,7 +629,7 @@ public void notifyReady(Chunk chunk) {
618629
}
619630
}
620631

621-
private static final class ConcurrentSkipListChunkCache implements ChunkCache {
632+
private static final class ConcurrentSkipListChunkCache extends ChunkCache {
622633
private final ConcurrentSkipListIntObjMultimap<Chunk> chunks;
623634

624635
private ConcurrentSkipListChunkCache() {
@@ -1744,6 +1755,7 @@ static final class AdaptiveByteBuf extends AbstractReferenceCountedByteBuf {
17441755
private AbstractByteBuf rootParent;
17451756
Chunk chunk;
17461757
private int length;
1758+
17471759
private int maxFastCapacity;
17481760
private ByteBuffer tmpNioBuf;
17491761
private boolean hasArray;
Collapse file

‎buffer/src/test/java/io/netty/buffer/AdaptiveByteBufAllocatorTest.java‎

Copy file name to clipboardExpand all lines: buffer/src/test/java/io/netty/buffer/AdaptiveByteBufAllocatorTest.java
-55Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -435,61 +435,6 @@ void benchmarkPatternChunkReuse() throws Exception {
435435
}
436436
}
437437

438-
@Test
439-
void chunkDeallocatesWhenNeverOfferedToCache() throws Exception {
440-
// A chunk that is never exhausted (never offered to the cache, never registered
441-
// in the cleanup list) must still deallocate correctly when the magazine is freed
442-
// (thread death) and all its segments are returned by external threads.
443-
AdaptiveByteBufAllocator allocator = newAllocator(false);
444-
long chunkSize = 128 * 1024; // MIN_CHUNK_SIZE
445-
446-
// Allocate a few buffers — NOT enough to exhaust the chunk.
447-
// The chunk stays as 'current' in the magazine and is never offered to the cache.
448-
int buffersPerChunk = (int) (chunkSize / 256);
449-
int buffersToAllocate = Math.max(2, buffersPerChunk / 4);
450-
List<ByteBuf> bufs = new ArrayList<>();
451-
452-
AtomicReference<Throwable> error = new AtomicReference<>();
453-
io.netty.util.concurrent.EventExecutor executor =
454-
io.netty.util.concurrent.ImmediateEventExecutor.INSTANCE;
455-
Runnable task = () -> {
456-
try {
457-
for (int i = 0; i < buffersToAllocate; i++) {
458-
bufs.add(allocator.heapBuffer(256));
459-
}
460-
} catch (Throwable t) {
461-
error.set(t);
462-
}
463-
};
464-
// Thread-local path: FastThreadLocalThread + EventExecutor mapping.
465-
// When the thread dies, the magazine is freed and markToDeallocate is
466-
// called directly on the chunk (never registered in cleanup list).
467-
Runnable mapped = io.netty.util.internal.ThreadExecutorMap.apply(task, executor);
468-
Thread ftlt = new io.netty.util.concurrent.FastThreadLocalThread(mapped);
469-
ftlt.start();
470-
ftlt.join();
471-
if (error.get() != null) {
472-
fail("Allocation failed", error.get());
473-
}
474-
475-
assertEquals(buffersToAllocate, bufs.size());
476-
477-
// Memory is still held — chunk is markToDeallocate'd but segments are outstanding.
478-
long memoryBeforeRelease = allocator.usedHeapMemory();
479-
assertTrue(memoryBeforeRelease >= chunkSize,
480-
"Chunk should still be tracked: " + memoryBeforeRelease);
481-
482-
// Release all buffers from the main thread (external returns).
483-
// Each releaseSegment goes through releaseSegmentExternal → deallocateIfNeeded.
484-
for (ByteBuf buf : bufs) {
485-
buf.release();
486-
}
487-
488-
// After all segments returned, the chunk should be fully deallocated.
489-
assertEquals(0, allocator.usedHeapMemory(),
490-
"Chunk should be deallocated after all segments returned");
491-
}
492-
493438
private static void shuffle(SplittableRandom rng, Object array) {
494439
int len = Array.getLength(array);
495440
for (int i = 0; i < len; i++) {

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.