From 9401b47eb77c826d9bce11c680ceb1dccbb3dd46 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 19:30:56 +0000 Subject: [PATCH 1/2] fix(import): back off on refused IMAP connections instead of shrinking the batch Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01LRRzcVEYpPMKt9dL8AN2jq --- src/lib/import/imap-utils.ts | 5 +++++ src/lib/import/jobs.ts | 17 +++++++++++------ tests/imap-chunk-size.test.mjs | 9 ++++++++- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/lib/import/imap-utils.ts b/src/lib/import/imap-utils.ts index c956088b1..cede7cd2c 100644 --- a/src/lib/import/imap-utils.ts +++ b/src/lib/import/imap-utils.ts @@ -49,6 +49,11 @@ export function isPermanentImapError(message: string): boolean { return /login|auth|credential|not allowed|unreadable|no longer exists/i.test(message); } +/** The server refused or dropped the connection, as opposed to failing on the messages themselves. */ +export function isImapConnectionError(message: string): boolean { + return /stream was cancelled|connection closed unexpectedly|did not send an OK greeting|connection (?:reset|refused)|timed? ?out/i.test(message); +} + /** A folder the server says does not exist or cannot be selected, such as Gmail's `[Gmail]` parent. */ export function isMissingImapFolderError(message: string): boolean { return /NONEXISTENT|unknown mailbox|mailbox (?:does not|doesn't) exist|nonexistent namespace|NOSELECT/i.test(message); diff --git a/src/lib/import/jobs.ts b/src/lib/import/jobs.ts index e50d87eec..bd4035b2f 100644 --- a/src/lib/import/jobs.ts +++ b/src/lib/import/jobs.ts @@ -4,7 +4,7 @@ import { importJobs, mailboxes } from "@/db/schema"; import { newId } from "@/lib/ids"; import { getImportMessageUserId, parseImportDestination } from "./destination"; import { fetchImapMessages } from "./imap"; -import { getImapChunkSize, growImapChunkSize, isMissingImapFolderError, isPermanentImapError } from "./imap-utils"; +import { getImapChunkSize, growImapChunkSize, isImapConnectionError, isMissingImapFolderError, isPermanentImapError } from "./imap-utils"; import { deleteStagedImport, importBatchKey, stageImportMessages, unpackImportMessages } from "./staging"; import { importMessagesToMailbox } from "./service"; import type { @@ -24,6 +24,7 @@ const MAX_STORED_ERRORS = 20; const IMAP_CHUNK_SIZE = 50; /** Consecutive failed IMAP chunks, each retried smaller, before the job gives up. */ const MAX_IMAP_FAILURES = 4; +const MAX_IMAP_CONNECTION_FAILURES = 10; const STALE_AFTER_MS = 30 * 60 * 1000; const KEEP_FINISHED_MS = 7 * 24 * 60 * 60 * 1000; @@ -46,8 +47,8 @@ function parseJson(value: string, fallback: T): T { } } -async function enqueue(env: CloudflareEnv, message: ImportJobMessage): Promise { - await env.OUTBOUND_QUEUE.send(message); +async function enqueue(env: CloudflareEnv, message: ImportJobMessage, delaySeconds?: number): Promise { + await env.OUTBOUND_QUEUE.send(message, delaySeconds ? { delaySeconds } : undefined); } export function toImportJobSummary(row: JobRow): ImportJobSummary { @@ -295,16 +296,20 @@ async function retryImapChunkSmaller( if (isPermanentImapError(reason)) return false; const cursor = parseJson(job.cursor, { source: 0, done: 0, counted: false }); const failures = (cursor.failures ?? 0) + 1; - if (failures >= MAX_IMAP_FAILURES) return false; + // A refused or dropped connection says nothing about the batch: hosts such as cPanel throttle repeated + // logins, so back off and keep the size rather than shrinking it and hammering the server again at once. + const refused = isImapConnectionError(reason); + if (failures >= (refused ? MAX_IMAP_CONNECTION_FAILURES : MAX_IMAP_FAILURES)) return false; const spec = parseJson(job.spec, null); const base = spec ? imapBaseChunkSize(spec, cursor) : IMAP_CHUNK_SIZE; - const next: ImapImportCursor = { ...cursor, failures, size: Math.max(1, Math.floor(getImapChunkSize(base, cursor.size) / 2)) }; + const size = refused ? cursor.size : Math.max(1, Math.floor(getImapChunkSize(base, cursor.size) / 2)); + const next: ImapImportCursor = { ...cursor, failures, size }; const recorded = await getDb(env) .update(importJobs) .set({ cursor: JSON.stringify(next), updatedAt: new Date() }) .where(and(eq(importJobs.id, job.id), inArray(importJobs.status, [...ACTIVE]))) .returning({ id: importJobs.id }); - if (recorded.length) await enqueue(env, message); + if (recorded.length) await enqueue(env, message, refused ? Math.min(60 * failures, 600) : undefined); return true; } diff --git a/tests/imap-chunk-size.test.mjs b/tests/imap-chunk-size.test.mjs index 5a91f79bc..c9f0e84b2 100644 --- a/tests/imap-chunk-size.test.mjs +++ b/tests/imap-chunk-size.test.mjs @@ -1,7 +1,7 @@ import assert from "node:assert/strict"; import test from "node:test"; -import { getImapChunkSize, planImapFetchGroups, growImapChunkSize, isPermanentImapError, isMissingImapFolderError } from "../src/lib/import/imap-utils.ts"; +import { getImapChunkSize, planImapFetchGroups, growImapChunkSize, isPermanentImapError, isMissingImapFolderError, isImapConnectionError } from "../src/lib/import/imap-utils.ts"; test("chunk size defaults to the base and never exceeds it", () => { assert.equal(getImapChunkSize(50, undefined), 50); @@ -40,3 +40,10 @@ test("a folder the server cannot open is detected so the import can skip it", () assert.equal(isMissingImapFolderError("IMAP command failed: A0002 NO [NONEXISTENT] Unknown Mailbox: [Gmail] (Failure)"), true); assert.equal(isMissingImapFolderError("IMAP connection closed unexpectedly"), false); }); + +test("connection refusals are told apart from batch failures", () => { + assert.equal(isImapConnectionError("Stream was cancelled."), true); + assert.equal(isImapConnectionError("IMAP connection closed unexpectedly"), true); + assert.equal(isImapConnectionError("IMAP fetch failed: A0005 NO [SERVERBUG]"), false); + assert.equal(isImapConnectionError("IMAP command failed: A0001 NO [AUTHENTICATIONFAILED]"), false); +}); From 27c75d03121979aee62acb4a3f4b9e199ffa7ec6 Mon Sep 17 00:00:00 2001 From: "Hieu Nguyen (Jack)" Date: Thu, 19 Nov 2026 00:08:21 +0700 Subject: [PATCH 2/2] fix(import): shrink the IMAP batch after repeated connection failures Keep the batch size for the first three connection failures, since a throttled host only needs time, then halve it as well: a batch that is too heavy can also drop the connection. The backoff delay keeps growing. --- src/lib/import/imap-utils.ts | 8 ++++++++ src/lib/import/jobs.ts | 6 ++++-- tests/imap-chunk-size.test.mjs | 8 +++++++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/lib/import/imap-utils.ts b/src/lib/import/imap-utils.ts index cede7cd2c..513dbb051 100644 --- a/src/lib/import/imap-utils.ts +++ b/src/lib/import/imap-utils.ts @@ -54,6 +54,14 @@ export function isImapConnectionError(message: string): boolean { return /stream was cancelled|connection closed unexpectedly|did not send an OK greeting|connection (?:reset|refused)|timed? ?out/i.test(message); } +/** Connection failures tolerated at the same batch size before it shrinks too: a throttled host needs time, a batch that is too heavy needs to be smaller. */ +export const IMAP_CONNECTION_KEEP_SIZE_FAILURES = 3; + +/** Whether a connection failure should retry at the same batch size, given how many failed in a row. */ +export function keepsImapChunkSize(failures: number): boolean { + return failures <= IMAP_CONNECTION_KEEP_SIZE_FAILURES; +} + /** A folder the server says does not exist or cannot be selected, such as Gmail's `[Gmail]` parent. */ export function isMissingImapFolderError(message: string): boolean { return /NONEXISTENT|unknown mailbox|mailbox (?:does not|doesn't) exist|nonexistent namespace|NOSELECT/i.test(message); diff --git a/src/lib/import/jobs.ts b/src/lib/import/jobs.ts index bd4035b2f..220b50a1e 100644 --- a/src/lib/import/jobs.ts +++ b/src/lib/import/jobs.ts @@ -4,7 +4,7 @@ import { importJobs, mailboxes } from "@/db/schema"; import { newId } from "@/lib/ids"; import { getImportMessageUserId, parseImportDestination } from "./destination"; import { fetchImapMessages } from "./imap"; -import { getImapChunkSize, growImapChunkSize, isImapConnectionError, isMissingImapFolderError, isPermanentImapError } from "./imap-utils"; +import { getImapChunkSize, growImapChunkSize, isImapConnectionError, isMissingImapFolderError, isPermanentImapError, keepsImapChunkSize } from "./imap-utils"; import { deleteStagedImport, importBatchKey, stageImportMessages, unpackImportMessages } from "./staging"; import { importMessagesToMailbox } from "./service"; import type { @@ -302,7 +302,9 @@ async function retryImapChunkSmaller( if (failures >= (refused ? MAX_IMAP_CONNECTION_FAILURES : MAX_IMAP_FAILURES)) return false; const spec = parseJson(job.spec, null); const base = spec ? imapBaseChunkSize(spec, cursor) : IMAP_CHUNK_SIZE; - const size = refused ? cursor.size : Math.max(1, Math.floor(getImapChunkSize(base, cursor.size) / 2)); + // The first few connection failures keep the size (a throttled host just needs time); later ones also + // shrink it, since a batch that is too heavy can drop the connection too. The delay keeps growing either way. + const size = refused && keepsImapChunkSize(failures) ? cursor.size : Math.max(1, Math.floor(getImapChunkSize(base, cursor.size) / 2)); const next: ImapImportCursor = { ...cursor, failures, size }; const recorded = await getDb(env) .update(importJobs) diff --git a/tests/imap-chunk-size.test.mjs b/tests/imap-chunk-size.test.mjs index c9f0e84b2..597d3926c 100644 --- a/tests/imap-chunk-size.test.mjs +++ b/tests/imap-chunk-size.test.mjs @@ -1,7 +1,7 @@ import assert from "node:assert/strict"; import test from "node:test"; -import { getImapChunkSize, planImapFetchGroups, growImapChunkSize, isPermanentImapError, isMissingImapFolderError, isImapConnectionError } from "../src/lib/import/imap-utils.ts"; +import { getImapChunkSize, planImapFetchGroups, growImapChunkSize, isPermanentImapError, isMissingImapFolderError, isImapConnectionError, keepsImapChunkSize } from "../src/lib/import/imap-utils.ts"; test("chunk size defaults to the base and never exceeds it", () => { assert.equal(getImapChunkSize(50, undefined), 50); @@ -47,3 +47,9 @@ test("connection refusals are told apart from batch failures", () => { assert.equal(isImapConnectionError("IMAP fetch failed: A0005 NO [SERVERBUG]"), false); assert.equal(isImapConnectionError("IMAP command failed: A0001 NO [AUTHENTICATIONFAILED]"), false); }); + +test("connection failures keep the batch size only for the first few attempts", () => { + assert.equal(keepsImapChunkSize(1), true); + assert.equal(keepsImapChunkSize(3), true); + assert.equal(keepsImapChunkSize(4), false); +});