Commit 3acb8ea
KTOR-8938 Inherit server coroutine context in WebSocket session (#5426)
* KTOR-8938 Add failing test for WebSocket context inheritance
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* KTOR-8938 Inherit server coroutine context in WebSocket session
The WebSocket session's coroutineContext was built solely from
the engine dispatcher, dropping custom CoroutineContext elements
from the server/call scope. Use call.coroutineContext as the base
so user-provided elements are preserved.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* KTOR-8938 Fix import ordering in WebSocketEngineSuite
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* KTOR-8938 Preserve original Job parent in WebSocket context fix
Strip the call's Job before merging its context, and restore
the original Job from the engine's processing coroutine. This
ensures custom context elements are inherited without changing
the WebSocket session's Job parent-child relationship.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* KTOR-8938 Use call coroutine context including Job for WebSocket session
Use call.coroutineContext directly (with its Job) so that the
WebSocket session is a proper child of the server's coroutine
scope. This ensures cancellation propagates correctly from
server to WebSocket sessions.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* KTOR-8938 Add test for WebSocket session cancellation on server stop
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* KTOR-8938 Make server-stop cancellation test open, skip on Tomcat
Tomcat's servlet-based WebSocket upgrade creates an upgradeJob
with no parent connection to the server scope, so server stop
does not propagate cancellation to WebSocket sessions. This is
a pre-existing Tomcat limitation unrelated to this fix.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* KTOR-8938 Fix WebSocket cancellation on server stop for servlet engines
Pass the servlet's SupervisorJob as parent for the upgrade handler's Job,
ensuring cancellation propagates when the servlet is destroyed. Use
onCancelling=true for WebConnection cleanup to break the deadlock between
blocked IO reads and Job completion.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* KTOR-8938 Skip server-stop cancellation test on JS/WASM
server.stop() calls runBlocking internally via runBlockingBridge,
which throws on JS and WASM platforms. Skip the test on those
targets with a runtime platform check.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* KTOR-8938 Narrow @OptIn(InternalCoroutinesApi::class) scope to init method
Move the annotation from class-level to the specific method that uses
invokeOnCompletion(onCancelling = true), in both servlet and servlet-jakarta
upgrade handlers.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* KTOR-8938 Use stopSuspend instead of stop in WebSocket test
- Replace server.stop() with server.stopSuspend() as suggested by reviewer
- Remove JS/WASM platform check since stopSuspend doesn't use runBlocking
- Remove open modifier from test function (no longer needed for overrides)
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>1 parent cec7d38 commit 3acb8ea
6 files changed
Lines changed: 70 additions & 5 deletions
File tree
- ktor-server
- ktor-server-plugins/ktor-server-websockets/common/src/io/ktor/server/websocket
- ktor-server-servlet-jakarta/jvm/src/io/ktor/server/servlet/jakarta
- ktor-server-servlet/jvm/src/io/ktor/server/servlet
- ktor-server-test-suites/common/src/io/ktor/server/testing/suites
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
95 | 95 | | |
96 | 96 | | |
97 | 97 | | |
98 | | - | |
| 98 | + | |
99 | 99 | | |
100 | 100 | | |
101 | 101 | | |
| |||
Lines changed: 3 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
125 | 125 | | |
126 | 126 | | |
127 | 127 | | |
128 | | - | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
129 | 131 | | |
130 | 132 | | |
131 | 133 | | |
| |||
Lines changed: 2 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
84 | 84 | | |
85 | 85 | | |
86 | 86 | | |
| 87 | + | |
87 | 88 | | |
88 | 89 | | |
89 | 90 | | |
90 | 91 | | |
91 | 92 | | |
92 | 93 | | |
93 | | - | |
| 94 | + | |
94 | 95 | | |
95 | 96 | | |
96 | 97 | | |
| |||
Lines changed: 3 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
125 | 125 | | |
126 | 126 | | |
127 | 127 | | |
128 | | - | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
129 | 131 | | |
130 | 132 | | |
131 | 133 | | |
| |||
Lines changed: 2 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
84 | 84 | | |
85 | 85 | | |
86 | 86 | | |
| 87 | + | |
87 | 88 | | |
88 | 89 | | |
89 | 90 | | |
90 | 91 | | |
91 | 92 | | |
92 | 93 | | |
93 | | - | |
| 94 | + | |
94 | 95 | | |
95 | 96 | | |
96 | 97 | | |
| |||
Lines changed: 59 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| 24 | + | |
24 | 25 | | |
25 | 26 | | |
26 | 27 | | |
| |||
653 | 654 | | |
654 | 655 | | |
655 | 656 | | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
656 | 713 | | |
657 | 714 | | |
658 | 715 | | |
| |||
830 | 887 | | |
831 | 888 | | |
832 | 889 | | |
| 890 | + | |
| 891 | + | |
833 | 892 | | |
834 | 893 | | |
835 | 894 | | |
| |||
0 commit comments