Skip to content

feat: customize server WebSocket close on handler completion - #1636

Merged
He-Pin merged 2 commits into
apache:mainfrom
He-Pin:feat/ws-server-completion-close
Nov 26, 2026
Merged

He-Pin merged 2 commits into
apache:mainfrom
He-Pin:feat/ws-server-completion-close

Conversation

@He-Pin

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

Copy link
Copy Markdown
Member

Motivation:
When a finite server WebSocket handler completes normally and initiates the
closing handshake, it sends status 1000 without a reason. Applications may
want to tell the peer why an otherwise orderly handler completed, for example
during a service restart.

Modification:
Add WebSocketUpgrade.withCompletionClose(response, code, reason) for
Scala and WebSocket.withCompletionClose(response, code, reason) for Java.
Both decorate the response returned by an existing message handler overload.
The code must be permitted for servers and the reason may be empty or up to
123 UTF-8 bytes. The handler retains its subprotocol and compression settings,
including compression opt-out and per-message filtering. Neither upgrade
trait changes, and the helpers do not require a particular upgrade subtype.
The server-side WebSocket guide shows both DSLs.

Result:
When successful outbound handler completion initiates the existing closing
handshake, the selected Close code and reason follow the final application
message. Without opting in, the server still sends 1000 with no reason.
Peer-initiated closing, handler failures, close timers, client handling,
and responses that do not upgrade are unchanged.

Validation:
Ten focused in-memory tests pass on each of Scala 2.13.18 and 3.3.9 with
JDK 17. They cover the typed Java handler (Flow<Object, TextMessage, NotUsed>)
with and without subprotocol selection, compression opt-out and a per-message
filter, the final message and Close frame, default and custom close,
peer-first and failure paths, an ordinary non-upgrade response, and the
UTF-8 boundary. Both http-core MiMa checks against 2.0.0, native Scalafmt,
Java formatting and scoped header checks pass locally. No full suite or
external network test was run locally.

References:
RFC 6455 sections 5.5.1 and 7.4:
https://www.rfc-editor.org/rfc/rfc6455.html#section-5.5.1
https://www.rfc-editor.org/rfc/rfc6455.html#section-7.4
Netty 4.2's positive test demonstrates the code/reason frame semantics,
not a matching automatic handler-completion lifecycle:
https://github.com/netty/netty/blob/fd00bc3a2a02a614fca8241d16570801a3a08314/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/CloseWebSocketFrameTest.java#L61-L71

@He-Pin
He-Pin requested a review from pjfanning November 25, 2026 22:52

@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.

This adds public Scala and Java API but nothing under docs/ changes. AGENTS.md asks for docs on public API changes, so could server-side/websocket-support.md get a short section for both DSLs?

@He-Pin
He-Pin force-pushed the feat/ws-server-completion-close branch from e14259a to ad78c92 Compare November 26, 2026 04:01
@He-Pin
He-Pin requested a review from pjfanning November 26, 2026 05:13

@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 revision addresses my earlier points. One small, non-blocking docs suggestion: most users install handlers with the handleWebSocketMessages directive rather than calling WebSocketUpgrade directly, and the new "Closing after the last outgoing message" section only shows the low-level form. Could it add a line showing that routing-DSL users can wrap the directive with mapResponse(WebSocketUpgrade.withCompletionClose(_, code, reason)) (Java: mapResponse(r -> WebSocket.withCompletionClose(r, code, reason), ...))?

Motivation: A finite WebSocket handler can complete normally for an
application-specific reason. Make it possible to report that reason
without replacing the existing closing handshake.

Modification: Decorate WebSocket upgrade responses in Scala and Java
with the requested server Close status and reason. Describe both direct
upgrade use and composing the helper with handleWebSocketMessages via
mapResponse in both routing DSLs.

Result: Ordinary handler completion can send the selected code and
reason after its final message. Opt-in only; existing subprotocol,
compression, timeout and unmodified response behavior are preserved.

Tests: The exact Scala and Java routing examples compile with JDK 17
against both Scala 2.13.18 and 3.3.9 APIs. Paradox rendered both
examples and both directive links. No runtime changes in this follow-up.

References: RFC 6455 §§5.5.1 and 7.4; apache#1636 review.
@He-Pin
He-Pin force-pushed the feat/ws-server-completion-close branch from ad78c92 to 063bba9 Compare November 26, 2026 13:14
@He-Pin
He-Pin requested a review from pjfanning November 26, 2026 13:15

@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 adding the mapResponse examples. The code looks good to me: the close frame is validated when the application sets it, it only applies when the server handler completes first, and the peer-initiated and failure paths are tested as unchanged. One thing left:

WebSocketCompletionCloseSpec.scala and WebSocketCompletionCloseJavaApi.java are new files but carry the header for Akka-derived code ("This file is part of the Apache Pekko project, which was derived from Akka"). New code should have the standard Apache header: please delete the headers and run sbt headerCreateAll. The header check in CI does not catch this.

For whoever merges: #1608 also changes UpgradeToOtherProtocolResponseHeader, so the second of the two to merge will need a rebase.

… close tests

Motivation:
WebSocketCompletionCloseSpec.scala and WebSocketCompletionCloseJavaApi.java
are new files but carried the header for code derived from Akka.

Modification:
Replace the header on both files with the standard Apache license header,
as generated by sbt headerCreate.

Result:
The new files carry the header that AGENTS.md requires for new code.

Tests:
- Not run - header comments only
- git diff --check: clean

References:
Refs apache#1636
@pjfanning

Copy link
Copy Markdown
Member

I pushed de386d7 to this branch to fix the header point from my review: WebSocketCompletionCloseSpec.scala and WebSocketCompletionCloseJavaApi.java now have the standard Apache header instead of the Akka-derived one. Only the header comments changed, so no tests were added.

@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 5bb4ce7 into apache:main Nov 26, 2026
6 checks passed
@He-Pin
He-Pin deleted the feat/ws-server-completion-close branch November 26, 2026 19:11
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.

2 participants