Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,14 @@ public final void onOutput() throws HttpException, IOException {
} else {
// RST_STREAM is not subject to flow control
for (final Iterator<H2Stream> it = streams.iterator(); it.hasNext(); ) {
it.next().resetIfCancelled();
final H2Stream stream = it.next();
stream.resetIfCancelled();
if (stream.isClosedPastLingerDeadline()) {
streams.dropStreamId(stream.getId());
stream.releaseResources();
it.remove();
requestSessionOutput();
}
}
}
final int pendingOutputRequests = outputRequests.get();
Expand Down Expand Up @@ -1353,6 +1360,7 @@ private void consumeSettingsFrame(final ByteBuffer payload) throws IOException {
}
applyRemoteSettings(configBuilder.build());
}

private void produceOutput() throws HttpException, IOException {
for (final Iterator<H2Stream> it = streams.iterator(); it.hasNext(); ) {
final H2Stream stream = it.next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.stream.IntStream;

Expand Down Expand Up @@ -1410,6 +1412,176 @@ void testExpiredStreamResetOnInboundContinuation() throws Exception {
ArgumentMatchers.anyList(), ArgumentMatchers.anyBoolean());
}

@SuppressWarnings("unchecked")
private static Map<Integer, H2Stream> getStreamMap(final AbstractH2StreamMultiplexer mux) throws Exception {
final Field streamsField = AbstractH2StreamMultiplexer.class.getDeclaredField("streams");
streamsField.setAccessible(true);
final H2Streams h2Streams = (H2Streams) streamsField.get(mux);
final Field streamMapField = H2Streams.class.getDeclaredField("streamMap");
streamMapField.setAccessible(true);
return (Map<Integer, H2Stream>) streamMapField.get(h2Streams);
}

private static void setConnOutputWindow(final AbstractH2StreamMultiplexer mux, final int value) throws Exception {
final Field connOutputWindowField = AbstractH2StreamMultiplexer.class.getDeclaredField("connOutputWindow");
connOutputWindowField.setAccessible(true);
((AtomicInteger) connOutputWindowField.get(mux)).set(value);
}

@Test
void testClosedStreamIsPurgedEvenWhenConnectionOutputWindowIsExhausted() throws Exception {
final H2Config h2Config = H2Config.custom().build();
final AbstractH2StreamMultiplexer mux = new H2StreamMultiplexerImpl(
protocolIOSession,
FRAME_FACTORY,
StreamIdGenerator.EVEN,
httpProcessor,
CharCodingConfig.DEFAULT,
h2Config,
h2StreamListener,
() -> streamHandler);
completeSettingsHandshake(mux);

final ByteArrayBuffer headerBuf = new ByteArrayBuffer(128);
final HPackEncoder encoder = new HPackEncoder(h2Config.getHeaderTableSize(),
CharCodingSupport.createEncoder(CharCodingConfig.DEFAULT));
final List<Header> headers = Arrays.asList(
new BasicHeader(":method", "GET"),
new BasicHeader(":scheme", "http"),
new BasicHeader(":path", "/empty"),
new BasicHeader(":authority", "example.test"));
encoder.encodeHeaders(headerBuf, headers, h2Config.isCompressionEnabled());

// A bodiless request: END_STREAM on HEADERS closes the remote side immediately.
final RawFrame headerFrame = FRAME_FACTORY.createHeaders(
1, ByteBuffer.wrap(headerBuf.array(), 0, headerBuf.length()), true, true);
mux.onInput(ByteBuffer.wrap(encodeFrame(headerFrame)));
Mockito.verify(streamHandler).consumeHeader(headersCaptor.capture(), ArgumentMatchers.eq(true));

final H2Stream stream = getStreamMap(mux).get(1);

// The application sent a bodiless response: the local side is also closed now, with
// nothing left for the stream to produce. Simulate that directly, exactly as a real
// response with no entity would (see ServerH2StreamHandler#commitResponse).
final Field channelField = H2Stream.class.getDeclaredField("channel");
channelField.setAccessible(true);
((H2StreamChannel) channelField.get(stream)).markLocalClosed();

// Pin the connection output window at zero, exactly as a peer that takes response DATA
// on other streams but never sends a connection-level WINDOW_UPDATE would. Settings are
// already ACKED above, so the window is the only reason produceOutput() would be skipped.
setConnOutputWindow(mux, 0);

mux.onOutput();

// The stream owed the peer nothing and has been closed on both sides; it must be purged
// regardless of the exhausted connection output window, which only constrains streams
// that still have DATA to produce.
Assertions.assertThat(getStreamMap(mux))
.as("closed stream with no pending output must not survive onOutput() just because the connection output window is exhausted")
.doesNotContainKey(1);
Mockito.verify(streamHandler).releaseResources();
}

@Test
void testStreamClosedDuringProduceOutputIsPurgedInTheSamePass() throws Exception {
final H2Config h2Config = H2Config.custom().build();
final AbstractH2StreamMultiplexer mux = new H2StreamMultiplexerImpl(
protocolIOSession,
FRAME_FACTORY,
StreamIdGenerator.EVEN,
httpProcessor,
CharCodingConfig.DEFAULT,
h2Config,
h2StreamListener,
() -> streamHandler);
completeSettingsHandshake(mux);

final ByteArrayBuffer headerBuf = new ByteArrayBuffer(128);
final HPackEncoder encoder = new HPackEncoder(h2Config.getHeaderTableSize(),
CharCodingSupport.createEncoder(CharCodingConfig.DEFAULT));
final List<Header> headers = Arrays.asList(
new BasicHeader(":method", "GET"),
new BasicHeader(":scheme", "http"),
new BasicHeader(":path", "/"),
new BasicHeader(":authority", "example.test"));
encoder.encodeHeaders(headerBuf, headers, h2Config.isCompressionEnabled());

final RawFrame headerFrame = FRAME_FACTORY.createHeaders(
1, ByteBuffer.wrap(headerBuf.array(), 0, headerBuf.length()), true, true);
mux.onInput(ByteBuffer.wrap(encodeFrame(headerFrame)));

final H2Stream stream = getStreamMap(mux).get(1);
final Field channelField = H2Stream.class.getDeclaredField("channel");
channelField.setAccessible(true);
final H2StreamChannel channel = (H2StreamChannel) channelField.get(stream);

// The handler closes the stream's output from inside produceOutput() itself, exactly as
// a real streaming response ends -- the ordering fix must purge it in the SAME onOutput()
// pass, not only on some later call. Window stays open: nothing here depends on it.
Mockito.doAnswer(invocation -> {
channel.endStream();
return null;
}).when(streamHandler).produceOutput();

mux.onOutput();

Assertions.assertThat(getStreamMap(mux))
.as("a stream closed inside produceOutput() must be purged within the same onOutput() pass, not deferred to a later one")
.doesNotContainKey(1);
Mockito.verify(streamHandler).releaseResources();
}

@Test
void testStreamClosedDuringProduceOutputIsReleasedBeforeGracefulShutdownCompletes() throws Exception {
final H2Config h2Config = H2Config.custom().build();
final AbstractH2StreamMultiplexer mux = new H2StreamMultiplexerImpl(
protocolIOSession,
FRAME_FACTORY,
StreamIdGenerator.EVEN,
httpProcessor,
CharCodingConfig.DEFAULT,
h2Config,
h2StreamListener,
() -> streamHandler);
completeSettingsHandshake(mux);

final ByteArrayBuffer headerBuf = new ByteArrayBuffer(128);
final HPackEncoder encoder = new HPackEncoder(h2Config.getHeaderTableSize(),
CharCodingSupport.createEncoder(CharCodingConfig.DEFAULT));
final List<Header> headers = Arrays.asList(
new BasicHeader(":method", "GET"),
new BasicHeader(":scheme", "http"),
new BasicHeader(":path", "/"),
new BasicHeader(":authority", "example.test"));
encoder.encodeHeaders(headerBuf, headers, h2Config.isCompressionEnabled());

final RawFrame headerFrame = FRAME_FACTORY.createHeaders(
1, ByteBuffer.wrap(headerBuf.array(), 0, headerBuf.length()), true, true);
mux.onInput(ByteBuffer.wrap(encodeFrame(headerFrame)));

final H2Stream stream = getStreamMap(mux).get(1);
final Field channelField = H2Stream.class.getDeclaredField("channel");
channelField.setAccessible(true);
final H2StreamChannel channel = (H2StreamChannel) channelField.get(stream);

Mockito.doAnswer(invocation -> {
channel.endStream();
return null;
}).when(streamHandler).produceOutput();

final Field connStateField = AbstractH2StreamMultiplexer.class.getDeclaredField("connState");
connStateField.setAccessible(true);
connStateField.set(mux, AbstractH2StreamMultiplexer.ConnectionHandshake.GRACEFUL_SHUTDOWN);

mux.onOutput();

// If the purge ran before produceOutput() closed the stream, the graceful-shutdown block
// (which drops the stream from the table without releasing it) would see it first, and
// the handler would never be released. Must still be released in this one pass.
Mockito.verify(streamHandler).releaseResources();
}

@Test
void testOutboundTrailersWithPseudoHeaderRejected() throws Exception {
final AbstractH2StreamMultiplexer mux = new H2StreamMultiplexerImpl(
Expand Down
Loading