Skip to content

Commit

Permalink
Merge pull request #892 from stuartwdouglas/UNDERTOW-1713
Browse files Browse the repository at this point in the history
UNDERTOW-1713 Don't start async IO too early
  • Loading branch information
fl4via committed Jun 2, 2020
2 parents 0892780 + a0e4a90 commit 89b9000
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public class ServletInputStreamImpl extends ServletInputStream {
private volatile int state;
private volatile AsyncContextImpl asyncContext;
private volatile PooledByteBuffer pooled;
private volatile boolean asyncIoStarted;

private static final AtomicIntegerFieldUpdater<ServletInputStreamImpl> stateUpdater = AtomicIntegerFieldUpdater.newUpdater(ServletInputStreamImpl.class, "state");

Expand Down Expand Up @@ -102,6 +103,10 @@ public boolean isReady() {
}
}
}
if (!asyncIoStarted) {
//make sure we don't call resumeReads unless we have started async IO
return false;
}
boolean ready = anyAreSet(state, FLAG_READY) && !finished;
if(!ready && listener != null && !finished) {
channel.resumeReads();
Expand Down Expand Up @@ -135,6 +140,7 @@ public void run() {
channel.getIoThread().execute(new Runnable() {
@Override
public void run() {
asyncIoStarted = true;
internalListener.handleEvent(channel);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public class ServletOutputStreamImpl extends ServletOutputStream implements Buff
private StreamSinkChannel channel;
private long written;
private volatile int state;
private volatile boolean asyncIoStarted;
private AsyncContextImpl asyncContext;

private WriteListener listener;
Expand Down Expand Up @@ -756,6 +757,11 @@ public boolean isReady() {
//TODO: is this the correct behaviour?
throw UndertowServletMessages.MESSAGES.streamNotInAsyncMode();
}
if (!asyncIoStarted) {
//if we don't add this guard here calls to isReady could start async IO too soon
//resulting in a 'resuming + dispatched' message
return false;
}
if (!anyAreSet(state, FLAG_READY)) {
if (channel != null) {
channel.resumeWrites();
Expand Down Expand Up @@ -790,6 +796,7 @@ public void setWriteListener(final WriteListener writeListener) {
asyncContext.addAsyncTask(new Runnable() {
@Override
public void run() {
asyncIoStarted = true;
if (channel == null) {
servletRequestContext.getExchange().getIoThread().execute(new Runnable() {
@Override
Expand Down

0 comments on commit 89b9000

Please sign in to comment.