From 620b4b67f7f52703044ba83579c1d2853c8684b3 Mon Sep 17 00:00:00 2001 From: "leonid.stashevsky" Date: Fri, 19 Jan 2024 11:02:49 +0100 Subject: [PATCH] KTOR-6664 Cancel pending incoming messages if the session is closing (#3939) (cherry picked from commit 71738f518f14e8847ffaffe9372751a6dcbcb010) --- .../io/ktor/client/plugins/websocket/builders.kt | 1 + .../test/io/ktor/client/tests/WebSocketTest.kt | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/ktor-client/ktor-client-core/common/src/io/ktor/client/plugins/websocket/builders.kt b/ktor-client/ktor-client-core/common/src/io/ktor/client/plugins/websocket/builders.kt index fe27fc6ccce..6da82ec46e6 100644 --- a/ktor-client/ktor-client-core/common/src/io/ktor/client/plugins/websocket/builders.kt +++ b/ktor-client/ktor-client-core/common/src/io/ktor/client/plugins/websocket/builders.kt @@ -101,6 +101,7 @@ public suspend fun HttpClient.webSocket( block(it) } finally { it.close() + it.incoming.cancel() } } } diff --git a/ktor-client/ktor-client-tests/common/test/io/ktor/client/tests/WebSocketTest.kt b/ktor-client/ktor-client-tests/common/test/io/ktor/client/tests/WebSocketTest.kt index 7b084584867..5ae39ae2bfd 100644 --- a/ktor-client/ktor-client-tests/common/test/io/ktor/client/tests/WebSocketTest.kt +++ b/ktor-client/ktor-client-tests/common/test/io/ktor/client/tests/WebSocketTest.kt @@ -307,4 +307,19 @@ class WebSocketTest : ClientLoader() { } } } + + @Test + fun testIncomingOverflow() = clientTests(ENGINES_WITHOUT_WS) { + config { + install(WebSockets) + } + + test { client -> + client.webSocket("$TEST_WEBSOCKET_SERVER/websockets/echo") { + repeat(1000) { + send("test") + } + } + } + } }