Skip to content

Commit

Permalink
Make the cleanup logic in ChannelOutboundBuffer more robust
Browse files Browse the repository at this point in the history
- Fixes netty#1601
  • Loading branch information
trustin committed Jul 18, 2013
1 parent 9c8d980 commit e285949
Showing 1 changed file with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -212,18 +212,25 @@ boolean isEmpty() {
}

void clearUnflushed(Throwable cause) {
if (inFail) {
return;
}

MessageList unflushed = unflushedMessageList;
if (unflushed == null) {
return;
}

inFail = true;

// Release all unflushed messages.
Object[] messages = unflushed.messages();
ChannelPromise[] promises = unflushed.promises();
final int size = unflushed.size();
try {
for (int i = 0; i < size; i++) {
ReferenceCountUtil.release(messages[i]);
safeRelease(messages[i]);

ChannelPromise p = promises[i];
if (!(p instanceof VoidChannelPromise)) {
if (!p.tryFailure(cause)) {
Expand All @@ -235,6 +242,7 @@ void clearUnflushed(Throwable cause) {
unflushed.recycle();
decrementPendingOutboundBytes(unflushedMessageListSize);
unflushedMessageListSize = 0;
inFail = false;
}
}

Expand Down Expand Up @@ -271,7 +279,8 @@ void fail(Throwable cause) {
final int size = current.size();
try {
for (int i = currentMessageIndex; i < size; i++) {
ReferenceCountUtil.release(messages[i]);
safeRelease(messages[i]);

ChannelPromise p = promises[i];
if (!(p instanceof VoidChannelPromise) && !p.tryFailure(cause)) {
logger.warn("Promise done already: {} - new exception is:", p, cause);
Expand All @@ -286,4 +295,12 @@ void fail(Throwable cause) {
inFail = false;
}
}

private static void safeRelease(Object message) {
try {
ReferenceCountUtil.release(message);
} catch (Throwable t) {
logger.warn("Failed to release a message.", t);
}
}
}

0 comments on commit e285949

Please sign in to comment.