Skip to content

Commit 7412578

Browse files
committed
Be more careful about release netty buffers
1 parent 0287c53 commit 7412578

1 file changed

Lines changed: 35 additions & 25 deletions

File tree

cloudata-files/src/main/java/com/cloudata/files/fs/SliceByteInput.java

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,43 +38,53 @@ public void close() throws Exception {
3838
@Override
3939
public ByteBuf readChunk(ChannelHandlerContext ctx) throws Exception {
4040
while (true) {
41-
ByteBuf buffer = inner.readChunk(ctx);
41+
ByteBuf buffer = null;
42+
boolean release = true;
4243

43-
if (buffer == null) {
44-
return null;
45-
}
44+
try {
45+
buffer = inner.readChunk(ctx);
46+
47+
if (buffer == null) {
48+
return null;
49+
}
4650

47-
// log.info("SliceByteInput position=" + position);
51+
// log.info("SliceByteInput position=" + position);
4852

49-
if (from != null) {
50-
if (position < from) {
51-
int skip = (int) Math.min(from - position, buffer.readableBytes());
52-
if (skip > 0) {
53-
buffer.skipBytes(skip);
54-
position += skip;
53+
if (from != null) {
54+
if (position < from) {
55+
int skip = (int) Math.min(from - position, buffer.readableBytes());
56+
if (skip > 0) {
57+
buffer.skipBytes(skip);
58+
position += skip;
59+
}
5560
}
5661
}
57-
}
5862

59-
int consumed = buffer.readableBytes();
63+
int consumed = buffer.readableBytes();
6064

61-
if (to != null) {
62-
long n = to - position;
63-
long limitWriterIndex = buffer.readerIndex() + n;
65+
if (to != null) {
66+
long n = to - position;
67+
long limitWriterIndex = buffer.readerIndex() + n;
6468

65-
if (limitWriterIndex < buffer.writerIndex()) {
66-
buffer.writerIndex((int) limitWriterIndex);
67-
}
69+
if (limitWriterIndex < buffer.writerIndex()) {
70+
buffer.writerIndex((int) limitWriterIndex);
71+
}
6872

69-
if (position > to && !buffer.isReadable()) {
70-
return null;
73+
if (position > to && !buffer.isReadable()) {
74+
return null;
75+
}
7176
}
72-
}
7377

74-
position += consumed;
78+
position += consumed;
7579

76-
if (buffer.isReadable()) {
77-
return buffer;
80+
if (buffer.isReadable()) {
81+
release = false;
82+
return buffer;
83+
}
84+
} finally {
85+
if (release && buffer != null) {
86+
buffer.release();
87+
}
7888
}
7989
}
8090
}

0 commit comments

Comments
 (0)