Skip to content

Commit

Permalink
Fix memory leak of direct memory. (#3983)
Browse files Browse the repository at this point in the history
Descriptions of the changes in this PR:

### Motivation
When open a non-exist file for DirectReader, it will throw an exception. The ByteBuf won't release.
When close DirectWriter, if the close failed, the ByteBuf also won't release.

(cherry picked from commit c8c593e)
  • Loading branch information
horizonzy authored and zymap committed Jun 19, 2023
1 parent 07d3af6 commit cec8551
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ class DirectReader implements LogReader {
this.filename = filename;
this.maxSaneEntrySize = maxSaneEntrySize;
this.readBlockStats = readBlockStats;

nativeBuffer = new Buffer(nativeIO, bufferSize);
closed = false;

try {
Expand All @@ -71,6 +69,7 @@ class DirectReader implements LogReader {
.kv("errno", ne.getErrno()).toString());
}
refreshMaxOffset();
nativeBuffer = new Buffer(nativeIO, bufferSize);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,11 @@ public void close() throws IOException {
throw new IOException(exMsg(ne.getMessage())
.kv("file", filename)
.kv("errno", ne.getErrno()).toString());
}
synchronized (bufferLock) {
bufferPool.release(nativeBuffer);
nativeBuffer = null;
} finally {
synchronized (bufferLock) {
bufferPool.release(nativeBuffer);
nativeBuffer = null;
}
}
}

Expand Down

0 comments on commit cec8551

Please sign in to comment.