Skip to content

fix(imap): fail pending commands when the connection is lost - #288

Open
hobleyd wants to merge 1 commit into
Enough-Software:mainfrom
hobleyd:fix/imap-fail-pending-on-connection-lost
Open

hobleyd wants to merge 1 commit into
Enough-Software:mainfrom
hobleyd:fix/imap-fail-pending-on-connection-lost

Conversation

@hobleyd

@hobleyd hobleyd commented Sep 22, 2026

Copy link
Copy Markdown

Problem

ImapClient.onConnectionError reset state and fired ImapConnectionLostEvent, but never completed the futures of commands that were queued or awaiting a response. With no responseTimeout set, await client.noop() (or any other command in flight when the socket died) hung for the lifetime of the process. With a timeout set it still waited out the full timeout for an error that had already arrived. The event cannot complete the caller's future on its own.

Fix

Before firing the event, every CommandTask in _queue and _tasks is error-completed with ImapException(this, 'connection lost: $error'), and the queue, task map, current task and idle task are cleared. This mirrors what _failPendingIdleContinuation (#274) already does for idleStart(waitForContinuation: true), extended to all commands.

Tests

Two new tests in imap_client_test.dart: one in-flight NOOP, and one in-flight NOOP with a second queued behind it. Both drive client.onConnectionError(...) directly and expect an ImapException. Without the fix both tests time out.

onConnectionError only fired ImapConnectionLostEvent, 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.

Every queued or in-flight CommandTask is now error-completed with an
ImapException before the event is fired, mirroring what
_failPendingIdleContinuation already does for idleStart.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants