Skip to content

Commit

Permalink
Merge branch 'apache:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
jfclere committed Jul 28, 2023
2 parents 5990ce3 + 5cd6da7 commit 33aad4b
Show file tree
Hide file tree
Showing 12 changed files with 540 additions and 399 deletions.
4 changes: 3 additions & 1 deletion java/jakarta/el/ImportHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public class ImportHandler {
standardPackages.put("jakarta.servlet.jsp", servletJspClassNames);

Set<String> javaLangClassNames = new HashSet<>();
// Based on Java 21 EA24
// Based on Java 21 EA29
// Interfaces
javaLangClassNames.add("Appendable");
javaLangClassNames.add("AutoCloseable");
Expand Down Expand Up @@ -185,6 +185,8 @@ public class ImportHandler {
javaLangClassNames.add("Runtime");
javaLangClassNames.add("Runtime.Version");
javaLangClassNames.add("RuntimePermission");
javaLangClassNames.add("ScopedValue");
javaLangClassNames.add("ScopedValue.Carrier");
javaLangClassNames.add("SecurityManager");
javaLangClassNames.add("Short");
javaLangClassNames.add("StackTraceElement");
Expand Down
84 changes: 55 additions & 29 deletions java/org/apache/coyote/http2/AbstractStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
*/
package org.apache.coyote.http2;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.apache.tomcat.util.res.StringManager;
Expand All @@ -33,6 +37,8 @@ abstract class AbstractStream {
private final String idAsString;

private long windowSize = ConnectionSettingsBase.DEFAULT_INITIAL_WINDOW_SIZE;
protected final Lock windowAllocationLock = new ReentrantLock();
protected final Condition windowAllocationAvailable = windowAllocationLock.newCondition();

private volatile int connectionAllocationRequested = 0;
private volatile int connectionAllocationMade = 0;
Expand All @@ -59,13 +65,23 @@ final int getIdAsInt() {
}


final synchronized void setWindowSize(long windowSize) {
this.windowSize = windowSize;
final void setWindowSize(long windowSize) {
windowAllocationLock.lock();
try {
this.windowSize = windowSize;
} finally {
windowAllocationLock.unlock();
}
}


final synchronized long getWindowSize() {
return windowSize;
final long getWindowSize() {
windowAllocationLock.lock();
try {
return windowSize;
} finally {
windowAllocationLock.unlock();
}
}


Expand All @@ -76,37 +92,47 @@ final synchronized long getWindowSize() {
*
* @throws Http2Exception If the window size is now higher than the maximum allowed
*/
synchronized void incrementWindowSize(int increment) throws Http2Exception {
// No need for overflow protection here.
// Increment can't be more than Integer.MAX_VALUE and once windowSize
// goes beyond 2^31-1 an error is triggered.
windowSize += increment;

if (log.isDebugEnabled()) {
log.debug(sm.getString("abstractStream.windowSizeInc", getConnectionId(), getIdAsString(),
Integer.toString(increment), Long.toString(windowSize)));
}
void incrementWindowSize(int increment) throws Http2Exception {
windowAllocationLock.lock();
try {
// No need for overflow protection here.
// Increment can't be more than Integer.MAX_VALUE and once windowSize
// goes beyond 2^31-1 an error is triggered.
windowSize += increment;

if (log.isDebugEnabled()) {
log.debug(sm.getString("abstractStream.windowSizeInc", getConnectionId(), getIdAsString(),
Integer.toString(increment), Long.toString(windowSize)));
}

if (windowSize > ConnectionSettingsBase.MAX_WINDOW_SIZE) {
String msg = sm.getString("abstractStream.windowSizeTooBig", getConnectionId(), identifier,
Integer.toString(increment), Long.toString(windowSize));
if (identifier.intValue() == 0) {
throw new ConnectionException(msg, Http2Error.FLOW_CONTROL_ERROR);
} else {
throw new StreamException(msg, Http2Error.FLOW_CONTROL_ERROR, identifier.intValue());
if (windowSize > ConnectionSettingsBase.MAX_WINDOW_SIZE) {
String msg = sm.getString("abstractStream.windowSizeTooBig", getConnectionId(), identifier,
Integer.toString(increment), Long.toString(windowSize));
if (identifier.intValue() == 0) {
throw new ConnectionException(msg, Http2Error.FLOW_CONTROL_ERROR);
} else {
throw new StreamException(msg, Http2Error.FLOW_CONTROL_ERROR, identifier.intValue());
}
}
} finally {
windowAllocationLock.unlock();
}
}


final synchronized void decrementWindowSize(int decrement) {
// No need for overflow protection here. Decrement can never be larger
// the Integer.MAX_VALUE and once windowSize goes negative no further
// decrements are permitted
windowSize -= decrement;
if (log.isDebugEnabled()) {
log.debug(sm.getString("abstractStream.windowSizeDec", getConnectionId(), getIdAsString(),
Integer.toString(decrement), Long.toString(windowSize)));
final void decrementWindowSize(int decrement) {
windowAllocationLock.lock();
try {
// No need for overflow protection here. Decrement can never be larger
// the Integer.MAX_VALUE and once windowSize goes negative no further
// decrements are permitted
windowSize -= decrement;
if (log.isDebugEnabled()) {
log.debug(sm.getString("abstractStream.windowSizeDec", getConnectionId(), getIdAsString(),
Integer.toString(decrement), Long.toString(windowSize)));
}
} finally {
windowAllocationLock.unlock();
}
}

Expand Down
16 changes: 12 additions & 4 deletions java/org/apache/coyote/http2/Http2AsyncUpgradeHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import jakarta.servlet.http.WebConnection;

Expand All @@ -44,9 +46,9 @@ public class Http2AsyncUpgradeHandler extends Http2UpgradeHandler {
// Ensures headers are generated and then written for one thread at a time.
// Because of the compression used, headers need to be written to the
// network in the same order they are generated.
private final Object headerWriteLock = new Object();
private final Lock headerWriteLock = new ReentrantLock();
// Ensures thread triggers the stream reset is the first to send a RST frame
private final Object sendResetLock = new Object();
private final Lock sendResetLock = new ReentrantLock();
private final AtomicReference<Throwable> error = new AtomicReference<>();
private final AtomicReference<IOException> applicationIOE = new AtomicReference<>();

Expand Down Expand Up @@ -149,7 +151,8 @@ void sendStreamReset(StreamStateMachine state, StreamException se) throws IOExce
// may see out of order RST frames which may hard to follow if
// the client is unaware the RST frames may be received out of
// order.
synchronized (sendResetLock) {
sendResetLock.lock();
try {
if (state != null) {
boolean active = state.isActive();
state.sendReset();
Expand All @@ -160,6 +163,8 @@ void sendStreamReset(StreamStateMachine state, StreamException se) throws IOExce

socketWrapper.write(BlockingMode.SEMI_BLOCK, protocol.getWriteTimeout(), TimeUnit.MILLISECONDS, null,
SocketWrapperBase.COMPLETE_WRITE, errorCompletion, ByteBuffer.wrap(rstFrame));
} finally {
sendResetLock.unlock();
}
handleAsyncException();
}
Expand Down Expand Up @@ -192,7 +197,8 @@ protected void writeGoAwayFrame(int maxStreamId, long errorCode, byte[] debugMsg
@Override
void writeHeaders(Stream stream, int pushedStreamId, MimeHeaders mimeHeaders, boolean endOfStream, int payloadSize)
throws IOException {
synchronized (headerWriteLock) {
headerWriteLock.lock();
try {
AsyncHeaderFrameBuffers headerFrameBuffers = (AsyncHeaderFrameBuffers) doWriteHeaders(stream,
pushedStreamId, mimeHeaders, endOfStream, payloadSize);
if (headerFrameBuffers != null) {
Expand All @@ -201,6 +207,8 @@ void writeHeaders(Stream stream, int pushedStreamId, MimeHeaders mimeHeaders, bo
headerFrameBuffers.bufs.toArray(BYTEBUFFER_ARRAY));
handleAsyncException();
}
} finally {
headerWriteLock.unlock();
}
if (endOfStream) {
sentEndOfStream(stream);
Expand Down
Loading

0 comments on commit 33aad4b

Please sign in to comment.