Skip to content

connectionStatusProvider burns CPU on disconnect #123

@higaski

Description

@higaski

When the WebSocket closes, it catches the error, yields disconnected, then loops back and calls ref.read(z21ServiceProvider), which returns the same closed instance... basically a loop which burns CPU.

@Riverpod(keepAlive: true)
Stream<ConnectionStatus> connectionStatus(ref) async* {
final timeout = ref.watch(
settingsProvider.select(
(config) =>
config.value?.httpReceiveTimeout ?? Config().httpReceiveTimeout,
),
);
ConnectionStatus? previousStatus;
while (true) {
final z21 = ref.read(z21ServiceProvider);
try {
await for (final _ in z21.stream.timeout(Duration(seconds: timeout))) {
if (previousStatus != ConnectionStatus.connected) {
previousStatus = ConnectionStatus.connected;
yield previousStatus;
}
}
} catch (_) {
if (previousStatus != ConnectionStatus.disconnected) {
previousStatus = ConnectionStatus.disconnected;
yield previousStatus;
}
}
}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions