Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions lib/src/imap/imap_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,37 @@ class ImapClient extends ClientBase {
_isInIdleMode = false;
_selectedMailbox = null;
_failPendingIdleContinuation('connection error: $error');
// Previously only the event below was fired, so a command that was
// awaiting a response when the socket died never completed: with no
// responseTimeout set, `await sendCommand(...)` hung for the lifetime of
// the process, and even with one it waited out the full timeout for an
// error that had already arrived. The event cannot complete the caller's
// future on its own.
_completePendingTasksWithError(error);
fireEvent(ImapConnectionLostEvent(this));
}

/// Error-completes every task that is queued or awaiting a response.
void _completePendingTasksWithError(dynamic error) {
final pending = <CommandTask>[..._queue, ..._tasks.values];
_queue.clear();
_tasks.clear();
_currentCommandTask = null;
_idleCommandTask = null;
for (final task in pending) {
if (task.completer.isCompleted) {
continue;
}
try {
task.completer.completeError(
ImapException(this, 'connection lost: $error'),
);
} catch (e) {
logApp('unable to completeError for task $task: $e');
}
}
}

@override
Future<void> disconnect() async {
// Fail-first so any future returned by idleStart(waitForContinuation:true)
Expand Down
43 changes: 42 additions & 1 deletion test/imap/imap_client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// cSpell:disable

import 'dart:async';
import 'dart:io' show Platform;
import 'dart:io' show Platform, SocketException;

import 'package:enough_mail/enough_mail.dart';
import 'package:enough_mail/src/private/util/client_base.dart';
Expand Down Expand Up @@ -1440,6 +1440,47 @@ void main() {
expect(uidResponseCode?.targetSequence.toList().first, 176);
});

test(
'ImapClient fails commands awaiting a response when the connection is lost',
() async {
await _selectInbox();
// No server response is configured, so this NOOP stays in flight.
mockServer.response = null;
final pending = client.noop();
await Future.delayed(const Duration(milliseconds: 15));

client.onConnectionError(const SocketException('connection reset'));

await expectLater(
pending,
throwsA(
isA<ImapException>().having(
(e) => e.message,
'message',
contains('connection lost'),
),
),
);
},
);

test(
'ImapClient fails queued commands when the connection is lost',
() async {
await _selectInbox();
mockServer.response = null;
// The first command is sent and waits; the second is queued behind it.
final inFlight = client.noop();
final queued = client.noop();
await Future.delayed(const Duration(milliseconds: 15));

client.onConnectionError(const SocketException('connection reset'));

await expectLater(inFlight, throwsA(isA<ImapException>()));
await expectLater(queued, throwsA(isA<ImapException>()));
},
);

test('ImapClient idle', () async {
final box = await _selectInbox();
expungedMessages = [];
Expand Down