Skip to content

fix(connections): handle IMAP errors to prevent crash on idle timeout#44

Open
hazzcomm wants to merge 3 commits into
codefuturist:developfrom
hazzcomm:fix-idle-disconnect
Open

fix(connections): handle IMAP errors to prevent crash on idle timeout#44
hazzcomm wants to merge 3 commits into
codefuturist:developfrom
hazzcomm:fix-idle-disconnect

Conversation

@hazzcomm

Copy link
Copy Markdown

Summary

When the IMAP server closes an idle connection (e.g. Dovecot's default 5-minute socket timeout), imapflow emits an 'error' event on the client. Since no handler is attached, Node crashes with:

node:events:487
      throw er; // Unhandled 'error' event

Error: Socket timeout
    at TLSSocket.<anonymous> (.../imapflow/lib/imap-flow.js:875:29)
    code: 'ETIMEOUT'

This takes the MCP server down and requires a Claude Desktop restart to recover.

Fix

Attach an error handler to the ImapFlow client after connect(). On error, log it and remove the client from the map. The existing usable check in getImapClient() already handles reconnection on the next tool call — it just needs the process to stay alive to reach it.

client.on('error', (err: Error) => {
  mcpLog('warning', 'imap', `IMAP connection lost for "${accountName}": ${err.message}`).catch(() => {});
  this.imapClients.delete(accountName);
});

The change is 11 lines. All 150 unit tests pass.

Test plan

  • pnpm check clean
  • pnpm typecheck clean
  • pnpm test — 150/150 pass
  • Manual idle test: leave server idle >5 minutes, verify next tool call reconnects without crash

🤖 Generated with Claude Code

codefuturist and others added 3 commits May 20, 2026 15:26
Release: CI fixes + Claude action integration
Release: switch claude action to Haiku 4.5
When the IMAP server closes an idle connection (e.g. Dovecot 5-minute
socket timeout), imapflow emits an error event on the client. Without
a handler, Node crashes with Unhandled error event.

Attach an error handler after connect() that logs the event and removes
the stale client from the map. The existing usable check in
getImapClient() already handles reconnection on the next tool call.
@hazzcomm hazzcomm requested a review from codefuturist as a code owner May 22, 2026 02:52
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