Skip to content

feat: make the WebSocket close timeout configurable - #1486

Merged
He-Pin merged 2 commits into
apache:mainfrom
He-Pin:feat/websocket-close-timeout
Nov 23, 2026
Merged

He-Pin merged 2 commits into
apache:mainfrom
He-Pin:feat/websocket-close-timeout

Conversation

@He-Pin

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

Copy link
Copy Markdown
Member

Motivation

Allow applications to configure the existing three-second graceful close
wait without adding periodic work on open WebSocket connections.

Modification

Add bounded finite client and server close-timeout settings with Java and
Scala APIs. Start a one-shot timer only on entering the close wait and
share the client's original budget across peer response and transport
closure. Preserve the three-second default.

Include PJ Fanning's cleanup from d4cd5a0:
remove the obsolete internal Tick and unused messageAPI overload, with an
exact MiMa filter for the removed Tick class.

Result

No periodic close-wait ticks are emitted. The timeout is subject to
scheduler granularity, not a hard end-to-end wall-clock bound.

Tests

  • Historical focused WebSocket and settings suites: PJ reported 111 passing
    and http-core/mimaReportBinaryIssues passing on the previous head.
  • Mechanical replay on current main matches the independently calculated
    merge tree. All 12 changed files are byte-identical to the previous head.
  • git diff --check passed. No new full build, test suite, MiMa or CI run.

References

Refs #1486, Netty #8896 and #9967 (context only; independent implementation).

@He-Pin
He-Pin marked this pull request as ready for review November 17, 2026 20:33
@He-Pin
He-Pin requested review from pjfanning and a balanced review from Copilot November 17, 2026 20:33

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@He-Pin
He-Pin requested an adaptive review from Copilot November 17, 2026 20:34

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@pjfanning pjfanning added this to the 2.1.0 milestone Nov 17, 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 for this. A few points:

Tick interval now follows the setting. WebSocket.messageAPI still uses Source.tick(closeTimeout, closeTimeout, Tick) (WebSocket.scala:288, not in the diff), and that tick runs for the whole life of every connection, not just while closing. Before this PR it was fixed at 3s; now a small close-timeout such as 10ms makes every idle WebSocket push ~100 ticks/s through BypassMerge/FrameOutHandler, and a large one such as 60s lets a close take up to ~2x the setting because the deadline is only checked on a tick. Could the tick interval be decoupled from closeTimeout (e.g. min(closeTimeout, someCap) with a sensible floor), or the deadline driven by a timer started only when FrameOutHandler enters a closing state?

Docs. docs/src/main/paradox/client-side/websocket-support.md:99 still says the connection closes after "a timeout of 3 seconds". It would be good to mention close-timeout there.

He-Pin added a commit to He-Pin/incubator-pekko-http that referenced this pull request Nov 17, 2026
Motivation:
WebSocket close waits currently use a fixed three-second setting and a
periodic tick, even while connections are not closing. Applications need
to tune close-handshake waits without changing other timeout settings.

Modification:
Expose the positive client/server close timeout (maximum one hour) via
config and the Scala and Java settings APIs. Preserve three seconds by
default and for legacy config snippets. Arm a one-shot timer only while
waiting for peer Close or transport closure; keep the original budget
for a client transitioning between those phases. Keep the legacy stack
entry points and default getter, route settings-aware callers through
the configured value, use the shared Java duration converter, and
document the client behavior.

Result:
Both endpoints honor the configured wait with no periodic work on open
connections. Existing idle timeout semantics are unchanged. Timer
delivery remains subject to scheduler granularity.

Tests:
- Strict Scala 2.13.18 compilation of the six affected production sources
  and three affected specifications against cached dependencies: passed.
- Eight settings cases and two directional client/server close cases:
  passed; both directional cases fail against original-main wiring.
- Targeted MiMa against the exact baseline: zero findings.
- Cached Scalafmt and git diff --check: passed.
- Full repository CI and cross-Scala build not run locally.

References:
Refs apache#1486 and Netty netty/netty#8896, netty/netty#9967 (independent Pekko implementation).
@He-Pin
He-Pin force-pushed the feat/websocket-close-timeout branch from aa09d48 to b237ff4 Compare November 17, 2026 20:59
@He-Pin He-Pin changed the title Make the WebSocket close timeout configurable feat: make the WebSocket close timeout configurable Nov 17, 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, this addresses all of my earlier points. The one-shot timer in FrameOutHandler is a nice improvement over the periodic tick. A few small follow-ups:

He-Pin added a commit to He-Pin/incubator-pekko-http that referenced this pull request Nov 17, 2026
Motivation:
Allow applications to tune the current fixed three-second close wait
without changing the idle timeout or adding work to open connections.

Modification:
Configure the client and server close wait with Scala and Java settings
and a finite positive range up to one hour. Arm one timer while closing;
reuse the remaining time when an active client receives the peer Close
but still waits for transport closure. Route standard stack calls
through settings and remove the dead internal tick and default getter.
Keep the legacy messageAPI parameter with an explicit unused annotation.
Document the default, bounds and scheduler granularity.

Result:
Normal traffic has no periodic close check, and a client's two closing
phases share one configured budget. The default remains three seconds.

Tests:
- Strict Scala 2.13.18 production/test compile against cached modules.
- Eight settings cases and normal client/server configurable close
  cases passed in the first review round; the old wiring fails both.
- Current focused client configurable close and shared-budget cases
  passed; the shared-budget case fails when the timer restarts.
- Targeted MiMa with a freshly compiled source baseline reports zero.
  Two obsolete implementation-only symbols are intentionally removed.
- Scalafmt and git diff --check passed; full CI not run locally.

References:
Refs apache#1486, netty/netty#8896 and netty/netty#9967 (independent implementation).
@He-Pin
He-Pin force-pushed the feat/websocket-close-timeout branch from b237ff4 to 05ca137 Compare November 17, 2026 21:26
He-Pin added a commit to He-Pin/incubator-pekko-http that referenced this pull request Nov 17, 2026
Motivation:
Allow applications to tune the fixed three-second close wait without
changing the idle timeout or adding work to open connections.

Modification:
Configure the client and server close wait with Scala and Java settings
and a finite positive range up to one hour. Arm one timer while closing
and reuse its remaining time after the peer Close reply. Route stack
calls through settings and remove the obsolete internal stack default
getter. Keep the legacy messageAPI overload and retain Tick only as an
ABI symbol from 2.0.0; do not schedule or emit it. Qualify the Java
getter's Scala value explicitly for cross-version compilation.

Result:
Open connections no longer receive periodic close checks; the client's
two closing phases share one budget. The default remains three seconds.

Tests:
- Changed sources compiled in Scala 2.13.18; eight settings and two
  focused client cases passed against the rebuilt classes.
- The three affected settings sources compiled on Scala 3.3.9 and 3.9.0
  against published dependencies; the unqualified getter reproduced E049.
- Targeted MiMa with the 2.0.0 artifact and rebuilt classes reproduced
  the missing Tick symbol, then reported zero problems with the fix.
- Scalafmt and git diff --check passed. The cloud JDK lacks the Java 17
  release symbols required by the unchanged aggregate build; full CI
  is not claimed locally.

References:
Refs apache#1486, netty/netty#8896 and netty/netty#9967 (independent implementation).
@He-Pin
He-Pin force-pushed the feat/websocket-close-timeout branch from 05ca137 to c5c75b4 Compare November 17, 2026 22:42
@He-Pin
He-Pin requested a review from pjfanning November 18, 2026 03:46
@pjfanning
pjfanning force-pushed the feat/websocket-close-timeout branch from c5c75b4 to d4cd5a0 Compare November 18, 2026 21:59
pjfanning pushed a commit to He-Pin/incubator-pekko-http that referenced this pull request Nov 18, 2026
Motivation:
Allow applications to tune the fixed three-second close wait without
changing the idle timeout or adding work to open connections.

Modification:
Configure the client and server close wait with Scala and Java settings
and a finite positive range up to one hour. Arm one timer while closing
and reuse its remaining time after the peer Close reply. Route stack
calls through settings and remove the obsolete internal stack default
getter. Keep the legacy messageAPI overload and retain Tick only as an
ABI symbol from 2.0.0; do not schedule or emit it. Qualify the Java
getter's Scala value explicitly for cross-version compilation.

Result:
Open connections no longer receive periodic close checks; the client's
two closing phases share one budget. The default remains three seconds.

Tests:
- Changed sources compiled in Scala 2.13.18; eight settings and two
  focused client cases passed against the rebuilt classes.
- The three affected settings sources compiled on Scala 3.3.9 and 3.9.0
  against published dependencies; the unqualified getter reproduced E049.
- Targeted MiMa with the 2.0.0 artifact and rebuilt classes reproduced
  the missing Tick symbol, then reported zero problems with the fix.
- Scalafmt and git diff --check passed. The cloud JDK lacks the Java 17
  release symbols required by the unchanged aggregate build; full CI
  is not claimed locally.

References:
Refs apache#1486, netty/netty#8896 and netty/netty#9967 (independent implementation).
pjfanning added a commit to He-Pin/incubator-pekko-http that referenced this pull request Nov 18, 2026
Motivation:
After the one-shot close timer in FrameOutHandler, nothing emits
WebSocket.Tick any more, and the two-argument messageAPI overload had no
callers and ignored its closeTimeout parameter.

Modification:
Remove `case object Tick` and the unused overload. Add a MiMa exclude
for the removed internal Tick object.

Result:
No dead close-timeout code is left behind.

Tests:
- http-core/testOnly WebSocketClientSpec WebSocketServerSpec
  ClientConnectionSettingsSpec: 111 passed
- http-core/mimaReportBinaryIssues: passed

References:
Refs apache#1486
@pjfanning

Copy link
Copy Markdown
Member

I rebased this onto main (which now has #1436 and #1428) and pushed a separate commit (d4cd5a0) for the last nits: case object Tick is removed, with a MiMa exclude for the internal object, and so is the unused two-argument messageAPI overload. WebSocket client/server and settings specs pass locally (111 tests), and http-core/mimaReportBinaryIssues passes.

Motivation:
Allow applications to configure the existing three-second graceful close
wait without adding periodic work on open WebSocket connections.

Modification:
Add bounded finite client and server close-timeout settings with Java and
Scala APIs. Start a one-shot timer only on entering the close wait and
share the client's original budget across peer response and transport
closure. Preserve the three-second default.

Include PJ Fanning's cleanup from d4cd5a0:
remove the obsolete internal Tick and unused messageAPI overload, with an
exact MiMa filter for the removed Tick class.

Result:
No periodic close-wait ticks are emitted. The timeout is subject to
scheduler granularity, not a hard end-to-end wall-clock bound.

Tests:
- Historical focused WebSocket and settings suites: PJ reported 111 passing
  and http-core/mimaReportBinaryIssues passing on the previous head.
- Mechanical replay on current main matches the independently calculated
  merge tree. All 12 changed files are byte-identical to the previous head.
- git diff --check passed. No new full build, test suite, MiMa or CI run.

References:
Refs apache#1486, Netty #8896 and #9967 (context only; independent implementation).
@He-Pin
He-Pin force-pushed the feat/websocket-close-timeout branch from d4cd5a0 to c537a9e Compare November 20, 2026 04:24
Motivation:
An invalid `close-timeout` in config failed in two different ways:
`infinite` threw a ConfigurationException naming the setting, while a
zero, negative or over-1-hour value fell through to the settings
constructor's `require` and surfaced as an IllegalArgumentException
that does not name the setting.

The server test "when user handler instantly tries to send messages"
had also been renamed and switched to a 750ms close timeout, although it
never checks close timing, so that scenario was no longer covered with
the default settings.

Modification:
Validate the range in `readCloseTimeout` and throw a
ConfigurationException naming `close-timeout` for every invalid config
value. The constructor's `require` stays for programmatic use.

Restore the server test to its original name and default settings. The
configured close wait is covered by the separate "use the configured
close wait when the peer delays its close response" test.

Result:
Every invalid configured `close-timeout` is reported as a
ConfigurationException that names the setting, and the default 3s close
timeout path keeps its original server test.

Tests:
- sbt "http-core / Test / testOnly org.apache.pekko.http.scaladsl.settings.ClientConnectionSettingsSpec org.apache.pekko.http.impl.engine.ws.WebSocketServerSpec": 81 passed, 7 pending
- sbt "http-core / scalafmtAll": no further changes
- sbt "http-core / mimaReportBinaryIssues": passed
- git diff --check: clean

References:
Refs apache#1486

@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

@He-Pin
He-Pin merged commit 26abb7b into apache:main Nov 23, 2026
6 checks passed
He-Pin added a commit that referenced this pull request Nov 23, 2026
Motivation:
Allow applications to configure the existing three-second graceful close
wait without adding periodic work on open WebSocket connections.

Modification:
Add bounded finite client and server close-timeout settings with Java and
Scala APIs. Start a one-shot timer only on entering the close wait and
share the client's original budget across peer response and transport
closure. Preserve the three-second default.

Include PJ Fanning's cleanup from d4cd5a0:
remove the obsolete internal Tick and unused messageAPI overload, with an
exact MiMa filter for the removed Tick class.

Result:
No periodic close-wait ticks are emitted. The timeout is subject to
scheduler granularity, not a hard end-to-end wall-clock bound.

Tests:
- Historical focused WebSocket and settings suites: PJ reported 111 passing
  and http-core/mimaReportBinaryIssues passing on the previous head.
- Mechanical replay on current main matches the independently calculated
  merge tree. All 12 changed files are byte-identical to the previous head.
- git diff --check passed. No new full build, test suite, MiMa or CI run.

References:
Refs #1486, Netty #8896 and #9967 (context only; independent implementation).
@He-Pin
He-Pin deleted the feat/websocket-close-timeout branch November 23, 2026 03:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants