Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 725156f

Browse files
author
Norman Maurer
committed
[netty#1242] Fix infinity-loop which was triggered when a write failed and AioSocketChannel was used
1 parent 3b86737 commit 725156f

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

transport/src/main/java/io/netty/channel/socket/aio/AioSocketChannel.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,12 @@ private static AsynchronousSocketChannel newSocket(AsynchronousChannelGroup grou
7676
private boolean readInProgress;
7777
private boolean inDoBeginRead;
7878
private boolean readAgain;
79-
private boolean writeInProgress;
79+
80+
private static final int NO_WRITE_IN_PROGRESS = 0;
81+
private static final int WRITE_IN_PROGRESS = 1;
82+
private static final int WRITE_FAILED = -2;
83+
84+
private int writeInProgress;
8085
private boolean inDoFlushByteBuffer;
8186

8287
/**
@@ -246,7 +251,7 @@ protected boolean isFlushPending() {
246251

247252
@Override
248253
protected void doFlushByteBuffer(ByteBuf buf) throws Exception {
249-
if (inDoFlushByteBuffer || writeInProgress) {
254+
if (inDoFlushByteBuffer || writeInProgress != NO_WRITE_IN_PROGRESS) {
250255
return;
251256
}
252257

@@ -263,7 +268,7 @@ protected void doFlushByteBuffer(ByteBuf buf) throws Exception {
263268
// discardReadBytes() later, modifying the readerIndex and the writerIndex unexpectedly.
264269
buf.discardReadBytes();
265270

266-
writeInProgress = true;
271+
writeInProgress = WRITE_IN_PROGRESS;
267272
if (buf.nioBufferCount() == 1) {
268273
javaChannel().write(
269274
buf.nioBuffer(), config.getWriteTimeout(), TimeUnit.MILLISECONDS, this, WRITE_HANDLER);
@@ -279,7 +284,13 @@ protected void doFlushByteBuffer(ByteBuf buf) throws Exception {
279284
}
280285
}
281286

282-
if (writeInProgress) {
287+
if (writeInProgress != NO_WRITE_IN_PROGRESS) {
288+
if (writeInProgress == WRITE_FAILED) {
289+
// failed because of an exception so reset state and break out of the loop now
290+
// See #1242
291+
writeInProgress = NO_WRITE_IN_PROGRESS;
292+
break;
293+
}
283294
// JDK decided to write data (or notify handler) later.
284295
buf.suspendIntermediaryDeallocations();
285296
break;
@@ -368,7 +379,7 @@ private static final class WriteHandler<T extends Number> extends AioCompletionH
368379

369380
@Override
370381
protected void completed0(T result, AioSocketChannel channel) {
371-
channel.writeInProgress = false;
382+
channel.writeInProgress = NO_WRITE_IN_PROGRESS;
372383

373384
ByteBuf buf = channel.unsafe().directOutboundContext().outboundByteBuffer();
374385
if (buf.refCnt() == 0) {
@@ -407,7 +418,7 @@ protected void completed0(T result, AioSocketChannel channel) {
407418

408419
@Override
409420
protected void failed0(Throwable cause, AioSocketChannel channel) {
410-
channel.writeInProgress = false;
421+
channel.writeInProgress = WRITE_FAILED;
411422
channel.flushFutureNotifier.notifyFlushFutures(cause);
412423

413424
// Check if the exception was raised because of an InterruptedByTimeoutException which means that the

0 commit comments

Comments
 (0)