Conversation
ba68933 to
86d22ae
Compare
86d22ae to
7a23db9
Compare
2008352 to
b4d550e
Compare
There was a problem hiding this comment.
Pull Request Overview
This PR enhances the datastream connection monitoring functionality to provide better fallback handling when the connection is lost. The changes implement a robust monitoring system that continuously checks the datastream connection status and automatically falls back to API-based flag checks when the connection is unavailable.
- Adds background connection monitoring with periodic status checks
- Implements proper connection state tracking and fallback logic
- Refactors connection handling to use callbacks instead of TaskCompletionSource
Reviewed Changes
Copilot reviewed 6 out of 7 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| src/SchematicHQ.Client/Schematic.cs | Adds connection monitoring background task and improves fallback logic in CheckFlag method |
| src/SchematicHQ.Client/Datastream/DatastreamClientAdapter.cs | Introduces ConnectionStateTracker class and connection status checking functionality |
| src/SchematicHQ.Client/Datastream/Client.cs | Refactors connection handling to use callback-based state updates and improves error handling |
| src/SchematicHQ.Client.Test/Datastream/Mocks/DatastreamClientTestFactory.cs | Updates test factory to use new callback-based connection monitoring |
| examples/DatastreamTestServer/Program.cs | Updates example configuration for Redis cache and base URL |
| .github/workflows/claude-code.yml | Fixes GitHub API membership check logic |
| // Start a background task that periodically checks the connection status | ||
| Task.Run(async () => | ||
| { | ||
| try | ||
| { | ||
| while (!_disposed) |
There was a problem hiding this comment.
Using Task.Run for fire-and-forget background tasks can lead to unhandled exceptions. Consider storing the task reference to properly handle exceptions or use a more robust background service pattern.
| // Start a background task that periodically checks the connection status | |
| Task.Run(async () => | |
| { | |
| try | |
| { | |
| while (!_disposed) | |
| // Create a CancellationTokenSource for managing task cancellation | |
| _connectionMonitoringCancellationTokenSource = new CancellationTokenSource(); | |
| var cancellationToken = _connectionMonitoringCancellationTokenSource.Token; | |
| // Start a background task that periodically checks the connection status | |
| _connectionMonitoringTask = Task.Run(async () => | |
| { | |
| try | |
| { | |
| while (!cancellationToken.IsCancellationRequested) |
| public void UpdateConnectionState(bool connected) | ||
| { | ||
| _stateLock.Wait(); |
There was a problem hiding this comment.
Using synchronous Wait() on SemaphoreSlim in an async context can cause deadlocks. Use WaitAsync() instead to maintain async patterns throughout the call chain.
| public void UpdateConnectionState(bool connected) | |
| { | |
| _stateLock.Wait(); | |
| public async Task UpdateConnectionStateAsync(bool connected) | |
| { | |
| await _stateLock.WaitAsync(); |
| @@ -141,17 +141,23 @@ private async Task ConnectAndReadAsync() | |||
| try | |||
| { | |||
| await _reconnectSemaphore.WaitAsync(); | |||
There was a problem hiding this comment.
The semaphore is acquired but the corresponding Release() call is moved to a catch block far below. This makes the resource management pattern hard to follow and error-prone. Consider using a using statement or try-finally block immediately after acquisition.
|
|
||
| if (_webSocket != null) | ||
| { | ||
| try { _webSocket.Dispose(); } catch { /* ignore */ } |
There was a problem hiding this comment.
Empty catch blocks that silently ignore exceptions make debugging difficult. Consider logging the exception or at minimum adding a comment explaining why the exception can be safely ignored.
| try { _webSocket.Dispose(); } catch { /* ignore */ } | |
| try { _webSocket.Dispose(); } catch (Exception disposeEx) { _logger.Error("Error disposing WebSocket: {0}", disposeEx.Message); } |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…onnected