[ConsoleProxy] Performance fixes on concurrency and closing console sessions sockets#13683
[ConsoleProxy] Performance fixes on concurrency and closing console sessions sockets#13683nvazquez wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR targets ConsoleProxy reliability and resource usage by (1) making the “allowed sessions” tracking safe under concurrent access, and (2) ensuring VNC-related connections are actively closed when a noVNC browser session ends, preventing lingering sockets on KVM hosts.
Changes:
- Introduces explicit close paths for NIO socket resources and noVNC client connections to avoid leaking VNC sockets.
- Makes
ConsoleProxy.allowedSessionsconcurrency-safe and guards one-time session consumption. - Ensances WebSocket close handling to trigger client teardown and make remote-address logging more robust.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| services/console-proxy/server/src/main/java/com/cloud/consoleproxy/vnc/NoVncClient.java | Adds a close() method to shut down NIO, websocket, and tunnel socket resources. |
| services/console-proxy/server/src/main/java/com/cloud/consoleproxy/vnc/network/NioSocketHandlerImpl.java | Stores the underlying NioSocket and exposes close() to release it. |
| services/console-proxy/server/src/main/java/com/cloud/consoleproxy/vnc/network/NioSocketHandler.java | Adds a close() contract to the handler interface. |
| services/console-proxy/server/src/main/java/com/cloud/consoleproxy/vnc/network/NioSocket.java | Adds a close() method to release the socket channel and selectors. |
| services/console-proxy/server/src/main/java/com/cloud/consoleproxy/ConsoleProxyNoVNCHandler.java | Ensures viewer teardown is triggered on WebSocket close and hardens remote-address logging. |
| services/console-proxy/server/src/main/java/com/cloud/consoleproxy/ConsoleProxyNoVncClient.java | Ensures closeClient() closes the underlying NoVncClient before removing the viewer. |
| services/console-proxy/server/src/main/java/com/cloud/consoleproxy/ConsoleProxy.java | Switches allowedSessions to a concurrent set and synchronizes one-time session consumption; uses closeClient() when replacing viewers. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| public void close() { | ||
| if (nioSocketConnection != null) { | ||
| nioSocketConnection.close(); | ||
| } | ||
| if (webSocketReverseProxy != null) { | ||
| webSocketReverseProxy.close(); | ||
| } | ||
| if (socket != null) { | ||
| try { | ||
| if (is != null) { | ||
| is.close(); | ||
| } | ||
| if (os != null) { | ||
| os.close(); | ||
| } | ||
| socket.close(); | ||
| } catch (IOException e) { | ||
| logger.debug("Error closing socket: " + e.getMessage(), e); | ||
| } | ||
| } | ||
| } |
| synchronized (allowedSessionsLock) { | ||
| if (allowedSessions.contains(sessionUuid)) { | ||
| LOGGER.debug("Acquiring the session " + sessionUuid + " not available for future use"); | ||
| allowedSessions.remove(sessionUuid); | ||
| } else { | ||
| LOGGER.info("Session " + sessionUuid + " has already been used, cannot connect"); | ||
| authResult.setSuccess(false); | ||
| return authResult; | ||
| } | ||
| } |
| public void closeClient() { | ||
| this.connectionAlive = false; | ||
| // Clear buffer reference to allow GC when client disconnects | ||
| this.readBuffer = null; | ||
| if (client != null) { | ||
| client.close(); | ||
| } | ||
| ConsoleProxy.removeViewer(this); |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## 4.22 #13683 +/- ##
============================================
- Coverage 17.69% 17.69% -0.01%
Complexity 15833 15833
============================================
Files 5925 5925
Lines 533534 533585 +51
Branches 65273 65282 +9
============================================
- Hits 94421 94417 -4
- Misses 428434 428492 +58
+ Partials 10679 10676 -3
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
@blueorangutan package |
|
@nvazquez a [SL] Jenkins job has been kicked to build packages. It will be bundled with KVM, XenServer and VMware SystemVM templates. I'll keep you posted as I make progress. |
|
Packaging result [SF]: ✔️ el8 ✔️ el9 ✔️ el10 ✔️ debian ✔️ suse15. SL-JID 18654 |
| LOGGER.info("Session " + sessionUuid + " has already been used, cannot connect"); | ||
| authResult.setSuccess(false); | ||
| return authResult; | ||
| } |
Description
This PR addresses 2 issues:
In case of concurrent console proxy requests, 2 different threads could modify the allowedSessions set and cause problems accessing the console sessions (fix to be verified)
After closing the browser tab for a VM console session, it was observed that the connection remained open on the KVM host's VNC port.
Fixes: #13422
Types of changes
Feature/Enhancement Scale or Bug Severity
Feature/Enhancement Scale
Bug Severity
Screenshots (if appropriate):
How Has This Been Tested?
agent.zipfile on/usr/share/cloudstack-common/vms/for each KVM host and recreated the CPVMss -tnpcommand for VM's VNC portsHow did you try to break this feature and the system with this change?