@@ -76,7 +76,12 @@ private static AsynchronousSocketChannel newSocket(AsynchronousChannelGroup grou
76
76
private boolean readInProgress ;
77
77
private boolean inDoBeginRead ;
78
78
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 ;
80
85
private boolean inDoFlushByteBuffer ;
81
86
82
87
/**
@@ -246,7 +251,7 @@ protected boolean isFlushPending() {
246
251
247
252
@ Override
248
253
protected void doFlushByteBuffer (ByteBuf buf ) throws Exception {
249
- if (inDoFlushByteBuffer || writeInProgress ) {
254
+ if (inDoFlushByteBuffer || writeInProgress != NO_WRITE_IN_PROGRESS ) {
250
255
return ;
251
256
}
252
257
@@ -263,7 +268,7 @@ protected void doFlushByteBuffer(ByteBuf buf) throws Exception {
263
268
// discardReadBytes() later, modifying the readerIndex and the writerIndex unexpectedly.
264
269
buf .discardReadBytes ();
265
270
266
- writeInProgress = true ;
271
+ writeInProgress = WRITE_IN_PROGRESS ;
267
272
if (buf .nioBufferCount () == 1 ) {
268
273
javaChannel ().write (
269
274
buf .nioBuffer (), config .getWriteTimeout (), TimeUnit .MILLISECONDS , this , WRITE_HANDLER );
@@ -279,7 +284,13 @@ protected void doFlushByteBuffer(ByteBuf buf) throws Exception {
279
284
}
280
285
}
281
286
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
+ }
283
294
// JDK decided to write data (or notify handler) later.
284
295
buf .suspendIntermediaryDeallocations ();
285
296
break ;
@@ -368,7 +379,7 @@ private static final class WriteHandler<T extends Number> extends AioCompletionH
368
379
369
380
@ Override
370
381
protected void completed0 (T result , AioSocketChannel channel ) {
371
- channel .writeInProgress = false ;
382
+ channel .writeInProgress = NO_WRITE_IN_PROGRESS ;
372
383
373
384
ByteBuf buf = channel .unsafe ().directOutboundContext ().outboundByteBuffer ();
374
385
if (buf .refCnt () == 0 ) {
@@ -407,7 +418,7 @@ protected void completed0(T result, AioSocketChannel channel) {
407
418
408
419
@ Override
409
420
protected void failed0 (Throwable cause , AioSocketChannel channel ) {
410
- channel .writeInProgress = false ;
421
+ channel .writeInProgress = WRITE_FAILED ;
411
422
channel .flushFutureNotifier .notifyFlushFutures (cause );
412
423
413
424
// Check if the exception was raised because of an InterruptedByTimeoutException which means that the
0 commit comments