From a1b9281a0e84d19ccb8fd93672bfc6788e8a8aed Mon Sep 17 00:00:00 2001 From: "He-Pin(kerr)" Date: Fri, 27 Nov 2026 03:55:28 +0800 Subject: [PATCH] fix: complete WebSocket upgrade result when the handshake is cancelled Motivation: An opening handshake cancellation could leave the materialized upgrade result pending when the opening timeout was disabled. Modification: Settle a still-pending upgrade result when cancellation reaches the handshake stage, independent of the opening deadline. Cover cancellation before and after request dispatch and document the behavior. Result: Cancellation completes the upgrade result exceptionally without changing already completed outcomes, peer EOF handling, or the API. References: Apache Pekko HTTP #1461 and #1549; RFC 6455 Section 4.1 for the opening handshake. --- .../paradox/client-side/websocket-support.md | 2 + .../engine/ws/WebSocketClientBlueprint.scala | 2 +- .../impl/engine/ws/WebSocketClientSpec.scala | 64 +++++++++++-------- 3 files changed, 41 insertions(+), 27 deletions(-) diff --git a/docs/src/main/paradox/client-side/websocket-support.md b/docs/src/main/paradox/client-side/websocket-support.md index 4df03069b..4d902a964 100644 --- a/docs/src/main/paradox/client-side/websocket-support.md +++ b/docs/src/main/paradox/client-side/websocket-support.md @@ -54,6 +54,8 @@ the WebSocket stream fail with a `java.util.concurrent.TimeoutException`. If the deadline but cannot accept it as a WebSocket upgrade, the status and parsed headers are still available through the upgrade result's @scala[`response`]@java[`response()`]. +If the opening handshake itself is cancelled, the upgrade result fails even when the timeout is disabled. + The deadline may overlap transport setup, but does not replace the TCP connection or TLS timeouts. It stops on a successful upgrade and is independent of idle timeouts and the WebSocket close handshake. It can also be configured through `ClientConnectionSettings` and `WebSocketSettings.withOpeningTimeout`. diff --git a/http-core/src/main/scala/org/apache/pekko/http/impl/engine/ws/WebSocketClientBlueprint.scala b/http-core/src/main/scala/org/apache/pekko/http/impl/engine/ws/WebSocketClientBlueprint.scala index 87e4173c9..8d5b1d904 100644 --- a/http-core/src/main/scala/org/apache/pekko/http/impl/engine/ws/WebSocketClientBlueprint.scala +++ b/http-core/src/main/scala/org/apache/pekko/http/impl/engine/ws/WebSocketClientBlueprint.scala @@ -174,7 +174,7 @@ private[http] object WebSocketClientBlueprint { setHandlers(in, out, this) override def onDownstreamFinish(cause: Throwable): Unit = { - if (openingDeadline.isDefined && !result.isCompleted) + if (!result.isCompleted) result.tryFailure(new RuntimeException("WebSocket opening handshake was cancelled before completion.", cause)) super.onDownstreamFinish(cause) diff --git a/http-core/src/test/scala/org/apache/pekko/http/impl/engine/ws/WebSocketClientSpec.scala b/http-core/src/test/scala/org/apache/pekko/http/impl/engine/ws/WebSocketClientSpec.scala index 658b46a9f..86c23aa11 100644 --- a/http-core/src/test/scala/org/apache/pekko/http/impl/engine/ws/WebSocketClientSpec.scala +++ b/http-core/src/test/scala/org/apache/pekko/http/impl/engine/ws/WebSocketClientSpec.scala @@ -215,33 +215,45 @@ class WebSocketClientSpec extends PekkoSpecWithMaterializer("pekko.http.client.w } } - "complete the opening handshake result when cancelled before dispatch" in { - Utils.assertAllStagesStopped { - val defaults = ClientConnectionSettings(system) - val settings = defaults.withWebsocketSettings(defaults.websocketSettings.withOpeningTimeout(1.minute)) - val handshake = - WebSocketClientBlueprint.handshake(WebSocketRequest("ws://example.org/ws"), settings, system.log) - val incoming = TestPublisher.probe[ByteString]() - val application = TestSubscriber.probe[ByteString]() - val network = TestSubscriber.probe[ByteString]() - val result = RunnableGraph.fromGraph(GraphDSL.createGraph(handshake) { implicit b => stage => - import GraphDSL.Implicits._ - Source.fromPublisher(incoming) ~> stage.in1 - stage.out1 ~> Sink.fromSubscriber(application) - Source.maybe[ByteString] ~> stage.in2 - stage.out2 ~> Sink.fromSubscriber(network) - ClosedShape - }).run() - - network.ensureSubscription() - try { - // The network has not requested an opening handshake, so the deadline cannot fire. - application.cancel() - result.failed.awaitResult(3.seconds.dilated).getMessage should include("cancelled before completion") - incoming.expectCancellation() - } finally network.cancel() + Seq( + "complete the opening handshake result when cancelled before dispatch" -> false, + "complete the opening handshake result when cancelled after dispatch without an opening timeout" -> true) + .foreach { case (description, afterDispatch) => + description in { + Utils.assertAllStagesStopped { + val defaults = ClientConnectionSettings(system) + val settings = + if (afterDispatch) defaults + else defaults.withWebsocketSettings(defaults.websocketSettings.withOpeningTimeout(1.minute)) + val handshake = + WebSocketClientBlueprint.handshake(WebSocketRequest("ws://example.org/ws"), settings, system.log) + val incoming = TestPublisher.probe[ByteString]() + val application = TestSubscriber.probe[ByteString]() + val network = TestSubscriber.probe[ByteString]() + val result = RunnableGraph.fromGraph(GraphDSL.createGraph(handshake) { implicit b => stage => + import GraphDSL.Implicits._ + Source.fromPublisher(incoming) ~> stage.in1 + stage.out1 ~> Sink.fromSubscriber(application) + Source.maybe[ByteString] ~> stage.in2 + stage.out2 ~> Sink.fromSubscriber(network) + ClosedShape + }).run() + + network.ensureSubscription() + try { + if (afterDispatch) { + settings.websocketSettings.openingTimeout shouldBe Duration.Inf + network.requestNext().utf8String should startWith("GET /ws HTTP/1.1\r\n") + } + // Do not respond or close the input: the handshake is ended by downstream cancellation alone. + result.isCompleted shouldBe false + application.cancel() + incoming.expectCancellation() + result.failed.awaitResult(3.seconds.dilated).getMessage should include("cancelled before completion") + } finally network.cancel() + } + } } - } "retain the HTTP rejection with a configured opening handshake timeout" in { Utils.assertAllStagesStopped {