Fix null ref exception when websocket client retries. - #4589
Open
TingluoHuang wants to merge 2 commits into
Open
Fix null ref exception when websocket client retries.#4589TingluoHuang wants to merge 2 commits into
TingluoHuang wants to merge 2 commits into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Refactors ResultServer’s websocket initialization/retry flow to avoid null-reference failures during reconnect attempts by centralizing ClientWebSocket creation and improving retry robustness.
Changes:
- Extracted websocket client creation (headers/user-agent setup) into
CreateWebSocketClient. - Updated websocket initialization to rely on the instance
_tokenrather than passing anaccessTokenparameter. - Added additional disposal/re-init behavior on failures and ensured connect attempts recreate the client when needed.
Show a summary per file
| File | Description |
|---|---|
src/Runner.Common/ResultsServer.cs |
Refactors websocket client creation and adjusts reconnect/disposal behavior to address retry-time null reference exceptions. |
Review details
Comments suppressed due to low confidence (1)
src/Runner.Common/ResultsServer.cs:275
- In the
!delivered"Giving up" path you dispose/null_websocketClient, but any in-flight_websocketConnectTaskstarted by the retry loop (e.g., afterInitializeWebsocketClient(_liveConsoleFeedUrl, delay)) is not canceled/awaited here. SinceConnectWebSocketClientcan (re)create a client when it observes_websocketClient == null, a background connect attempt can still re-establish the connection after we "give up", contradicting the intent of this block and creating disposal/connect races. Consider introducing a dedicated CancellationTokenSource for connect attempts (cancel it here and before starting a new connect), or otherwise ensuring no connect attempt can proceed after this block runs.
if (!delivered)
{
// Giving up for now, so next invocation of this method won't attempt to reconnect
_websocketClient?.Dispose();
_websocketClient = null;
- Files reviewed: 1/1 changed files
- Comments generated: 1
- Review effort level: Low
TingluoHuang
commented
Jul 29, 2026
| if (!string.IsNullOrEmpty(liveConsoleFeedUrl)) | ||
| { | ||
| _liveConsoleFeedUrl = liveConsoleFeedUrl; | ||
| InitializeWebsocketClient(liveConsoleFeedUrl, token, TimeSpan.Zero, retryConnection: true); |
Member
Author
There was a problem hiding this comment.
we have private string _token; defined in the class, so we can just use that and not pass anything around.
TingluoHuang
commented
Jul 29, 2026
| Trace.Info($"Attempting to start websocket client with delay {delay}."); | ||
| await Task.Delay(delay); | ||
| using var connectTimeoutTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(30)); | ||
| await this._websocketClient.ConnectAsync(new Uri(feedStreamUrl), connectTimeoutTokenSource.Token); |
Member
Author
There was a problem hiding this comment.
after the catch, _websocketClient become null.
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.
This pull request refactors how the websocket client is initialized and managed in
ResultsServer.cs, focusing on improving code clarity and reliability. The main change is extracting websocket client creation logic into a newCreateWebSocketClientmethod, which reduces code duplication and centralizes header setup. The refactor also ensures that the websocket client is properly disposed and re-initialized on failures.WebSocket Client Initialization and Management:
CreateWebSocketClient, replacing inline instantiation and header assignment logic. [1] [2]InitializeWebsocketClientto remove the explicitaccessTokenparameter, using the instance_tokeninstead for authorization. [1] [2] [3] [4]nullbefore re-initialization after connection failures, preventing resource leaks and ensuring a fresh client is used. [1] [2]ConnectWebSocketClientto re-create the websocket client if it'snullbefore attempting a connection, improving robustness in reconnection scenarios.These changes improve maintainability, reduce duplication, and enhance the stability of websocket connections.