← Back to Pull requests

fix(stream): implement RFC 6455 WebSocket ping/pong heartbeats and TCP keepalive (#1841) #2807

Open
unknown opened this pull request with 314 lines diff across 3 files
View on GitHub
unknown unknown commented on Nov 24, 2026 11:07 UTC
Author

What type of PR is this?

/kind bug

What this PR does / why we need it:

Long-running streaming commands (exec, attach, and port-forward) via stream() or portforward() can drop silently or raise WebSocketConnectionClosedException when passing through idle intermediate proxies, firewalls, or load balancers (such as AWS ALB, Envoy, or NGINX).

This PR adds opt-in application-level RFC 6455 WebSocket Ping heartbeats alongside TCP keepalive socket options in kubernetes/stream/:
1. Opt-in Background Ping Worker (WSClient): Accepts ping_interval on WSClient, websocket_call(), and stream() (forwarded only when explicitly provided). When ping_interval > 0 (excluding booleans), a daemon thread sends periodic RFC 6455 Ping control frames via self.sock.ping() until close(), run_forever() completion, or receiving ABNF.OPCODE_CLOSE.
2. Incoming Ping Handling Preserved Without Duplicates: Relies on websocket-client's recv_data_frame(True) automatic Pong reply in both WSClient.update() and PortForward._proxy() rather than sending a second pong frame.
3. TCP Keepalive & Custom Socket Options (create_websocket): Applies configuration.socket_options if configured, or tcp_keepalive_socket_options() when configuration.keep_alive is True. Keeping configuration.keep_alive scoped solely to TCP keepalive preserves existing behavior for callers unless ping_interval is explicitly passed.
4. Unit Tests: Covers opt-in ping_interval forwarding and validation, keeping keep_alive=True scoped to TCP keepalive, applying TCP/custom socket options in create_websocket, and asserting single-pong replies via mock_pong.assert_called_once_with(...) in WSClient.update() and PortForward._proxy().

Which issue(s) this PR fixes:

Fixes #1841

Special notes for your reviewer:

Verified locally on Windows (Python 3.14) across kubernetes/stream/stream_test.py, kubernetes/utils/keepalive_test.py, and WSClientPingPongTest / CreateWebsocketCaCertDataTest / non-select.poll tests in kubernetes/stream/ws_client_test.py.

Does this PR introduce a user-facing change?

``release-note
Added opt-in
ping_interval support for WebSocket Ping heartbeats in stream() / WSClient and applied TCP keepalive (configuration.keep_alive) / configuration.socket_options in create_websocket().


#### Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:

docs

``

Diff: 314 lines (+235 / -79) across 3 files Reviewer: roycaihw Review duration: 9601m
All Vouch review confidence checks passed
Residual risk 0.21 = Change Risk (0.40) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.40 · 314 lines diff across 3 files
0.40
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
0.02
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
46%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.344
Shallow Review Depth shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #2807"
general weight: 0.35
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 46.7% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 40.3% (MEDIUM) · Higher = larger blast radius
40.3%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 46.7% · Lower = rushed or shallow review
46.7%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 21.5% · Safe to merge
21.5%