Skip to content

OCPBUGS-84721: Fix WebSocket InvalidStateError in pod terminal#16377

Open
Leo6Leo wants to merge 1 commit intoopenshift:mainfrom
Leo6Leo:OCPBUGS-84721
Open

OCPBUGS-84721: Fix WebSocket InvalidStateError in pod terminal#16377
Leo6Leo wants to merge 1 commit intoopenshift:mainfrom
Leo6Leo:OCPBUGS-84721

Conversation

@Leo6Leo
Copy link
Copy Markdown
Contributor

@Leo6Leo Leo6Leo commented Apr 30, 2026

Analysis / Root cause:
WSFactory.send() only checked if the WebSocket instance existed (this.ws && this.ws.send(data)) before sending. When the WebSocket is still in CONNECTING state (e.g. during terminal reconnects or container switches), resize/input events from xterm.js can fire send() before the handshake completes, throwing InvalidStateError. CLI oc exec is unaffected since this is purely a frontend timing issue.

Solution description:
Guard send() with readyState === WebSocket.OPEN so messages are silently dropped on connections that aren't ready. Added unit tests covering all WebSocket readyState values.

Screenshots / screen recording:

Test setup:

  1. Authenticate to a live OCP 4.18+ cluster
  2. Run the console locally with source ./contrib/oc-environment.sh && ./bin/bridge
  3. Open browser DevTools Console to monitor for errors

Test cases:

  • Open pod terminal, throttle network to Slow 3G, switch containers rapidly — no InvalidStateError
  • Resize browser window during reconnect — no error
  • Type into terminal during reconnect — no error
  • Unit tests: send succeeds when OPEN, silently dropped when CONNECTING/CLOSING/CLOSED/destroyed

Browser conformance:

  • Chrome
  • Firefox
  • Safari (or Epiphany on Linux)

Additional info:
Jira: https://redhat.atlassian.net/browse/OCPBUGS-84721
Backport to release-4.18 planned.

Reviewers and assignees:

Summary by CodeRabbit

  • Bug Fixes
    • Improved WebSocket communication reliability by ensuring data transmission only occurs when the connection is fully established, preventing send failures during connection setup or teardown phases.

WSFactory.send() only checked if the WebSocket instance existed before
calling send(), which throws InvalidStateError when the connection is
still in CONNECTING state. This race condition occurs during terminal
reconnects or container switches when resize/input events fire before
the handshake completes.

Guard send() with a readyState === OPEN check to silently drop messages
on connections that are not yet ready.
@openshift-ci-robot openshift-ci-robot added jira/valid-reference Indicates that this PR references a valid Jira ticket of any type. jira/invalid-bug Indicates that a referenced Jira bug is invalid for the branch this PR is targeting. labels Apr 30, 2026
@openshift-ci-robot
Copy link
Copy Markdown
Contributor

@Leo6Leo: This pull request references Jira Issue OCPBUGS-84721, which is invalid:

  • expected the bug to target the "5.0.0" version, but no target version was set

Comment /jira refresh to re-evaluate validity if changes to the Jira bug are made, or edit the title of this pull request to link to a different bug.

The bug has been updated to refer to the pull request using the external bug tracker.

Details

In response to this:

Analysis / Root cause:
WSFactory.send() only checked if the WebSocket instance existed (this.ws && this.ws.send(data)) before sending. When the WebSocket is still in CONNECTING state (e.g. during terminal reconnects or container switches), resize/input events from xterm.js can fire send() before the handshake completes, throwing InvalidStateError. CLI oc exec is unaffected since this is purely a frontend timing issue.

Solution description:
Guard send() with readyState === WebSocket.OPEN so messages are silently dropped on connections that aren't ready. Added unit tests covering all WebSocket readyState values.

Screenshots / screen recording:

Test setup:

  1. Authenticate to a live OCP 4.18+ cluster
  2. Run the console locally with source ./contrib/oc-environment.sh && ./bin/bridge
  3. Open browser DevTools Console to monitor for errors

Test cases:

  • Open pod terminal, throttle network to Slow 3G, switch containers rapidly — no InvalidStateError
  • Resize browser window during reconnect — no error
  • Type into terminal during reconnect — no error
  • Unit tests: send succeeds when OPEN, silently dropped when CONNECTING/CLOSING/CLOSED/destroyed

Browser conformance:

  • Chrome
  • Firefox
  • Safari (or Epiphany on Linux)

Additional info:
Jira: https://redhat.atlassian.net/browse/OCPBUGS-84721
Backport to release-4.18 planned.

Reviewers and assignees:

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the openshift-eng/jira-lifecycle-plugin repository.

@openshift-ci openshift-ci Bot requested review from rhamilto and stefanonardo April 30, 2026 03:21
@openshift-ci openshift-ci Bot added the component/sdk Related to console-plugin-sdk label Apr 30, 2026
@openshift-ci
Copy link
Copy Markdown
Contributor

openshift-ci Bot commented Apr 30, 2026

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Leo6Leo
Once this PR has been reviewed and has the lgtm label, please assign logonoff for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@Leo6Leo
Copy link
Copy Markdown
Contributor Author

Leo6Leo commented Apr 30, 2026

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 30, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 30, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Central YAML (inherited)

Review profile: CHILL

Plan: Enterprise

Run ID: 19961e7f-ef9b-4c96-821d-6e870f536362

📥 Commits

Reviewing files that changed from the base of the PR and between 335d6bd and dd2483a.

📒 Files selected for processing (2)
  • frontend/packages/console-dynamic-plugin-sdk/src/utils/k8s/__tests__/ws-factory.spec.ts
  • frontend/packages/console-dynamic-plugin-sdk/src/utils/k8s/ws-factory.ts
📜 Recent review details
🔇 Additional comments (2)
frontend/packages/console-dynamic-plugin-sdk/src/utils/k8s/ws-factory.ts (1)

346-349: Ready-state gate in send() correctly closes the InvalidStateError path.

This change is tight and aligned with the bug root cause: only forwarding sends when the socket is OPEN is the right guard here.

frontend/packages/console-dynamic-plugin-sdk/src/utils/k8s/__tests__/ws-factory.spec.ts (1)

42-85: Good state-matrix coverage for send() behavior.

The suite cleanly validates OPEN pass-through, non-OPEN drop behavior, and post-destroy() safety for the regression scenario.


📝 Walkthrough

Walkthrough

This change modifies the WSFactory.send() method to validate the underlying WebSocket's readyState before transmitting data, ensuring sends only occur when the connection state is OPEN. Previously, the method would attempt to send whenever the socket instance existed, regardless of connection readiness. A comprehensive test suite is added that verifies this behavior across socket states (CONNECTING, CLOSING, CLOSED) and confirms graceful handling after destroy() is called.

🚥 Pre-merge checks | ✅ 12
✅ Passed checks (12 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately reflects the main change: fixing an InvalidStateError in the pod terminal WebSocket implementation.
Description check ✅ Passed The description comprehensively covers all required template sections with clear analysis, solution, test setup, test cases, and browser conformance.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Stable And Deterministic Test Names ✅ Passed Ginkgo test naming check is inapplicable to this PR; it modifies Jest TypeScript tests, not Go Ginkgo tests.
Test Structure And Quality ✅ Passed Jest test suite demonstrates strong quality with single-responsibility tests, proper setup handling, and appropriate test structure for synchronous mocked WebSocket interactions.
Microshift Test Compatibility ✅ Passed The PR contains only Jest unit tests for frontend WebSocket functionality with no Ginkgo e2e tests or Kubernetes API interactions, making this check not applicable.
Single Node Openshift (Sno) Test Compatibility ✅ Passed PR introduces Jest unit tests for a frontend WebSocket utility, not Ginkgo e2e tests. The custom check applies only to Ginkgo e2e tests, which are absent.
Topology-Aware Scheduling Compatibility ✅ Passed PR modifies only frontend WebSocket utility code and unit tests; no deployment manifests, operator code, or Kubernetes infrastructure code present.
Ote Binary Stdout Contract ✅ Passed OTE Binary Stdout Contract check does not apply; PR modifies only frontend TypeScript/JavaScript code with no Go binaries, test suite setup, or stdout writes.
Ipv6 And Disconnected Network Test Compatibility ✅ Passed The test file ws-factory.spec.ts is a Jest unit test for a frontend WebSocket utility, not a Ginkgo e2e test, making this Kubernetes-specific check inapplicable.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Review rate limit: 9/10 reviews remaining, refill in 6 minutes.

Comment @coderabbitai help to get the list of available commands and usage tips.

@openshift-ci
Copy link
Copy Markdown
Contributor

openshift-ci Bot commented Apr 30, 2026

@Leo6Leo: all tests passed!

Full PR test history. Your PR dashboard.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

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

Labels

component/sdk Related to console-plugin-sdk jira/invalid-bug Indicates that a referenced Jira bug is invalid for the branch this PR is targeting. jira/valid-reference Indicates that this PR references a valid Jira ticket of any type.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants