Skip to content

Commit

Permalink
fix BorrowedBuffer deadlock
Browse files Browse the repository at this point in the history
  • Loading branch information
trip-lixia committed Jun 8, 2022
1 parent 9a61132 commit 485d3f0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@ private void fireDataReceived() {
public boolean encode(Message message) {
checkHandlerIsBound();

ByteBuffer buffer = outboundBuffer.lockAndGet();
if (buffer == null) {
// buffer has been released
// TODO: So what? Maybe throw an exception then?
return false;
}

try {
ByteBuffer buffer = outboundBuffer.lockAndGet();
if (buffer == null) {
// buffer has been released
// this may happen in handshake stage when client send EOF, buffer has been released by SocketChannelHandler
// TODO: So what? Maybe throw an exception then?
return false;
}
return writeMessageToBuffer(message, buffer);
} finally {
outboundBuffer.unlock();
Expand Down
11 changes: 6 additions & 5 deletions bt-core/src/main/java/bt/net/pipeline/SocketChannelHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,13 @@ private boolean processInboundData() throws IOException {
public void flush() {
synchronized (outboundBufferLock) {
ByteBuffer buffer = outboundBuffer.lockAndGet();
if (buffer == null) {
// buffer has been released
return;
}
buffer.flip();
try {
if (buffer == null) {
// buffer has been released
outboundBuffer.unlock();
return;
}
buffer.flip();
while (buffer.hasRemaining() && !closing) {
channel.write(buffer);
}
Expand Down

0 comments on commit 485d3f0

Please sign in to comment.