Skip to content

Avoid premature HTTP/2 ping timeouts after scheduler pauses - #1462

Merged
pjfanning merged 1 commit into
apache:mainfrom
He-Pin:fix/http2-ping-fixed-delay
Nov 18, 2026
Merged

pjfanning merged 1 commit into
apache:mainfrom
He-Pin:fix/http2-ping-fixed-delay

Conversation

@He-Pin

@He-Pin He-Pin commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

Motivation

HTTP/2 keepalive callbacks may accumulate while the scheduler or stream interpreter is delayed. Counting callbacks can expire a newly sent ping before its acknowledgment budget has elapsed.

Modification

Keep fixed-delay scheduling and measure elapsed time from the PING send using a monotonic clock. Keep only one ping outstanding so queued ticks cannot replace its timestamp. Preserve idle-data accounting, the zero-timeout fallback and the existing internal two-argument constructor. Add deterministic clock coverage and paired delayed-ACK and no-ACK integration cases.

Result

Callback bursts alone cannot consume the acknowledgment budget. Missing acknowledgments still expire after elapsed time, detected on a subsequent tick. This does not promise immediate processing during dispatcher starvation or exact wall-clock timeout precision.

Release note

HTTP/2 keepalive: an unacknowledged ping now times out based on elapsed time since it was sent, measured against the tick interval (ping-timeout, or ping-interval when ping-timeout is 0). The timeout is detected on the next tick, so the connection is closed after 1 to 2 tick intervals. Before, it always took 2 ticks, so the effective timeout may be shorter than in previous versions. Timer callbacks that queue up while the connection's stage is delayed no longer cause false ping timeouts.

Tests

  • Offline Scala 2.13.18 compilation of changed production and focused test/support sources with -deprecation -Wunused:imports -Werror: passed.
  • org.scalatest.tools.Runner -o -s org.apache.pekko.http.impl.engine.http2.Http2PingSchedulingSpec: four cases passed against cached modules, including direct GOAWAY observation with the exact Ping ack timeout reason in the no-ACK companion.
  • The directional burst test failed with the old runtime body and constructor-only test shim, then passed with the new implementation.
  • Cached Scalafmt 3.11.5 and git diff --check: passed.
  • Clean local sbt build, cross-Scala/MiMa and full client/server ping suites were not run. The no-ACK integration uses a bounded 1200ms no-output observation before advancing the paused scheduler; state-level clock coverage is deterministic.
    GitHub CI passed on this exact revision, including Validate and test run 44233089324.

References

Refs #714.
Related timer discussion: akka/akka-http #4182, retained as a citation following PJ's clarification. No source was copied for this follow-up.
Original production copyright notices are retained; independently written tests have ASF headers.

@pjfanning pjfanning added this to the 2.1.0 milestone Nov 15, 2026

@pjfanning pjfanning left a comment

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.

Thanks, the fix makes sense. The ack timeout is counted in ticks, so the catch-up burst that fixed-rate scheduling produces after a pause can use up the whole timeout window right after a ping is sent. Fixed delay avoids that. The trade-off is that ticks drift later under load, which only makes the ping interval and timeout longer than configured, never shorter. That's the safe direction. The PausedScheduler regression is a nice deterministic way to show it. A few points:

  1. Reference to akka/akka-http#4182. That PR was merged in November 2022, after Akka's licence change, so it's BSL-licensed. Pekko shouldn't derive changes from post-licence-change Akka code. The change here is a one-word scheduling choice with its own reasoning in the description, so I don't think it's a problem in substance. But I'd drop the Akka PR/commit from the description and references and present it on the scheduler-catch-up analysis alone.
  2. reference.conf comment. "Delayed timer execution is not compensated with a burst of keepalive ticks" describes an implementation detail that users can't act on. If anything, the useful thing to say is that the interval and timeout are approximate and may be longer under load. Otherwise I'd leave the config docs as they were.

He-Pin added a commit to He-Pin/incubator-pekko-http that referenced this pull request Nov 17, 2026
Motivation:
The keepalive settings note described a scheduling detail rather than
what users can expect from the configured timing.

Modification:
Explain for both the client and server that the ping interval and
acknowledgment timeout are approximate and may be longer under load.

Result:
The notes describe the observable timing. Configuration defaults,
protocol behavior, tests and attribution are unchanged.

Tests:
Not run - comment-only. git diff --check and exact comment-only diff
verification passed.

References:
Refs apache#1462 and PJ's review.
He-Pin added a commit to He-Pin/incubator-pekko-http that referenced this pull request Nov 17, 2026
Motivation:
Fixed-rate keepalive ticks can catch up after a scheduler pause and
prematurely exhaust the acknowledgment window of a newly sent ping.

Modification:
Use fixed-delay ticks and cover delayed execution with a deterministic
regression. Clarify that the interval and ping timeout may be longer
under load for both client and server settings.

Result:
Delayed callbacks no longer compress the acknowledgment window;
unacknowledged pings still time out.

Tests:
The earlier identical runtime code passed the new focused regression
and six existing ping cases. The documentation follow-up was checked
as comment-only; no runtime retest or CI query for this squash.

References:
Refs apache#1462, apache#714 and akka/akka-http#4182.
@He-Pin
He-Pin force-pushed the fix/http2-ping-fixed-delay branch from 20b7776 to 8129747 Compare November 17, 2026 15:12
He-Pin added a commit to He-Pin/incubator-pekko-http that referenced this pull request Nov 17, 2026
Motivation:
Fixed-rate keepalive ticks can catch up after a scheduler pause and
prematurely exhaust the acknowledgment window of a newly sent ping.

Modification:
Use fixed-delay ticks and cover delayed execution with a deterministic
regression. Clarify that the interval and ping timeout may be longer
under load for both client and server settings.

Result:
Delayed callbacks no longer compress the acknowledgment window;
unacknowledged pings still time out.

Tests:
The earlier identical runtime code passed the new focused regression
and six existing ping cases. The documentation follow-up was checked
as comment-only; no runtime retest or CI query for this squash.

References:
Refs apache#1462 and apache#714.
@He-Pin
He-Pin force-pushed the fix/http2-ping-fixed-delay branch from 8129747 to 8fef30f Compare November 17, 2026 17:37
@He-Pin
He-Pin requested a review from pjfanning November 17, 2026 17:47

@pjfanning pjfanning left a comment

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.

Thanks for the updates; the Akka reference and the reference.conf wording from my earlier review are addressed. A follow-up on the approach and the tests:

Measuring elapsed time instead of counting ticks. EnabledPingState.pingAckOverdue is pingInFlight && ticksSincePing > 1 (Http2Demux.scala:174), so anything that delivers ticks in a burst still produces a false timeout. Fixed delay removes the scheduler's catch-up burst, but not bursts from the stage side (see the inline comment). Recording System.nanoTime() in sendingPing() and checking now - pingSentAt > pingTimeout in the tick handler would make the result independent of how ticks are delivered, while keeping the cheap periodic tick rather than reading the clock per frame. Would you consider that instead of, or as well as, the scheduling change?

PR description. There's no Tests section (the validation notes are under Result), and References should use Refs #... / Fixes #... / None - <context> per AGENTS.md.

@pjfanning

Copy link
Copy Markdown
Member

Correction to point 1 of my earlier review: I was wrong about akka/akka-http#4182. BSL-licensed Akka code becomes Apache 2.0 three years after the first release that contains it. That change first shipped in Akka HTTP 10.5.0 (2023-02-21), so it has been Apache-licensed since 2026-02-21. Citing it is fine, and you're welcome to restore the reference in the description. Sorry for the noise.

@He-Pin
He-Pin force-pushed the fix/http2-ping-fixed-delay branch from 8fef30f to a826191 Compare November 17, 2026 21:27
Motivation:
HTTP/2 keepalive callbacks may accumulate while either the scheduler or stream interpreter is delayed. Counting callbacks can prematurely expire a newly sent ping even when its acknowledgment budget has not elapsed.

Modification:
- Keep fixed-delay scheduling and measure outstanding ping age with a monotonic clock.
- Keep one ping outstanding so queued ticks cannot replace its send timestamp.
- Preserve idle-data accounting, the zero-timeout fallback, and the existing internal two-argument constructor.
- Add deterministic clock coverage for queued callbacks, deadline expiry, acknowledgments, ordinary data, and clock wraparound. Pair the delayed-ACK integration with an unacknowledged-ping timeout observation, assert the timeout reason, and drain pending scheduler work in both tests.

Result:
A callback burst alone cannot consume the acknowledgment budget. Missing acknowledgments still expire after elapsed time, with detection on a subsequent tick. This does not promise immediate processing during dispatcher starvation.

Tests:
- Offline Scala 2.13.18 compilation of changed production and focused test/support sources with `-deprecation -Wunused:imports -Werror`: passed.
- `org.scalatest.tools.Runner -o -s org.apache.pekko.http.impl.engine.http2.Http2PingSchedulingSpec`: 4 passed against cached HTTP module classes/dependencies after rebasing to 73c1ef8, including the exact GOAWAY debug text.
- Directional burst test: failed with old runtime body plus constructor-only test shim, passed with the new implementation.
- Cached official scalafmt 3.11.5 and `git diff --check`: passed.
- Not run: clean sbt build, cross-Scala/MiMa, full client/server ping suites. The no-ACK integration uses a bounded 1200ms no-output observation before advancing the paused scheduler; state-level clock coverage is deterministic.

References:
Refs apache#714.
Related timer discussion: akka/akka-http#4182 (citation only; no source copied for this follow-up).

Provenance: preserves original He-Pin PR head 8fef30f and incorporates PJ review feedback.
@He-Pin
He-Pin force-pushed the fix/http2-ping-fixed-delay branch from a826191 to 48a1d19 Compare November 18, 2026 03:49
@He-Pin
He-Pin requested a review from pjfanning November 18, 2026 04:27

@pjfanning pjfanning left a comment

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.

LGTM. Note for the release notes: an unacknowledged ping now times out after the elapsed tick interval (1 to 2 ticks), rather than after a fixed 2 ticks.

@pjfanning
pjfanning merged commit 944f549 into apache:main Nov 18, 2026
6 checks passed
@pjfanning pjfanning added the release notes should be mentioned in release notes label Nov 18, 2026
pjfanning added a commit that referenced this pull request Nov 19, 2026
Motivation:
Several recently merged PRs left review points unaddressed: docs that
were asked to be dropped, comments and tests that were suggested, and
small API and internal tidy-ups.

Modification:
- Add a Scala FilenameEncoding alias and values to Multipart.FormData
  and use them in the Scala toEntity signature and tests (#1494).
- Render the form-data Content-Disposition header directly instead of
  rendering it twice and slicing off the prefix (#1494).
- Drop the docs paragraph about unpaired URI surrogates (#1426) and the
  cookie -0000 sentence in request-level.md (#1493).
- Fix the javadsl Multipart.FormData doc the same way #1483 fixed the
  scaladsl one.
- Comment the cookie date rule copies so they stay in sync (#1458).
- Test custom methods starting with Q next to the QUERY fast path
  (#1477).
- Use one root package import and a dilated timeout in
  HeaderCompressionSpec (#1438).
- Pass isTrailer to HttpMessageRendering.renderHeaders explicitly
  (#1463).
- Remove the unused forwarding overloads in Handshake.Client and
  EnabledPingState (#1436, #1462).
- Move the media type model checks from the routing spec to a new
  http-core MediaTypesSpec (#1437, #1454).
- Hash the WebSocket key without String.valueOf (#1439).

Result:
The review points are addressed. The only public API change is the
added Scala FilenameEncoding alias.

Tests:
- http-core/testOnly MultipartSpec MultipartsSpec MediaTypesSpec
  RequestParserCRLFSpec RequestParserLFSpec HeaderCompressionSpec
  WebSocketClientSpec HeaderSpec HttpHeaderSpec: 314 passed
- http2-tests/testOnly HttpMessageRenderingSpec Http2PingSchedulingSpec:
  38 passed
- http-tests/testOnly FileAndResourceDirectivesSpec: 63 passed
- http-core/mimaReportBinaryIssues: passed

References:
Refs #1426, #1436, #1437, #1438, #1439, #1454, #1458, #1462, #1463,
#1477, #1483, #1493, #1494
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release notes should be mentioned in release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants