fix: background message fetching reliability
Some checks are pending
Flutter analyze & test / flutter_analyze_and_test (push) Waiting to run

This commit is contained in:
otsmr 2026-05-19 02:12:12 +02:00
parent fc5c74eaed
commit 927589a505
3 changed files with 30 additions and 6 deletions

View file

@ -1,5 +1,10 @@
# Changelog
## 0.2.17
- Fix: Issue with receiving messages when user closed app while decrypting
- Fix: Background message fetching reliability.
## 0.2.16
- Fix: Images not shown after opening due to cleanup
@ -14,7 +19,7 @@
- New: Tutorial on how to use zoom.
- New: Manage storage view.
- Improved: Media thumbnails for faster loading.
- Fix: Some message where not marked as opened.
- Fix: Some messages were not marked as opened.
## 0.2.12

View file

@ -101,12 +101,20 @@ class ApiService {
Uri.parse(apiUrl),
pingInterval: const Duration(seconds: 30),
);
try {
await channel.ready.timeout(const Duration(seconds: 10));
} catch (e) {
channel.sink.close().ignore();
rethrow;
}
_channel = channel;
_channel!.stream.listen(_onData, onDone: _onDone, onError: _onError);
await _channel!.ready;
Log.info('websocket connected to $apiUrl');
return true;
} catch (e, s) {
_channel = null;
if (kDebugMode) {
print('DEBUG: _connectTo caught exception: $e\n$s');
}
@ -156,6 +164,7 @@ class ApiService {
}
Future<void> onClosed() async {
if (_channel == null) return;
Log.info('websocket connection closed');
_channel = null;
isAuthenticated = false;
@ -187,15 +196,19 @@ class ApiService {
_reconnectionDelay = 3;
}
Future<void> close(Function callback) async {
Future<void> close(Function? callback) async {
Log.info('closing websocket connection');
if (_channel != null) {
await _channel!.sink.close();
try {
await _channel!.sink.close().timeout(const Duration(seconds: 2));
} catch (e) {
Log.warn('Timeout or error closing websocket: $e');
}
await onClosed();
callback();
callback?.call();
return;
}
callback();
callback?.call();
}
Future<void> listenToNetworkChanges() async {

View file

@ -119,9 +119,15 @@ Future<void> handlePeriodicTask({int lastExecutionInSecondsLimit = 120}) async {
if (!shouldBeExecuted) return;
Log.info('eu.twonly.periodic_task was called.');
AppState.gotMessageFromServer = false;
final stopwatch = Stopwatch()..start();
// Issue: Because the background isolate can be reused across multiple periodic tasks,
// the API connection state might be stale or disconnected from a previous run.
// Explicitly close it here to ensure a clean slate before connecting.
await apiService.close(null);
if (!await apiService.connect()) {
Log.info('Could not connect to the api. Returning early.');
return;