Skip to content

[ConsoleProxy] Performance fixes on concurrency and closing console sessions sockets#13683

Open
nvazquez wants to merge 2 commits into
apache:4.22from
shapeblue:422-console-intermittent-issue
Open

[ConsoleProxy] Performance fixes on concurrency and closing console sessions sockets#13683
nvazquez wants to merge 2 commits into
apache:4.22from
shapeblue:422-console-intermittent-issue

Conversation

@nvazquez

Copy link
Copy Markdown
Contributor

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.

    • Before the fix: After opening multiple sessions for the same VM, each new session opens a new socket connection on the KVM host on the VNC port but was not closed after closing the console browser tab.
[root@ref-trl-12127-k-Mol9-nicolas-vazquez-kvm2 ~]# ss -tnp | grep 5901
ESTAB 0      55              10.0.33.112:5901           10.0.39.91:43024 users:(("qemu-kvm",pid=77212,fd=38))                                     
ESTAB 0      55              10.0.33.112:5901           10.0.39.91:51796 users:(("qemu-kvm",pid=77212,fd=36))                                     
ESTAB 0      0               10.0.33.112:5901           10.0.39.91:43008 users:(("qemu-kvm",pid=77212,fd=37))                                     
ESTAB 0      0               10.0.33.112:5901           10.0.39.91:60794 users:(("qemu-kvm",pid=77212,fd=35))  
  • After the fix: Each time a console session is acquired, then only one socket connection remains open per VM, and when it is closed then no connections remain open:
[root@ref-trl-12127-k-Mol9-nicolas-vazquez-kvm2 ~]# ss -tnp | grep 5901
[root@ref-trl-12127-k-Mol9-nicolas-vazquez-kvm2 ~]# ss -tnp | grep 5901
ESTAB 0      0               10.0.33.112:5901           10.0.39.99:41826 users:(("qemu-kvm",pid=77212,fd=35))                                     
[root@ref-trl-12127-k-Mol9-nicolas-vazquez-kvm2 ~]# ss -tnp | grep 5901
[root@ref-trl-12127-k-Mol9-nicolas-vazquez-kvm2 ~]# ss -tnp | grep 5901
ESTAB 0      0               10.0.33.112:5901           10.0.39.99:36836 users:(("qemu-kvm",pid=77212,fd=35))   

Fixes: #13422

Types of changes

  • Breaking change (fix or feature that would cause existing functionality to change)
  • New feature (non-breaking change which adds functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • Enhancement (improves an existing feature and functionality)
  • Cleanup (Code refactoring and cleanup, that may add test cases)
  • Build/CI
  • Test (unit or integration test code)

Feature/Enhancement Scale or Bug Severity

Feature/Enhancement Scale

  • Major
  • Minor

Bug Severity

  • BLOCKER
  • Critical
  • Major
  • Minor
  • Trivial

Screenshots (if appropriate):

How Has This Been Tested?

  • Replaced the agent.zip file on /usr/share/cloudstack-common/vms/ for each KVM host and recreated the CPVM
  • Acquired multiple console sessions and verified the the established using the ss -tnp command for VM's VNC ports

How did you try to break this feature and the system with this change?

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.allowedSessions concurrency-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.

Comment on lines +132 to +152
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);
}
}
}
Comment on lines +213 to 222
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;
}
}
Comment on lines 373 to 380
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

codecov Bot commented Jul 24, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 3.22581% with 60 lines in your changes missing coverage. Please review.
✅ Project coverage is 17.69%. Comparing base (0339f31) to head (08a76ba).

Files with missing lines Patch % Lines
.../com/cloud/consoleproxy/vnc/network/NioSocket.java 0.00% 20 Missing ⚠️
...n/java/com/cloud/consoleproxy/vnc/NoVncClient.java 0.00% 16 Missing ⚠️
...m/cloud/consoleproxy/ConsoleProxyNoVNCHandler.java 0.00% 10 Missing ⚠️
...main/java/com/cloud/consoleproxy/ConsoleProxy.java 20.00% 8 Missing ⚠️
...consoleproxy/vnc/network/NioSocketHandlerImpl.java 0.00% 4 Missing ⚠️
...om/cloud/consoleproxy/ConsoleProxyNoVncClient.java 0.00% 2 Missing ⚠️
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     
Flag Coverage Δ
uitests 3.69% <ø> (ø)
unittests 18.77% <3.22%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@nvazquez

Copy link
Copy Markdown
Contributor Author

@blueorangutan package

@blueorangutan

Copy link
Copy Markdown

@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.

@blueorangutan

Copy link
Copy Markdown

Packaging result [SF]: ✔️ el8 ✔️ el9 ✔️ el10 ✔️ debian ✔️ suse15. SL-JID 18654

@DaanHoogland DaanHoogland linked an issue Jul 24, 2026 that may be closed by this pull request

@DaanHoogland DaanHoogland left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clgtm

LOGGER.info("Session " + sessionUuid + " has already been used, cannot connect");
authResult.setSuccess(false);
return authResult;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@Pearl1594 Pearl1594 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code lgtm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Intermittent Console Proxy Failure in 4.22.1.0

5 participants