Skip to content

fix(imap): handle cached client error and close events safely - #64

Open
Seger85 wants to merge 1 commit into
codefuturist:mainfrom
Seger85:fix/imap-client-error-lifecycle
Open

fix(imap): handle cached client error and close events safely#64
Seger85 wants to merge 1 commit into
codefuturist:mainfrom
Seger85:fix/imap-client-error-lifecycle

Conversation

@Seger85

@Seger85 Seger85 commented Aug 13, 2026

Copy link
Copy Markdown

Summary

Handle ImapFlow error and close events for cached IMAP clients so connection failures cannot become unhandled EventEmitter errors and stale client events cannot invalidate a newer replacement connection.

What changes

  • attach an error listener to each cached ImapFlow client
  • log the connection error and invalidate that cached client so the next request reconnects
  • attach a close listener to invalidate a disconnected cached client
  • invalidate the cache only when the event belongs to the client instance that is currently cached for that account
  • add an error listener to the short-lived IMAP discovery client as well, preventing EventEmitter error from escaping that path
  • add focused tests for current-client error, current-client close and stale-client events

Why

Node treats an emitted EventEmitter error without a listener as an uncaught exception. A long-running IMAP connection can encounter socket/network failures, so the connection manager needs to own that event lifecycle explicitly.

The identity check is important: an old client may emit close or error after a replacement has already been cached. Such a stale event must not remove the new healthy client.

Validation

Validated from repository base 99ce431aa81dd4cafc2879bd35b6ee3acd0f2d74 on Node 24 as part of the combined production candidate:

  • full test suite: 157/157 tests passed
  • tsc --noEmit: passed
  • Biome check across src: passed
  • production build: passed
  • Docker image build: passed
  • deployed together with the separately scoped HTTP session lifecycle fix and verified through the normal MCP health path with IMAP and SMTP connected

This PR is intentionally separate from the HTTP session/heap lifecycle fix in #63.

@Seger85
Seger85 requested a review from codefuturist as a code owner August 13, 2026 13:43
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.

1 participant