Fix additional Bugbot review issues

- Add onerror handler for image loading to prevent memory leaks
- Reset isPaused on disconnect to avoid UI desync
- Fix data race on backoff variable using nonisolated(unsafe)
This commit is contained in:
Thomas Marchand
2026-01-02 22:32:03 +00:00
parent 0480828aa2
commit d7007acc20
3 changed files with 14 additions and 1 deletions

View File

@@ -124,6 +124,10 @@ export function DesktopStream({
}
URL.revokeObjectURL(imageUrl);
};
img.onerror = () => {
// Revoke URL on failed load to prevent memory leak
URL.revokeObjectURL(imageUrl);
};
img.src = imageUrl;
} else if (typeof event.data === "string") {
// Text message = JSON (error or control response)

View File

@@ -61,6 +61,7 @@ final class DesktopStreamService {
webSocket?.cancel(with: .normalClosure, reason: nil)
webSocket = nil
isConnected = false
isPaused = false // Reset paused state for fresh connection
currentFrame = nil
frameCount = 0
}

View File

@@ -806,14 +806,17 @@ struct ControlView: View {
}
// Start streaming - this will block until the stream ends
// Use nonisolated(unsafe) to allow mutation from closure (we accept the risk for this simple boolean)
nonisolated(unsafe) var receivedEvent = false
let streamCompleted = await withCheckedContinuation { continuation in
let innerTask = api.streamControl { eventType, data in
receivedEvent = true
Task { @MainActor in
// Successfully received an event - we're connected
if !self.connectionState.isConnected {
self.connectionState = .connected
self.reconnectAttempt = 0
currentBackoff = 1
}
self.handleStreamEvent(type: eventType, data: data)
}
@@ -826,6 +829,11 @@ struct ControlView: View {
}
}
// Reset backoff after successful connection (stream has ended, safe to read)
if receivedEvent {
currentBackoff = 1
}
// Stream ended - check if we should reconnect
guard !Task.isCancelled else { break }