Skip to content

Commit

Permalink
[netty#989] Fix possible deeadlock on close in SslHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
Norman Maurer committed Feb 13, 2013
1 parent 9842763 commit cf61cc4
Showing 1 changed file with 31 additions and 25 deletions.
56 changes: 31 additions & 25 deletions src/main/java/org/jboss/netty/handler/ssl/SslHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -1551,35 +1551,41 @@ public void operationComplete(ChannelFuture future) throws Exception {
* See <a href="https://github.com/netty/netty/issues/305">#305</a> for more details.
*/
@Override
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
Throwable cause = null;
synchronized (pendingUnencryptedWrites) {
for (;;) {
PendingWrite pw = pendingUnencryptedWrites.poll();
if (pw == null) {
break;
}
if (cause == null) {
cause = new ClosedChannelException();
}
pw.future.setFailure(cause);
}
public void channelClosed(final ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
// Move the fail of the writes to the IO-Thread to prevent possible deadlock
// See https://github.com/netty/netty/issues/989
ctx.getPipeline().execute(new Runnable() {
public void run() {
Throwable cause = null;
synchronized (pendingUnencryptedWrites) {
for (;;) {
PendingWrite pw = pendingUnencryptedWrites.poll();
if (pw == null) {
break;
}
if (cause == null) {
cause = new ClosedChannelException();
}
pw.future.setFailure(cause);
}

for (;;) {
MessageEvent ev = pendingEncryptedWrites.poll();
if (ev == null) {
break;
for (;;) {
MessageEvent ev = pendingEncryptedWrites.poll();
if (ev == null) {
break;
}
if (cause == null) {
cause = new ClosedChannelException();
}
ev.getFuture().setFailure(cause);
}
}
if (cause == null) {
cause = new ClosedChannelException();

if (cause != null) {
fireExceptionCaught(ctx, cause);
}
ev.getFuture().setFailure(cause);
}
}

if (cause != null) {
fireExceptionCaught(ctx, cause);
}
});

super.channelClosed(ctx, e);
}
Expand Down

0 comments on commit cf61cc4

Please sign in to comment.