Skip to content

Commit 6e341d3

Browse files
committed
Attempt to fix websocket controller disposing race condition
1 parent 8f0ed43 commit 6e341d3

2 files changed

Lines changed: 6 additions & 11 deletions

File tree

Common/Websocket/WebsockBaseController.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public virtual async ValueTask DisposeAsync()
9090

9191
Channel.Writer.TryComplete();
9292
await Close.CancelAsync();
93+
9394
WebSocket?.Dispose();
9495
LinkedSource.Dispose();
9596

@@ -150,13 +151,11 @@ await HttpContext.Response.WriteAsJsonAsync(response, jsonOptions.Value.Serializ
150151
await SendInitialData();
151152

152153
await Logic();
153-
154-
155-
if (_disposed) return;
156-
154+
// Logic ended
155+
157156
await UnregisterConnection();
158-
159-
await DisposeAsync();
157+
158+
await Close.CancelAsync();
160159
}
161160

162161
#region Send Loop

LiveControlGateway/Controllers/LiveControlController.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,6 @@ await WebSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Normal close",
321321
Logger.LogError(ex, "Exception while processing websocket request");
322322
}
323323
}
324-
325-
await Close.CancelAsync();
326324
}
327325

328326
private Task ProcessResult(BaseRequest<LiveRequestType> request)
@@ -568,7 +566,7 @@ await QueueMessage(new LiveControlResponse<LiveResponseType>
568566
[NonAction]
569567
public async Task HubDisconnected()
570568
{
571-
_unregistered = true;
569+
_unregistered = true; // The hub lifetime has already unregistered us
572570

573571
Logger.LogTrace("Hub disconnected, disposing controller");
574572

@@ -590,8 +588,6 @@ await SendWebSocketMessage(new LiveControlResponse<LiveResponseType>
590588
// We don't really care if this fails
591589
Logger.LogDebug(e, "Error while sending disconnect message or closing websocket");
592590
}
593-
594-
await DisposeAsync();
595591
}
596592

597593
/// <inheritdoc />

0 commit comments

Comments
 (0)