Skip to content
Open
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 @@ -47,6 +47,10 @@ private[http2] trait Http2Multiplexer {

def maxBytesToBufferPerSubstream: Int

/**
* True only when no frames remain buffered and no state transition is in progress.
* A stream can close synchronously while its final frame is still being handed downstream.
*/
def hasFlushedAllData: Boolean
}

Expand Down Expand Up @@ -143,7 +147,13 @@ private[http2] trait Http2MultiplexerSupport { logic: GraphStageLogic with Stage

private var _state: MultiplexerState = Idle

def hasFlushedAllData: Boolean = allDataFlushed(_state)
// A transition can close the last stream before its final frame is pushed, e.g. when `sendDataFrame` pulls the
// last DATA frame of a stream. Don't report all data as flushed until the transition is done; `updateState` calls
// `onAllDataFlushed` afterwards if the new state has flushed everything.
// This depth only guards the flushed-data observation during synchronous callbacks. It does not
// make nested state transitions safe: the outer transition still owns the resulting state.
private var transitionDepth = 0
def hasFlushedAllData: Boolean = transitionDepth == 0 && allDataFlushed(_state)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The underlying issue is that Http2StreamHandling calls onAllStreamsClosed synchronously from inside a multiplexer transition. Gating hasFlushedAllData works, but any other re-entrant logic in completeIfDone still runs mid-transition. Was deferring onAllStreamsClosed until the transition completes considered (similar to how deferredStreamToEnqueue already defers enqueueOutStream)? If you prefer to keep this approach, could the changed contract ("returns false while a transition is in progress") also be documented on the trait declaration at line 50, since that's what Http2Demux codes against?

private def allDataFlushed(state: MultiplexerState): Boolean = (state eq WaitingForData) || (state eq Idle)

private val controlFrameBuffer: mutable.Queue[FrameEvent] = new mutable.Queue[FrameEvent]
Expand All @@ -158,7 +168,10 @@ private[http2] trait Http2MultiplexerSupport { logic: GraphStageLogic with Stage

private def updateState(transition: MultiplexerState => MultiplexerState): Unit = {
val oldState = _state
val newState = transition(_state)
transitionDepth += 1

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A depth counter suggests nested updateState calls are supported, but a nested call's result would still be overwritten by the outer _state = newState (that hazard predates this PR, e.g. if a stream callback inside pullNextFrame does multiplexer.pushControlFrame(RstStreamFrame(...))). If nesting isn't expected, a Boolean inTransition flag would be clearer about intent; if it is possible, it probably needs separate handling.

val newState =
try transition(_state)
finally transitionDepth -= 1
_state = newState

if (isDebugEnabled && newState.name != oldState.name) recordStateChange(oldState.name, newState.name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,31 @@ class Http2ClientSpec extends PekkoSpecWithMaterializer("""
user.responseIn.expectComplete()
})

"until an in-flight request body and trailer are sent after the response completes".inAssertAllStagesStopped(
new TestSetup with NetProbes {
val requestStreamProbe = TestPublisher.probe[ChunkStreamPart]()
val requestEntity = Chunked(ContentTypes.`application/octet-stream`, Source.fromPublisher(requestStreamProbe))
user.emitRequest(HttpRequest(method = HttpMethods.POST, uri = "https://www.example.com/",
entity = requestEntity))
network.expectDecodedResponseHEADERSPairs(1, endStream = false)

network.sendHEADERS(1, endStream = true, Seq(RawHeader(":status", "200")))
user.expectResponse().status shouldBe StatusCodes.OK
requestStreamProbe.sendNext(Chunk(ByteString("hello")))
network.expectDATA(1, endStream = false, ByteString("hello"))
user.requestOut.sendComplete()

requestStreamProbe.sendNext(Chunk(ByteString("world")))
requestStreamProbe.sendNext(LastChunk(trailer = Seq(RawHeader("x-result", "complete"))))
requestStreamProbe.sendComplete()
network.expectDATA(1, endStream = false, ByteString("world"))
network.expectDecodedResponseHEADERSPairs(1, endStream = true) shouldEqual
Seq("x-result" -> "complete")
user.responseIn.expectComplete()
network.toNet.expectComplete()
network.fromNet.expectCancellation()
})

"until in-flight streaming requests are fully sent (after response is already complete) (uncommon)".inAssertAllStagesStopped(
new TestSetup with NetProbes {
val requestStreamProbe = TestPublisher.probe[ByteString]()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2045,6 +2045,47 @@ class Http2ServerSpec extends Http2SpecWithMaterializer("""
network.expectComplete()
terminated.futureValue
})
for (withTrailer <- Seq(false, true)) {
s"drain a flow-control-blocked response before completing local graceful shutdown (trailer=$withTrailer)".inAssertAllStagesStopped(
new TestSetup(Setting(SettingIdentifier.SETTINGS_INITIAL_WINDOW_SIZE, 8)) with RequestResponseProbes {
// The probe tracks stream windows from the default initial window size, so tell it about the 8-byte window
// as well, which lets it detect the server sending past its stream credit.
network.updateFromServerWindows(1, _ => 8)
network.sendRequest(1, HttpRequest())
user.expectRequest()
network.sendRequest(3, HttpRequest())
user.expectRequest()

val body = ByteString("abcdefghijklmnop")
val response = HttpResponse(entity = HttpEntity(ContentTypes.`application/octet-stream`, body))
user.emitResponse(1,
if (withTrailer)
response.addAttribute(AttributeKeys.trailer, Trailer(RawHeader("x-result", "complete")))
else response)
network.expectDecodedHEADERS(1, endStream = false)

val first = network.expectDATA(1, endStream = false, numBytes = 8)
first shouldBe body.take(8)

val terminated = serverTerminator.terminate(1.minute)
network.expectGOAWAY(3)
// Another accepted stream can finish while the first waits for more stream-level credit.
user.emitResponse(3, HttpResponse())
network.expectDecodedHEADERS(3, endStream = true)
// Smoke check only: the positive DATA/trailer and completion assertions below establish the result.
network.expectNoBytes(100.millis)
terminated.isCompleted shouldBe false

network.sendWINDOW_UPDATE(1, 8)
val rest = network.expectDATA(1, endStream = !withTrailer, numBytes = 8)
first ++ rest shouldBe body
if (withTrailer)
network.expectDecodedResponseHEADERSPairs(1, endStream = true) shouldEqual
Seq("x-result" -> "complete")
network.expectComplete()
terminated.futureValue
})
}
"refuse late incoming requests".inAssertAllStagesStopped(new TestSetup with RequestResponseProbes {
network.sendRequest(1, HttpRequest())
user.expectRequest()
Expand Down
Loading