🔨 replace Netty server engine with CIO on desktop - #4911
Merged
Conversation
The desktop sync server and the local MCP server were the only Netty users. Netty keeps a connection/worker/call event loop group sized for a server host (parallelism/2+1, parallelism/2+1, parallelism threads) and every event loop thread it starts lives until shutdown, so a long-running CrossPaste instance ends up holding dozens of parked eventLoopGroupProxy threads (#4908). CIO has no dedicated threads: the selector loop and request handling run on Dispatchers.IO, whose idle workers are reclaimed automatically. The CLI server already runs on CIO, and the commonMain routing is engine agnostic, so this only swaps the engine type, drops the Netty bootstrap options (Ktor's network layer already sets TCP_NODELAY on accepted sockets and the WebSocket ping covers keep-alive) and removes the Netty dependency and its maxDirectMemory JVM flag.
2 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Part of #4908 (thread count growing over long uptime).
The desktop sync server and the local MCP server were the only Netty users in the app. Netty keeps three event loop groups sized for a server host (connection = parallelism/2+1, worker = parallelism/2+1, call = parallelism), starts each event loop thread lazily on first use, and never releases it until shutdown. On an 18-core machine that is up to 38 permanently parked
eventLoopGroupProxy-*threads that only appear to "grow" over days because they start on demand.This PR swaps both servers to Ktor's CIO engine and removes the Netty dependency entirely:
DesktopServerFactory/DesktopPasteServer/DesktopNetworkModulenow useCIOApplicationEngine.DesktopMcpServerusesembeddedServer(CIO, ...); the MCP SDK'smcp {}plugin is engine agnostic.ktor-server-nettyis dropped from the version catalog and the desktop dependencies, along with the-Dio.netty.maxDirectMemoryJVM flag. The wholeio.netty:*4.2.x stack disappears from the runtime classpath.TCP_NODELAYon accepted sockets, and the 30 s WebSocket ping already covers keep-alive.CIO has no dedicated threads. The selector loop and request handling run on
Dispatchers.IO, whose idle workers are reclaimed after 60 s, so the server no longer contributes a fixed thread pool at all. The CLI server has been running on CIO since it was introduced, and thecommonMainrouting is engine agnostic (the mobile apps cannot use Netty), so no routing or protocol code changes.Verified from Ktor 3.5.2 sources that CIO's
connectionIdleTimeoutSeconds(45 s) only applies while waiting between HTTP responses on a keep-alive connection; after a WebSocket upgrade the pipeline closes its writer channel and hands the connection over, so long-lived sync sockets are unaffected.Test plan
./gradlew app:desktopTest(fast tier): 1680 tests, 0 failures./gradlew app:desktopTest -PintegrationTests --tests SyncIntegrationTest(3 servers, full pairing + sync in one JVM): 15/15DesktopPasteServerTestport-in-use fallback path still passesapp:dependencies --configuration desktopRuntimeClasspathcontains no Netty artifactsps -M <pid> | wc -l) after several hours compared with 2.2.0