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 concurrent chunk data write bug in adaptive allocator (#15652) · netty/netty@888666f

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • buffer/src
    • main/java/io/netty/buffer
      • AdaptiveByteBufAllocator.java
      • AdaptivePoolingAllocator.java
      • PooledByteBufAllocator.java
      • UnpooledByteBufAllocator.java
      • UnpooledDirectByteBuf.java
      • UnpooledUnsafeDirectByteBuf.java
      • UnpooledUnsafeNoCleanerDirectByteBuf.java
      • UnsafeByteBufUtil.java
    • test/java/io/netty/buffer
      • AbstractByteBufTest.java
      • BigEndianUnsafeNoCleanerDirectByteBufTest.java
      • LittleEndianUnsafeNoCleanerDirectByteBufTest.java
  • microbench/src/main/java/io/netty/buffer
    • AbstractByteBufNoCleanerBenchmark.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: buffer/src/main/java/io/netty/buffer/AdaptiveByteBufAllocator.java
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ private DirectChunkAllocator(ByteBufAllocator allocator) {
125125
@Override
126126
public AbstractByteBuf allocate(int initialCapacity, int maxCapacity) {
127127
return PlatformDependent.hasUnsafe() ?
128-
UnsafeByteBufUtil.newUnsafeDirectByteBuf(allocator, initialCapacity, maxCapacity) :
129-
new UnpooledDirectByteBuf(allocator, initialCapacity, maxCapacity);
128+
UnsafeByteBufUtil.newUnsafeDirectByteBuf(allocator, initialCapacity, maxCapacity, false) :
129+
new UnpooledDirectByteBuf(allocator, initialCapacity, maxCapacity, false);
130130
}
131131
}
132132
}
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
+4-6Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -543,9 +543,9 @@ private static final class SizeClassChunkControllerFactory implements ChunkContr
543543

544544
private SizeClassChunkControllerFactory(int segmentSize) {
545545
this.segmentSize = ObjectUtil.checkPositive(segmentSize, "segmentSize");
546-
this.chunkSize = Math.max(MIN_CHUNK_SIZE, segmentSize * MIN_SEGMENTS_PER_CHUNK);
546+
chunkSize = Math.max(MIN_CHUNK_SIZE, segmentSize * MIN_SEGMENTS_PER_CHUNK);
547547
int segmentsCount = chunkSize / segmentSize;
548-
this.segmentOffsets = new int[segmentsCount];
548+
segmentOffsets = new int[segmentsCount];
549549
for (int i = 0; i < segmentsCount; i++) {
550550
segmentOffsets[i] = i * segmentSize;
551551
}
@@ -1770,8 +1770,7 @@ private int setCharSequence0(int index, CharSequence sequence, Charset charset,
17701770
} else {
17711771
checkIndex(index, length);
17721772
}
1773-
// Directly pass in the rootParent() with the adjusted index
1774-
return ByteBufUtil.writeUtf8(rootParent(), idx(index), length, sequence, sequence.length());
1773+
return ByteBufUtil.writeUtf8(this, index, length, sequence, sequence.length());
17751774
}
17761775
if (charset.equals(CharsetUtil.US_ASCII) || charset.equals(CharsetUtil.ISO_8859_1)) {
17771776
int length = sequence.length();
@@ -1781,8 +1780,7 @@ private int setCharSequence0(int index, CharSequence sequence, Charset charset,
17811780
} else {
17821781
checkIndex(index, length);
17831782
}
1784-
// Directly pass in the rootParent() with the adjusted index
1785-
return ByteBufUtil.writeAscii(rootParent(), idx(index), sequence, length);
1783+
return ByteBufUtil.writeAscii(this, index, sequence, length);
17861784
}
17871785
byte[] bytes = sequence.toString().getBytes(charset);
17881786
if (expand) {
Collapse file

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

Copy file name to clipboardExpand all lines: buffer/src/main/java/io/netty/buffer/PooledByteBufAllocator.java
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,8 +403,8 @@ protected ByteBuf newDirectBuffer(int initialCapacity, int maxCapacity) {
403403
buf = directArena.allocate(cache, initialCapacity, maxCapacity);
404404
} else {
405405
buf = PlatformDependent.hasUnsafe() ?
406-
UnsafeByteBufUtil.newUnsafeDirectByteBuf(this, initialCapacity, maxCapacity) :
407-
new UnpooledDirectByteBuf(this, initialCapacity, maxCapacity);
406+
UnsafeByteBufUtil.newUnsafeDirectByteBuf(this, initialCapacity, maxCapacity, true) :
407+
new UnpooledDirectByteBuf(this, initialCapacity, maxCapacity, true);
408408
}
409409

410410
return toLeakAwareBuffer(buf);
Collapse file

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

Copy file name to clipboardExpand all lines: buffer/src/main/java/io/netty/buffer/UnpooledByteBufAllocator.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ private static final class InstrumentedUnpooledUnsafeNoCleanerDirectByteBuf
178178
extends UnpooledUnsafeNoCleanerDirectByteBuf {
179179
InstrumentedUnpooledUnsafeNoCleanerDirectByteBuf(
180180
UnpooledByteBufAllocator alloc, int initialCapacity, int maxCapacity) {
181-
super(alloc, initialCapacity, maxCapacity);
181+
super(alloc, initialCapacity, maxCapacity, true);
182182
}
183183

184184
@Override
Collapse file

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

Copy file name to clipboardExpand all lines: buffer/src/main/java/io/netty/buffer/UnpooledDirectByteBuf.java
+34-23Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public class UnpooledDirectByteBuf extends AbstractReferenceCountedByteBuf {
4343
private ByteBuffer tmpNioBuf;
4444
private int capacity;
4545
private boolean doNotFree;
46+
private final boolean allowSectionedInternalNioBufferAccess;
4647

4748
/**
4849
* Creates a new direct buffer.
@@ -51,6 +52,20 @@ public class UnpooledDirectByteBuf extends AbstractReferenceCountedByteBuf {
5152
* @param maxCapacity the maximum capacity of the underlying direct buffer
5253
*/
5354
public UnpooledDirectByteBuf(ByteBufAllocator alloc, int initialCapacity, int maxCapacity) {
55+
this(alloc, initialCapacity, maxCapacity, true);
56+
}
57+
58+
/**
59+
* Creates a new direct buffer.
60+
*
61+
* @param initialCapacity the initial capacity of the underlying direct buffer
62+
* @param maxCapacity the maximum capacity of the underlying direct buffer
63+
* @param allowSectionedInternalNioBufferAccess
64+
* {@code true} if {@link #internalNioBuffer(int, int)} is allowed to be called,
65+
* or {@code false} if it should throw an exception.
66+
*/
67+
UnpooledDirectByteBuf(ByteBufAllocator alloc, int initialCapacity, int maxCapacity,
68+
boolean allowSectionedInternalNioBufferAccess) {
5469
super(maxCapacity);
5570
ObjectUtil.checkNotNull(alloc, "alloc");
5671
checkPositiveOrZero(initialCapacity, "initialCapacity");
@@ -62,6 +77,7 @@ public UnpooledDirectByteBuf(ByteBufAllocator alloc, int initialCapacity, int ma
6277

6378
this.alloc = alloc;
6479
setByteBuffer(allocateDirect(initialCapacity), false);
80+
this.allowSectionedInternalNioBufferAccess = allowSectionedInternalNioBufferAccess;
6581
}
6682

6783
/**
@@ -95,6 +111,7 @@ protected UnpooledDirectByteBuf(ByteBufAllocator alloc, ByteBuffer initialBuffer
95111
doNotFree = !doFree;
96112
setByteBuffer((slice ? initialBuffer.slice() : initialBuffer).order(ByteOrder.BIG_ENDIAN), false);
97113
writerIndex(initialCapacity);
114+
allowSectionedInternalNioBufferAccess = true;
98115
}
99116

100117
/**
@@ -303,11 +320,10 @@ void getBytes(int index, byte[] dst, int dstIndex, int length, boolean internal)
303320

304321
ByteBuffer tmpBuf;
305322
if (internal) {
306-
tmpBuf = internalNioBuffer();
323+
tmpBuf = internalNioBuffer(index, length);
307324
} else {
308-
tmpBuf = buffer.duplicate();
325+
tmpBuf = (ByteBuffer) buffer.duplicate().clear().position(index).limit(index + length);
309326
}
310-
tmpBuf.clear().position(index).limit(index + length);
311327
tmpBuf.get(dst, dstIndex, length);
312328
}
313329

@@ -330,11 +346,10 @@ void getBytes(int index, ByteBuffer dst, boolean internal) {
330346

331347
ByteBuffer tmpBuf;
332348
if (internal) {
333-
tmpBuf = internalNioBuffer();
349+
tmpBuf = internalNioBuffer(index, dst.remaining());
334350
} else {
335-
tmpBuf = buffer.duplicate();
351+
tmpBuf = (ByteBuffer) buffer.duplicate().clear().position(index).limit(index + dst.remaining());
336352
}
337-
tmpBuf.clear().position(index).limit(index + dst.remaining());
338353
dst.put(tmpBuf);
339354
}
340355

@@ -449,21 +464,18 @@ public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) {
449464
@Override
450465
public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) {
451466
checkSrcIndex(index, length, srcIndex, src.length);
452-
ByteBuffer tmpBuf = internalNioBuffer();
453-
tmpBuf.clear().position(index).limit(index + length);
467+
ByteBuffer tmpBuf = internalNioBuffer(index, length);
454468
tmpBuf.put(src, srcIndex, length);
455469
return this;
456470
}
457471

458472
@Override
459473
public ByteBuf setBytes(int index, ByteBuffer src) {
460474
ensureAccessible();
461-
ByteBuffer tmpBuf = internalNioBuffer();
462-
if (src == tmpBuf) {
475+
if (src == tmpNioBuf) {
463476
src = src.duplicate();
464477
}
465-
466-
tmpBuf.clear().position(index).limit(index + src.remaining());
478+
ByteBuffer tmpBuf = internalNioBuffer(index, src.remaining());
467479
tmpBuf.put(src);
468480
return this;
469481
}
@@ -503,11 +515,10 @@ private int getBytes(int index, GatheringByteChannel out, int length, boolean in
503515

504516
ByteBuffer tmpBuf;
505517
if (internal) {
506-
tmpBuf = internalNioBuffer();
518+
tmpBuf = internalNioBuffer(index, length);
507519
} else {
508-
tmpBuf = buffer.duplicate();
520+
tmpBuf = (ByteBuffer) buffer.duplicate().clear().position(index).limit(index + length);
509521
}
510-
tmpBuf.clear().position(index).limit(index + length);
511522
return out.write(tmpBuf);
512523
}
513524

@@ -522,8 +533,8 @@ private int getBytes(int index, FileChannel out, long position, int length, bool
522533
return 0;
523534
}
524535

525-
ByteBuffer tmpBuf = internal ? internalNioBuffer() : buffer.duplicate();
526-
tmpBuf.clear().position(index).limit(index + length);
536+
ByteBuffer tmpBuf = internal ? internalNioBuffer(index, length) :
537+
(ByteBuffer) buffer.duplicate().clear().position(index).limit(index + length);
527538
return out.write(tmpBuf, position);
528539
}
529540

@@ -554,8 +565,7 @@ public int setBytes(int index, InputStream in, int length) throws IOException {
554565
if (readBytes <= 0) {
555566
return readBytes;
556567
}
557-
ByteBuffer tmpBuf = internalNioBuffer();
558-
tmpBuf.clear().position(index);
568+
ByteBuffer tmpBuf = internalNioBuffer(index, readBytes);
559569
tmpBuf.put(tmp, 0, readBytes);
560570
return readBytes;
561571
}
@@ -564,8 +574,7 @@ public int setBytes(int index, InputStream in, int length) throws IOException {
564574
@Override
565575
public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
566576
ensureAccessible();
567-
ByteBuffer tmpBuf = internalNioBuffer();
568-
tmpBuf.clear().position(index).limit(index + length);
577+
ByteBuffer tmpBuf = internalNioBuffer(index, length);
569578
try {
570579
return in.read(tmpBuf);
571580
} catch (ClosedChannelException ignored) {
@@ -576,8 +585,7 @@ public int setBytes(int index, ScatteringByteChannel in, int length) throws IOEx
576585
@Override
577586
public int setBytes(int index, FileChannel in, long position, int length) throws IOException {
578587
ensureAccessible();
579-
ByteBuffer tmpBuf = internalNioBuffer();
580-
tmpBuf.clear().position(index).limit(index + length);
588+
ByteBuffer tmpBuf = internalNioBuffer(index, length);
581589
try {
582590
return in.read(tmpBuf, position);
583591
} catch (ClosedChannelException ignored) {
@@ -616,6 +624,9 @@ public ByteBuf copy(int index, int length) {
616624
@Override
617625
public ByteBuffer internalNioBuffer(int index, int length) {
618626
checkIndex(index, length);
627+
if (!allowSectionedInternalNioBufferAccess) {
628+
throw new UnsupportedOperationException("Bug: unsafe access to shared internal chunk buffer");
629+
}
619630
return (ByteBuffer) internalNioBuffer().clear().position(index).limit(index + length);
620631
}
621632

Collapse file

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

Copy file name to clipboardExpand all lines: buffer/src/main/java/io/netty/buffer/UnpooledUnsafeDirectByteBuf.java
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ public UnpooledUnsafeDirectByteBuf(ByteBufAllocator alloc, int initialCapacity,
4141
super(alloc, initialCapacity, maxCapacity);
4242
}
4343

44+
UnpooledUnsafeDirectByteBuf(ByteBufAllocator alloc, int initialCapacity, int maxCapacity,
45+
boolean allowSectionedInternalNioBufferAccess) {
46+
super(alloc, initialCapacity, maxCapacity, allowSectionedInternalNioBufferAccess);
47+
}
48+
4449
/**
4550
* Creates a new direct buffer by wrapping the specified initial buffer.
4651
*
Collapse file

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

Copy file name to clipboardExpand all lines: buffer/src/main/java/io/netty/buffer/UnpooledUnsafeNoCleanerDirectByteBuf.java
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121

2222
class UnpooledUnsafeNoCleanerDirectByteBuf extends UnpooledUnsafeDirectByteBuf {
2323

24-
UnpooledUnsafeNoCleanerDirectByteBuf(ByteBufAllocator alloc, int initialCapacity, int maxCapacity) {
25-
super(alloc, initialCapacity, maxCapacity);
24+
UnpooledUnsafeNoCleanerDirectByteBuf(ByteBufAllocator alloc, int initialCapacity, int maxCapacity,
25+
boolean allowSectionedInternalNioBufferAccess) {
26+
super(alloc, initialCapacity, maxCapacity, allowSectionedInternalNioBufferAccess);
2627
}
2728

2829
@Override
Collapse file

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

Copy file name to clipboardExpand all lines: buffer/src/main/java/io/netty/buffer/UnsafeByteBufUtil.java
+6-3Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -680,11 +680,14 @@ private static int zeroTillAligned(long addr, int length) {
680680
}
681681

682682
static UnpooledUnsafeDirectByteBuf newUnsafeDirectByteBuf(
683-
ByteBufAllocator alloc, int initialCapacity, int maxCapacity) {
683+
ByteBufAllocator alloc, int initialCapacity, int maxCapacity,
684+
boolean allowSectionedInternalNioBufferAccess) {
684685
if (PlatformDependent.useDirectBufferNoCleaner()) {
685-
return new UnpooledUnsafeNoCleanerDirectByteBuf(alloc, initialCapacity, maxCapacity);
686+
return new UnpooledUnsafeNoCleanerDirectByteBuf(
687+
alloc, initialCapacity, maxCapacity, allowSectionedInternalNioBufferAccess);
686688
}
687-
return new UnpooledUnsafeDirectByteBuf(alloc, initialCapacity, maxCapacity);
689+
return new UnpooledUnsafeDirectByteBuf(
690+
alloc, initialCapacity, maxCapacity, allowSectionedInternalNioBufferAccess);
688691
}
689692

690693
private UnsafeByteBufUtil() { }

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.