Skip to content

Commit

Permalink
[UNDERTOW-1624] At AbstractFramedStreamSinkChannel.resumeWritesIntern…
Browse files Browse the repository at this point in the history
…al Runnable, count the loop check only if we are looping without any successful writes.
  • Loading branch information
fl4via committed Dec 6, 2019
1 parent 5799465 commit 162bad7
Showing 1 changed file with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ public abstract class AbstractFramedStreamSinkChannel<C extends AbstractFramedCh
private volatile boolean writesResumed;
@SuppressWarnings("unused")
private volatile int inListenerLoop;
/* keep track of successful writes to properly prevent a loop UNDERTOW-1624 */
private volatile boolean writeSucceeded;

private static final AtomicIntegerFieldUpdater<AbstractFramedStreamSinkChannel> inListenerLoopUpdater = AtomicIntegerFieldUpdater.newUpdater(AbstractFramedStreamSinkChannel.class, "inListenerLoop");

Expand Down Expand Up @@ -196,6 +198,8 @@ protected void resumeWritesInternal(boolean wakeup) {
if (inListenerLoopUpdater.compareAndSet(this, 0, 1)) {
getChannel().runInIoThread(new Runnable() {

// loopCount keeps track of runnable being invoked in a
// loop without any successful write operation
int loopCount = 0;

@Override
Expand All @@ -205,7 +209,11 @@ public void run() {
if (listener == null || !isWriteResumed()) {
return;
}
if (loopCount++ == 100) {
if (writeSucceeded) {
// reset write succeeded and loopCount
writeSucceeded = false;
loopCount = 0;
} else if (loopCount++ == 100) {
//should never happen
UndertowLogger.ROOT_LOGGER.listenerNotProgressing();
IoUtils.safeClose(AbstractFramedStreamSinkChannel.this);
Expand Down Expand Up @@ -387,6 +395,7 @@ public long write(ByteBuffer[] srcs, int offset, int length) throws IOException
if(!buffer.hasRemaining()) {
handleBufferFull();
}
writeSucceeded = writeSucceeded || copied > 0;
return copied;
}

Expand All @@ -408,6 +417,7 @@ public int write(ByteBuffer src) throws IOException {
if(!buffer.hasRemaining()) {
handleBufferFull();
}
writeSucceeded = writeSucceeded || copied > 0;
return copied;
}

Expand All @@ -433,6 +443,7 @@ public boolean send(PooledByteBuffer pooled) throws IOException {
protected boolean sendInternal(PooledByteBuffer pooled) throws IOException {
if (safeToSend()) {
this.body = pooled;
writeSucceeded = true;
return true;
}
return false;
Expand Down

0 comments on commit 162bad7

Please sign in to comment.