This is an automated email from the ASF dual-hosted git repository.
markt-asf pushed a commit to branch 11.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/11.0.x by this push:
new cedc9d6bb5 Fix HTTP/2 race that could leave a stream queued with no
stream executing
cedc9d6bb5 is described below
commit cedc9d6bb5837a1b099fc5c92c451179fe7a9584
Author: Mark Thomas <[email protected]>
AuthorDate: Wed Dec 2 10:29:27 2026 +0000
Fix HTTP/2 race that could leave a stream queued with no stream executing
A stream that was queued because the limit on concurrent stream execution
had been reached was not started if the last executing stream finished
between the limit check and the stream being added to the queue. It
remained queued until another stream finished, which might never happen.
Claim execution slots atomically and check the queue again after adding to
it.
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
---
.../apache/coyote/http2/Http2UpgradeHandler.java | 41 ++++++++++++++++------
webapps/docs/changelog.xml | 7 ++++
2 files changed, 37 insertions(+), 11 deletions(-)
diff --git a/java/org/apache/coyote/http2/Http2UpgradeHandler.java
b/java/org/apache/coyote/http2/Http2UpgradeHandler.java
index fac1f5b2d8..33f46c1b3a 100644
--- a/java/org/apache/coyote/http2/Http2UpgradeHandler.java
+++ b/java/org/apache/coyote/http2/Http2UpgradeHandler.java
@@ -436,11 +436,13 @@ class Http2UpgradeHandler extends AbstractStream
implements InternalHttpUpgradeH
if (streamConcurrency == null) {
socketWrapper.execute(streamRunnable);
} else {
- if (getStreamConcurrency() <
protocol.getMaxConcurrentStreamExecution()) {
- increaseStreamConcurrency();
+ if (tryIncreaseStreamConcurrency()) {
socketWrapper.execute(streamRunnable);
} else {
queueStreamRunnable(streamRunnable);
+ // An execution slot may have been released after the check
above but before the runnable was queued.
+ // If so, nothing else would run the queued runnable until
another slot was released.
+ executeQueuedStreams();
}
}
}
@@ -776,28 +778,45 @@ class Http2UpgradeHandler extends AbstractStream
implements InternalHttpUpgradeH
}
- private int increaseStreamConcurrency() {
- return streamConcurrency.incrementAndGet();
+ private boolean tryIncreaseStreamConcurrency() {
+ int max = protocol.getMaxConcurrentStreamExecution();
+ int current;
+ do {
+ current = streamConcurrency.get();
+ if (current >= max) {
+ return false;
+ }
+ } while (!streamConcurrency.compareAndSet(current, current + 1));
+ return true;
}
private int decreaseStreamConcurrency() {
return streamConcurrency.decrementAndGet();
}
- private int getStreamConcurrency() {
- return streamConcurrency.get();
- }
-
void executeQueuedStream() {
if (streamConcurrency == null) {
return;
}
decreaseStreamConcurrency();
- if (getStreamConcurrency() <
protocol.getMaxConcurrentStreamExecution()) {
+ executeQueuedStreams();
+ }
+
+
+ /*
+ * Runs queued streams while there are execution slots available. Every
release of a slot and every addition to the
+ * queue is followed by a call to this method. A slot is claimed before
the queue is polled and the queue is checked
+ * again after a slot is returned so that, whatever the interleaving of
those threads, a queued runnable is never
+ * left in the queue while a slot is free.
+ */
+ private void executeQueuedStreams() {
+ while (!queuedRunnable.isEmpty() && tryIncreaseStreamConcurrency()) {
StreamRunnable streamRunnable = queuedRunnable.poll();
- if (streamRunnable != null) {
+ if (streamRunnable == null) {
+ // Another thread took the runnable. Return the slot and check
the queue again.
+ decreaseStreamConcurrency();
+ } else {
removeFromQueueIndex(streamRunnable);
- increaseStreamConcurrency();
socketWrapper.execute(streamRunnable);
}
}
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index 50e69eb772..06eea3fde4 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -243,6 +243,13 @@
</subsection>
<subsection name="Coyote">
<changelog>
+ <fix>
+ Fix a race condition in HTTP/2 that could leave a stream waiting for an
+ execution slot indefinitely if a slot was released between the check
+ that no slot was available and the stream being added to the queue.
+ This only affected connections where the limit on the number of
+ streams that may be executing at the same time was reached. (markt)
+ </fix>
<fix>
Ensure that an HTTP/2 stream, its processor and its request and
response are cleaned up and recycled once a response written using
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]