This is an automated email from the ASF dual-hosted git repository.
markt-asf pushed a commit to branch 9.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/9.0.x by this push:
new 571c7499fb Fix HTTP/2 race that could leave a stream queued with no
stream executing
571c7499fb is described below
commit 571c7499fb717643b177b14557275694638de6a0
Author: Mark Thomas <[email protected]>
AuthorDate: Wed Dec 2 10:29:31 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 aa74b8e353..4ef05857b5 100644
--- a/java/org/apache/coyote/http2/Http2UpgradeHandler.java
+++ b/java/org/apache/coyote/http2/Http2UpgradeHandler.java
@@ -434,11 +434,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();
}
}
}
@@ -774,28 +776,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 bbd534eae1..6033fc4c1d 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]