Repository: netty/netty
File: buffer/src/main/java/io/netty5/buffer/adapt/AdaptivePoolingAllocator.java (Java)

Code before:
        return () -> manager.allocateConstChild(constantBuffer);
    }

    @Override
    public void close() {
        closed = true;
        long magsExpandWriteLock = magazineExpandLock.writeLock();
        try {
            for (Magazine mag : magazines) {
                long writeLock = mag.writeLock();
                try {
                    mag.close();
                } finally {
                    mag.unlockWrite(writeLock);
                }
            }
        } finally {
            magazineExpandLock.unlockWrite(magsExpandWriteLock);
        }
        if (liveCachedMagazines != null) {
            liveCachedMagazines.forEach(mag -> {
                try {
                    mag.ownerEventExecutor.execute(() -> {
                        threadLocalMagazine.remove();
                        // this is to prevent that a subsequent request will try to create a new Magazine
                        threadLocalMagazine.set(NO_MAGAZINE);
                        drainCloseCentralQueue();
                    });
                } catch (Throwable ignore) {
                    // if we've been rejected here, it's likely the event executor will take care of remove it
                    // but if the event executor was running on an FastThreadLocalThread which doesn't remove it,
                    // we're in trouble :"(
                }
            });
        }
        drainCloseCentralQueue();
    }

    private void drainCloseCentralQueue() {
        Buffer curr;
        while ((curr = centralQueue.poll()) != null) {
            curr.close();
        }
    }

    private boolean offerToQueue(Buffer buffer) {
        if (!centralQueue.offer(buffer)) {
            return false;
        }
        if (closed) {
            drainCloseCentralQueue();

Review comment (suggestion):
We don't need to set this because there's a check on `closed` in `allocate()` before we touch the thread-local.

Code after:
        return () -> manager.allocateConstChild(constantBuffer);
    }

    @Override
    public void close() {
        closed = true;
        long magsExpandWriteLock = magazineExpandLock.writeLock();
        try {
            for (Magazine mag : magazines) {
                long writeLock = mag.writeLock();
                try {
                    mag.close();
                } finally {
                    mag.unlockWrite(writeLock);
                }
            }
        } finally {
            magazineExpandLock.unlockWrite(magsExpandWriteLock);
        }
        if (liveCachedMagazines != null) {
            liveCachedMagazines.forEach(mag -> {
                try {
                    mag.ownerEventExecutor.execute(() -> {
                        threadLocalMagazine.remove();
                        drainCloseCentralQueue();
                    });
                } catch (Throwable ignore) {
                    // if we've been rejected here, it's likely the event executor will take care of remove it
                    // but if the event executor was running on an FastThreadLocalThread which doesn't remove it,
                    // we're in trouble :"(
                }
            });
        }
        drainCloseCentralQueue();
    }

    private void drainCloseCentralQueue() {
        Buffer curr;
        while ((curr = centralQueue.poll()) != null) {
            curr.close();
        }
    }

    private boolean offerToQueue(Buffer buffer) {
        if (!centralQueue.offer(buffer)) {
            return false;
        }
        if (closed) {
            drainCloseCentralQueue();
        }
        return true;