Make counting of active streams more robust · apache/tomcat@2344a4c · GitHub
Skip to content

Commit

Permalink
Make counting of active streams more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
markt-asf committed Jun 14, 2024
1 parent 550ef03 commit 2344a4c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
2 changes: 1 addition & 1 deletion java/org/apache/coyote/http2/Http2AsyncUpgradeHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ void sendStreamReset(StreamStateMachine state, StreamException se) throws IOExce
boolean active = state.isActive();
state.sendReset();
if (active) {
decrementActiveRemoteStreamCount();
decrementActiveRemoteStreamCount(getStream(se.getStreamId()));
}
}

Expand Down
20 changes: 10 additions & 10 deletions java/org/apache/coyote/http2/Http2UpgradeHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,8 @@ protected void processStreamOnContainerThread(Stream stream) {
}


protected void decrementActiveRemoteStreamCount() {
setConnectionTimeoutForStreamCount(activeRemoteStreamCount.decrementAndGet());
protected void decrementActiveRemoteStreamCount(Stream stream) {
setConnectionTimeoutForStreamCount(stream.decrementAndGetActiveRemoteStreamCount());
}


Expand Down Expand Up @@ -598,7 +598,7 @@ void sendStreamReset(StreamStateMachine state, StreamException se) throws IOExce
boolean active = state.isActive();
state.sendReset();
if (active) {
decrementActiveRemoteStreamCount();
decrementActiveRemoteStreamCount(getStream(se.getStreamId()));
}
}
socketWrapper.write(true, rstFrame, 0, rstFrame.length);
Expand Down Expand Up @@ -825,7 +825,7 @@ void writeBody(Stream stream, ByteBuffer data, int len, boolean finished) throws
protected void sentEndOfStream(Stream stream) {
stream.sentEndOfStream();
if (!stream.isActive()) {
decrementActiveRemoteStreamCount();
decrementActiveRemoteStreamCount(stream);
}
}

Expand Down Expand Up @@ -1208,7 +1208,7 @@ private int allocate(AbstractStream stream, int allocation) {
}


private Stream getStream(int streamId) {
Stream getStream(int streamId) {
Integer key = Integer.valueOf(streamId);
AbstractStream result = streams.get(key);
if (result instanceof Stream) {
Expand Down Expand Up @@ -1536,6 +1536,7 @@ public HeaderEmitter headersStart(int streamId, boolean headersEndStream) throws
Stream stream = getStream(streamId, false);
if (stream == null) {
stream = createRemoteStream(streamId);
activeRemoteStreamCount.incrementAndGet();
}
if (streamId < maxActiveRemoteStreamId) {
throw new ConnectionException(sm.getString("upgradeHandler.stream.old", Integer.valueOf(streamId),
Expand Down Expand Up @@ -1597,9 +1598,8 @@ public void headersEnd(int streamId, boolean endOfStream) throws Http2Exception
Stream stream = (Stream) abstractNonZeroStream;
if (stream.isActive()) {
if (stream.receivedEndOfHeaders()) {

if (localSettings.getMaxConcurrentStreams() < activeRemoteStreamCount.incrementAndGet()) {
decrementActiveRemoteStreamCount();
if (localSettings.getMaxConcurrentStreams() < activeRemoteStreamCount.get()) {
decrementActiveRemoteStreamCount(stream);
// Ignoring maxConcurrentStreams increases the overhead count
increaseOverheadCount(FrameType.HEADERS);
throw new StreamException(
Expand Down Expand Up @@ -1643,7 +1643,7 @@ public void receivedEndOfStream(int streamId) throws ConnectionException {
private void receivedEndOfStream(Stream stream) throws ConnectionException {
stream.receivedEndOfStream();
if (!stream.isActive()) {
decrementActiveRemoteStreamCount();
decrementActiveRemoteStreamCount(stream);
}
}

Expand All @@ -1669,7 +1669,7 @@ public void reset(int streamId, long errorCode) throws Http2Exception {
boolean active = stream.isActive();
stream.receiveReset(errorCode);
if (active) {
decrementActiveRemoteStreamCount();
decrementActiveRemoteStreamCount(stream);
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions java/org/apache/coyote/http2/Stream.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Supplier;
Expand Down Expand Up @@ -88,6 +89,7 @@ class Stream extends AbstractNonZeroStream implements HeaderEmitter {
private final StreamInputBuffer inputBuffer;
private final StreamOutputBuffer streamOutputBuffer = new StreamOutputBuffer();
private final Http2OutputBuffer http2OutputBuffer = new Http2OutputBuffer(coyoteResponse, streamOutputBuffer);
private final AtomicBoolean removedFromActiveCount = new AtomicBoolean(false);

// State machine would be too much overhead
private int headerState = HEADER_STATE_START;
Expand Down Expand Up @@ -838,6 +840,20 @@ public void setIncremental(boolean incremental) {
}


int decrementAndGetActiveRemoteStreamCount() {
/*
* Protect against mis-counting of active streams. This method should only be called once per stream but since
* the count of active streams is used to enforce the maximum concurrent streams limit, make sure each stream is
* only removed from the active count exactly once.
*/
if (removedFromActiveCount.compareAndSet(false, true)) {
return handler.activeRemoteStreamCount.decrementAndGet();
} else {
return handler.activeRemoteStreamCount.get();
}
}


class StreamOutputBuffer implements HttpOutputBuffer, WriteBuffer.Sink {

private final Lock writeLock = new ReentrantLock();
Expand Down
4 changes: 4 additions & 0 deletions webapps/docs/changelog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@
<code>Connector</code> element, similar to the <code>Executor</code>
element, for consistency. (remm)
</update>
<fix>
Make counting of active HTTP/2 streams per connection more robust.
(markt)
</fix>
</changelog>
</subsection>
<subsection name="Jasper">
Expand Down

0 comments on commit 2344a4c

Please sign in to comment.