@bobfrankston/rmfmail 1.2.47 → 1.2.49

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -24,15 +24,13 @@ const SMTP_PORT_STARTTLS = 587;
24
24
  const SMTP_PORT_IMPLICIT_TLS = 465;
25
25
 
26
26
  type OpsTask = () => Promise<void>;
27
- interface OpsQueue {
28
- fast: OpsTask[];
29
- slow: OpsTask[];
30
- /** Whether the fast lane is currently draining a task. Independent of
31
- * `runningSlow` both lanes can drain concurrently because each uses
32
- * its own connection (fastClient vs. opsClient). C123: the second
33
- * connection is lazy-allocated on the first fast-lane task. */
34
- runningFast: boolean;
35
- runningSlow: boolean;
27
+ /** One FIFO task queue per (account, lane). All lanes drain concurrently —
28
+ * each lane runs on its own persistent connection — so a slow op on one lane
29
+ * never blocks another. Within a lane, strictly sequential (one IMAP command
30
+ * in flight, no SELECT races). */
31
+ interface LaneQueue {
32
+ tasks: OpsTask[];
33
+ running: boolean;
36
34
  }
37
35
  interface HostSemaphore {
38
36
  permits: number;
@@ -591,39 +589,45 @@ export class ImapManager extends EventEmitter {
591
589
  // No semaphore, no pool, no per-operation connect/disconnect.
592
590
  // IDLE uses a separate connection (see startWatching).
593
591
 
594
- /** Persistent slow-lane operational connection per account. Used by sync,
595
- * prefetch, outbox-append, large backfills, and any other "this might
596
- * take a while" operation. */
597
- private opsClients = new Map<string, any>();
598
- /** Lazy-allocated fast-lane client per account (C123). Lives alongside
599
- * `opsClients` so click-time body fetches and flag toggles don't have
600
- * to wait behind a multi-minute prefetch / backfill on the slow client.
601
- * Created on the first fast-lane `withConnection` call; reused while
602
- * alive; recreated on stale-detect or error-discard like opsClients.
603
- * Costs +1 IMAP socket per active account, well under any reasonable
604
- * per-user-IP cap (Dovecot's default is 20). */
605
- private fastClients = new Map<string, any>();
606
- /** Two-lane operation queue per account interactive ops (body fetch on
607
- * click, flag toggle) drain before background ops (sync, prefetch). FIFO
608
- * within each lane. The single ops connection means there's never a race
609
- * on which folder is SELECTed; commands run strictly sequentially. */
610
- private opsQueues = new Map<string, OpsQueue>();
592
+ /** Per-workload connection lanes. Each (lane, account) gets its OWN
593
+ * persistent IMAP connection so a slow op on one lane can't block another:
594
+ * a 90s `SELECT Sent` on "outbox" or a giant `UID FETCH` on "prefetch" no
595
+ * longer wedges INBOX sync/backfill on "sync". This is the root fix for
596
+ * the "whole sync worker goes silent" wedge (Bob 2026-06-20): before, all
597
+ * background work shared ONE slow connection and serialized behind the
598
+ * slowest command (a 90s SELECT Sent / a giant prefetch FETCH stalled
599
+ * INBOX backfill behind it). Lanes in use:
600
+ * fast — interactive: body fetch on click, flag toggle, move
601
+ * slow — folder sync + set-diff backfill (the `{slow:true}` default)
602
+ * prefetch background body download (heaviest, longest)
603
+ * outbox — send + sent-sweep + Outbox/Sent SELECT/APPEND
604
+ * Per account that's ≤4 persistent sockets + 1 IDLE, under Dovecot's 20.
605
+ * laneName (accountId persistent client). */
606
+ private laneClients = new Map<string, Map<string, any>>();
607
+ /** Per-account, per-lane FIFO task queues. accountId (laneName → queue).
608
+ * All lanes for an account drain concurrently (each on its own socket);
609
+ * within a lane, strictly sequential — no SELECT race. */
610
+ private laneQueues = new Map<string, Map<string, LaneQueue>>();
611
611
  /** Per-host semaphore — caps simultaneous IMAP socket opens to one server.
612
- * Defensive guardrail: with the single-ops-per-account model an individual
613
- * user's mailx never hits more than (#accounts × 2) sockets per host, well
614
- * under any reasonable server cap. Exists for the multi-account-on-one-host
615
- * case (e.g. bobma + bobma2 both on imap.iecc.com). */
612
+ * Defensive guardrail for the multi-account-on-one-host case (e.g. bobma +
613
+ * bobma2 both on imap.iecc.com). NOTE: the permit is held for the
614
+ * connection's LIFETIME (released on logout/destroy), not just the open —
615
+ * so this also bounds the number of live persistent connections per host.
616
+ * Each account now keeps up to 4 persistent lanes (fast/slow/prefetch/
617
+ * outbox) + 1 IDLE (IDLE bypasses the semaphore). 8 permits covers two
618
+ * accounts' lanes with headroom, still far under Dovecot's ~20/user+IP. */
616
619
  private hostSemaphores = new Map<string, HostSemaphore>();
617
- private static readonly HOST_PERMITS = 4;
618
-
619
- /** Get (or create) a persistent connection for an account on the named
620
- * lane. Two lanes today: `slow` (the original `opsClients` map — sync,
621
- * prefetch, backfill, outbox) and `fast` (the C123 `fastClients` map —
622
- * click-time body fetch, flag toggles, anything user-driven). Same
623
- * stale-detect + reconnect semantics on both. logout() is wrapped as a
624
- * no-op so legacy callers don't close the persistent client. */
625
- private async getLaneClient(accountId: string, lane: "fast" | "slow"): Promise<any> {
626
- const map = lane === "fast" ? this.fastClients : this.opsClients;
620
+ private static readonly HOST_PERMITS = 8;
621
+
622
+ /** The (accountId → client) map for a lane, created on first use. */
623
+ private laneClientMap(lane: string): Map<string, any> {
624
+ let m = this.laneClients.get(lane);
625
+ if (!m) { m = new Map(); this.laneClients.set(lane, m); }
626
+ return m;
627
+ }
628
+
629
+ private async getLaneClient(accountId: string, lane: string): Promise<any> {
630
+ const map = this.laneClientMap(lane);
627
631
  let client = map.get(accountId);
628
632
  if (client) {
629
633
  // C38: health-check the cached client before returning. If the
@@ -640,7 +644,7 @@ export class ImapManager extends EventEmitter {
640
644
  console.log(` [conn] ${accountId}: stale ${lane} client detected — reconnecting`);
641
645
  client = undefined as any;
642
646
  }
643
- client = await this.newClient(accountId, lane === "fast" ? "fast" : "ops");
647
+ client = await this.newClient(accountId, lane);
644
648
  // Wrap logout as no-op — this is a persistent connection. The
645
649
  // newClient wrapper's close-counter runs on `_realLogout`.
646
650
  const realLogout = client.logout.bind(client);
@@ -657,33 +661,30 @@ export class ImapManager extends EventEmitter {
657
661
  return this.getLaneClient(accountId, "slow");
658
662
  }
659
663
 
660
- /** Run an operation on the account's connection — queued, sequential, no concurrency */
661
- /** Run an operation against the account's single ops connection. Tasks
662
- * queue strictly sequentially per account only one IMAP command in
663
- * flight at a time. This eliminates the SELECT-races and "stale client
664
- * recovery" paths the old multi-client design needed.
664
+ /** Run an operation against one of the account's per-workload lane
665
+ * connections. Tasks queue strictly sequentially WITHIN a lane (one IMAP
666
+ * command in flight, no SELECT race) but lanes run CONCURRENTLY, each on
667
+ * its own socket — so a slow op on one lane never blocks another.
665
668
  *
666
- * Default lane is `fast` covers virtually everything (body fetch,
667
- * flag toggle, move, incremental sync). Pass `slow: true` only for
668
- * operations the caller knows will take a long time and shouldn't
669
- * block the user (multi-folder prefetch batches, large backfills).
670
- * When both lanes have tasks, fast drains first.
669
+ * Pick the lane via `opts.lane` (e.g. "sync", "prefetch", "outbox").
670
+ * Default is "fast" interactive ops (body fetch on click, flag toggle,
671
+ * move). `slow: true` is a legacy alias for the "slow" misc lane; prefer
672
+ * a named lane so distinct workloads don't serialize behind each other.
671
673
  *
672
- * Within a lane, FIFO. The running task always finishes — IMAP can't
673
- * preempt a command mid-flight. */
674
+ * The running task always finishes — IMAP can't preempt a command
675
+ * mid-flight; the wall-clock timeout below bounds how long that can be. */
674
676
  async withConnection<T>(
675
677
  accountId: string,
676
678
  fn: (client: any) => Promise<T>,
677
- opts: { slow?: boolean; timeoutMs?: number } = {},
679
+ opts: { slow?: boolean; lane?: string; timeoutMs?: number } = {},
678
680
  ): Promise<T> {
679
- let queue = this.opsQueues.get(accountId);
680
- if (!queue) {
681
- queue = { fast: [], slow: [], runningFast: false, runningSlow: false };
682
- this.opsQueues.set(accountId, queue);
683
- }
684
- const lane: "fast" | "slow" = opts.slow ? "slow" : "fast";
681
+ const lane: string = opts.lane ?? (opts.slow ? "slow" : "fast");
682
+ let perAccount = this.laneQueues.get(accountId);
683
+ if (!perAccount) { perAccount = new Map(); this.laneQueues.set(accountId, perAccount); }
684
+ let queue = perAccount.get(lane);
685
+ if (!queue) { queue = { tasks: [], running: false }; perAccount.set(lane, queue); }
685
686
  // Per-task wall-clock cap. Without one, a wedged IMAP command (TCP
686
- // half-open, server stalled mid-FETCH) keeps the queue's running flag
687
+ // half-open, server stalled mid-FETCH) keeps the lane's running flag
687
688
  // set forever and every subsequent same-lane task — including the
688
689
  // retry button the user just hit — waits behind it. Default is
689
690
  // generous; callers driving user-visible reads pass a tighter value.
@@ -703,13 +704,12 @@ export class ImapManager extends EventEmitter {
703
704
  resolve(result);
704
705
  } catch (e: any) {
705
706
  clearTimeout(timer);
706
- // Discard the lane's client on any error — a half-broken
707
- // socket poisons every subsequent request on that lane.
708
- // Don't touch the OTHER lane's client; the lanes are
709
- // independent. Destroy synchronously kills the in-flight
710
- // command's socket so the underlying promise rejects and
711
- // stops holding state.
712
- const map = lane === "fast" ? this.fastClients : this.opsClients;
707
+ // Discard THIS lane's client on any error — a half-broken
708
+ // socket poisons every subsequent request on the lane.
709
+ // Other lanes' clients are independent and untouched.
710
+ // Destroy synchronously kills the in-flight command's
711
+ // socket so the underlying promise rejects.
712
+ const map = this.laneClientMap(lane);
713
713
  const stale = map.get(accountId);
714
714
  map.delete(accountId);
715
715
  if (stale) {
@@ -719,31 +719,27 @@ export class ImapManager extends EventEmitter {
719
719
  reject(e);
720
720
  }
721
721
  };
722
- queue![lane].push(task);
722
+ queue!.tasks.push(task);
723
723
  this.drainOpsQueue(accountId);
724
724
  });
725
725
  }
726
726
 
727
- /** Run the next queued task on each lane. Fast and slow lanes drain
728
- * CONCURRENTLY — each on its own connection (C123). FIFO within each
729
- * lane. The running flag per lane prevents reentrant draining of the
730
- * same lane. */
727
+ /** Drain the next queued task on EVERY lane for the account. Lanes run
728
+ * concurrently — each on its own connection and the per-lane running
729
+ * flag prevents reentrant draining of the same lane. */
731
730
  private drainOpsQueue(accountId: string): void {
732
- const queue = this.opsQueues.get(accountId);
733
- if (!queue) return;
734
- const drainLane = (lane: "fast" | "slow"): void => {
735
- const runningKey = lane === "fast" ? "runningFast" : "runningSlow";
736
- if (queue[runningKey]) return;
737
- const next = queue[lane].shift();
738
- if (!next) return;
739
- queue[runningKey] = true;
731
+ const perAccount = this.laneQueues.get(accountId);
732
+ if (!perAccount) return;
733
+ for (const queue of perAccount.values()) {
734
+ if (queue.running) continue;
735
+ const next = queue.tasks.shift();
736
+ if (!next) continue;
737
+ queue.running = true;
740
738
  next().finally(() => {
741
- queue[runningKey] = false;
742
- drainLane(lane);
739
+ queue.running = false;
740
+ this.drainOpsQueue(accountId);
743
741
  });
744
- };
745
- drainLane("fast");
746
- drainLane("slow");
742
+ }
747
743
  }
748
744
 
749
745
  /** Acquire one slot of the per-host connection semaphore. Returns a release
@@ -853,7 +849,7 @@ export class ImapManager extends EventEmitter {
853
849
  * so the server's connection slots free immediately rather than
854
850
  * waiting for socket idle timeouts. */
855
851
  async closeAllClients(accountId: string): Promise<void> {
856
- for (const map of [this.opsClients, this.fastClients]) {
852
+ for (const map of this.laneClients.values()) {
857
853
  const c = map.get(accountId);
858
854
  map.delete(accountId);
859
855
  if (c) { try { await (c._realLogout || c.logout)(); } catch { /* */ } try { c.destroy?.(); } catch { /* */ } }
@@ -871,7 +867,7 @@ export class ImapManager extends EventEmitter {
871
867
  /** Disconnect the persistent operational connections (both lanes) for
872
868
  * an account. */
873
869
  async disconnectOps(accountId: string): Promise<void> {
874
- for (const [name, map] of [["ops", this.opsClients], ["fast", this.fastClients]] as const) {
870
+ for (const [name, map] of this.laneClients) {
875
871
  const client = map.get(accountId);
876
872
  map.delete(accountId);
877
873
  if (client) {
@@ -1383,8 +1379,16 @@ export class ImapManager extends EventEmitter {
1383
1379
  // recent under stable UIDVALIDITY, so prefer newest when capping — the
1384
1380
  // user cares most about recent mail and the deficit gate keeps calling
1385
1381
  // us until the backlog is drained.
1382
+ //
1383
+ // Kept deliberately small (Bob 2026-06-20): each backfilled batch feeds
1384
+ // the body-prefetcher, which issues large UID FETCHes on the single
1385
+ // shared slow-lane connection. A 5000-message batch flooded that
1386
+ // connection (cmd-chain waits of 70s+, 90s FETCH timeouts) and wedged
1387
+ // the whole sync worker. A small batch per cycle keeps prefetch's queue
1388
+ // shallow so the connection stays responsive; the deficit gate drains
1389
+ // the backlog over more cycles instead of one connection-killing burst.
1386
1390
  const BACKFILL_CHUNK_SIZE = 100;
1387
- const RECONCILE_FETCH_CAP = 5000;
1391
+ const RECONCILE_FETCH_CAP = 1000;
1388
1392
  let toFetch = missingUids;
1389
1393
  if (missingUids.length > RECONCILE_FETCH_CAP) {
1390
1394
  console.log(` ${folderPath}: ${missingUids.length} server-only UIDs — capped at ${RECONCILE_FETCH_CAP}; resumes next cycle`);
@@ -2516,12 +2520,15 @@ export class ImapManager extends EventEmitter {
2516
2520
  return stored;
2517
2521
  }
2518
2522
 
2519
- /** Kill and recreate the persistent ops connection */
2523
+ /** Kill and recreate the persistent connections for an account — drops the
2524
+ * client on every lane so the next task on each reconnects fresh. */
2520
2525
  private async reconnectOps(accountId: string): Promise<void> {
2521
- const old = this.opsClients.get(accountId);
2522
- this.opsClients.delete(accountId);
2523
- if (old) { try { await (old._realLogout || old.logout)(); } catch { /* */ } }
2524
- console.log(` [conn] ${accountId}: reconnecting`);
2526
+ for (const map of this.laneClients.values()) {
2527
+ const old = map.get(accountId);
2528
+ map.delete(accountId);
2529
+ if (old) { try { await (old._realLogout || old.logout)(); } catch { /* */ } }
2530
+ }
2531
+ console.log(` [conn] ${accountId}: reconnecting (all lanes)`);
2525
2532
  }
2526
2533
 
2527
2534
  /** Handle sync errors — classify and emit appropriate UI events.
@@ -3554,7 +3561,7 @@ export class ImapManager extends EventEmitter {
3554
3561
  })());
3555
3562
  });
3556
3563
  await Promise.all(pending);
3557
- }, { slow: true, timeoutMs: 300_000 });
3564
+ }, { lane: "prefetch", timeoutMs: 300_000 });
3558
3565
  // 5-min cap, not the 90s interactive default:
3559
3566
  // prefetch is background, and a 25-message body
3560
3567
  // chunk on a slow Dovecot legitimately runs past
@@ -4318,7 +4325,7 @@ export class ImapManager extends EventEmitter {
4318
4325
  await this.withConnection(accountId, async (client) => {
4319
4326
  await client.createmailbox("Outbox");
4320
4327
  await this.syncFolders(accountId, client);
4321
- });
4328
+ }, { lane: "outbox" });
4322
4329
  } catch (e: any) {
4323
4330
  // Might already exist — benign
4324
4331
  if (!e.message?.includes("already exists")) throw e;
@@ -4429,7 +4436,7 @@ export class ImapManager extends EventEmitter {
4429
4436
  await this.withConnection(accountId, async (client) => {
4430
4437
  appendedUid = await client.appendMessage(outboxPath, rawMessage, ["\\Seen"]);
4431
4438
  console.log(` [outbox] Queued message in ${outboxPath}${appendedUid != null ? ` (UID ${appendedUid})` : ""}`);
4432
- });
4439
+ }, { lane: "outbox" });
4433
4440
  if (outboxFolder) {
4434
4441
  if (appendedUid != null) {
4435
4442
  // Inserts the row from the source bytes we already have +
@@ -4636,7 +4643,9 @@ export class ImapManager extends EventEmitter {
4636
4643
  // claim so the recovery sweeper picks it up next tick.
4637
4644
  try {
4638
4645
  const outboxPath = await this.ensureOutbox(accountId);
4639
- const client = await this.createClientWithLimit(accountId);
4646
+ // Outbox lane, not the slow/sync lane — a wedged APPEND here (which
4647
+ // withTimeout force-closes the socket on) must not disrupt sync.
4648
+ const client = await this.getLaneClient(accountId, "outbox");
4640
4649
  try {
4641
4650
  for (const { dir, file } of filesToSend) {
4642
4651
  const filePath = path.join(dir, file);
@@ -4772,9 +4781,9 @@ export class ImapManager extends EventEmitter {
4772
4781
  const account = settings.accounts.find(a => a.id === accountId);
4773
4782
  if (!account) return;
4774
4783
 
4775
- // List UIDs first — quick command, fast lane.
4784
+ // List UIDs first — quick command on the dedicated outbox lane.
4776
4785
  const uids = await this.withConnection(accountId, (client) =>
4777
- client.getUids(outboxFolder.path)
4786
+ client.getUids(outboxFolder.path), { lane: "outbox" }
4778
4787
  ) as number[];
4779
4788
  if (uids.length === 0) return;
4780
4789
 
@@ -4784,9 +4793,9 @@ export class ImapManager extends EventEmitter {
4784
4793
  const sentFolder = this.findFolder(accountId, "sent");
4785
4794
 
4786
4795
  for (const uid of uids) {
4787
- // Each iteration is one slow-lane turn — fast-lane work can run
4788
- // between iterations, so a body click during a long outbox drain
4789
- // gets serviced promptly.
4796
+ // Each iteration is one outbox-lane turn — sync, prefetch and
4797
+ // fast-lane (click) work all run on their own connections, so a
4798
+ // long outbox drain never blocks them.
4790
4799
  const result = await this.withConnection(accountId, async (client) => {
4791
4800
  const flags = await client.getFlags(outboxFolder.path, uid);
4792
4801
 
@@ -4834,7 +4843,7 @@ export class ImapManager extends EventEmitter {
4834
4843
  return { skip: true };
4835
4844
  }
4836
4845
  return { source: msg.source };
4837
- }, { slow: true });
4846
+ }, { lane: "outbox" });
4838
4847
 
4839
4848
  if ((result as any).skip) continue;
4840
4849
  const source = (result as any).source as string;
@@ -4850,13 +4859,13 @@ export class ImapManager extends EventEmitter {
4850
4859
  await this.withConnection(accountId, async (client) => {
4851
4860
  // Delete FIRST to prevent double-send if Sent-copy fails.
4852
4861
  await client.deleteMessageByUid(outboxFolder.path, uid);
4853
- }, { slow: true });
4862
+ }, { lane: "outbox" });
4854
4863
  if (sentFolder) {
4855
4864
  let appendedSentUid: number | null = null;
4856
4865
  try {
4857
4866
  await this.withConnection(accountId, async (client) => {
4858
4867
  appendedSentUid = await client.appendMessage(sentFolder.path, source, ["\\Seen"]);
4859
- }, { slow: true });
4868
+ }, { lane: "outbox" });
4860
4869
  } catch (sentErr: any) {
4861
4870
  console.error(` [outbox] Failed to copy to Sent: ${sentErr.message} — message was sent successfully`);
4862
4871
  }
@@ -4886,7 +4895,7 @@ export class ImapManager extends EventEmitter {
4886
4895
  await this.withConnection(accountId, async (client) => {
4887
4896
  await client.removeFlags(outboxFolder.path, uid, [sendingFlag]);
4888
4897
  await client.addFlags(outboxFolder.path, uid, ["$Failed"]);
4889
- }, { slow: true });
4898
+ }, { lane: "outbox" });
4890
4899
  } catch { /* best-effort */ }
4891
4900
  // Suppress the banner for transient network errors. ETIMEDOUT
4892
4901
  // on a Dovecot send is the server being slow / a TCP idle
@@ -5025,10 +5034,10 @@ export class ImapManager extends EventEmitter {
5025
5034
  this.outboxBackoffDelay.delete(accountId);
5026
5035
  } catch (e: any) {
5027
5036
  // Stale-socket errors (Dovecot silently drops idle connections,
5028
- // or the sync path timed out and destroyed the socket): force a
5029
- // fresh ops client so the next tick doesn't keep hitting the same
5030
- // dead socket. Without reconnectOps, the dead client stays in the
5031
- // opsClients map and every subsequent processOutbox call fails
5037
+ // or the sync path timed out and destroyed the socket): force
5038
+ // fresh lane clients so the next tick doesn't keep hitting the
5039
+ // same dead socket. Without reconnectOps, the dead client stays
5040
+ // in its lane map and every subsequent processOutbox call fails
5032
5041
  // immediately with "Not connected" — forever.
5033
5042
  const msg = String(e?.message || e);
5034
5043
  if (/Not connected|ECONNRESET|socket hang up|EPIPE|write after end/i.test(msg)) {
@@ -5128,19 +5137,18 @@ export class ImapManager extends EventEmitter {
5128
5137
  if (rows.length === 0) return;
5129
5138
  let reconciled = 0;
5130
5139
  let appended = 0;
5131
- // Use a DEDICATED IMAP client for sent-sweep do NOT share the
5132
- // slow-lane ops client. The priority-INBOX sync grabs the slow-lane
5133
- // client via getOpsClient() (no lane queueing) and runs SEARCH→FETCH
5134
- // as a logical transaction; if sent-sweep's searchByHeader (SELECT
5135
- // Sent SEARCH CLOSE) interleaves on the same wire between the
5136
- // sync's SEARCH and FETCH, the FETCH lands with no mailbox selected
5137
- // and Dovecot returns "BAD No mailbox selected" — new mail never
5138
- // lands locally (Bob 2026-05-25: "not seeing recent mail").
5139
- // iflow-direct serializes per-command, not per-task, so reusing the
5140
- // shared client is unsafe across logical transactions. A dedicated
5141
- // client is the smallest surgical fix.
5142
- const client = await this.createClientWithLimit(accountId);
5143
- try {
5140
+ // Run the whole sweep on the dedicated "outbox" lane its OWN
5141
+ // persistent connection, separate from the slow/sync lane. Two reasons:
5142
+ // 1. Sent-sweep's `SELECT Sent` has been observed taking 90s on this
5143
+ // server; on the shared slow lane that stalled INBOX sync/backfill
5144
+ // behind it (the "whole worker goes silent" wedge, Bob 2026-06-20).
5145
+ // 2. withConnection serializes the entire SEARCH→APPEND sweep on one
5146
+ // connection, so it can't interleave mid-transaction with another
5147
+ // task's SELECT (the "BAD No mailbox selected" class, Bob
5148
+ // 2026-05-25). Previously this used createClientWithLimit() which
5149
+ // actually returned the SHARED slow client and then destroyed it in
5150
+ // a finally, the opposite of "dedicated".
5151
+ await this.withConnection(accountId, async (client) => {
5144
5152
  for (const row of rows) {
5145
5153
  const msgId = row.message_id;
5146
5154
  if (!msgId) continue;
@@ -5177,10 +5185,7 @@ export class ImapManager extends EventEmitter {
5177
5185
  }
5178
5186
  }
5179
5187
  }
5180
- } finally {
5181
- try { await (client._realLogout || client.logout)(); } catch { /* */ }
5182
- try { client.destroy?.(); } catch { /* */ }
5183
- }
5188
+ }, { lane: "outbox", timeoutMs: 120_000 });
5184
5189
  if (reconciled + appended > 0) {
5185
5190
  this.emit("folderCountsChanged", accountId, {});
5186
5191
  console.log(` [sent-sweep] ${accountId}: ${reconciled} rebound, ${appended} re-appended`);
@@ -5461,13 +5466,13 @@ export class ImapManager extends EventEmitter {
5461
5466
  this.stopPeriodicSync();
5462
5467
  this.stopOutboxWorker();
5463
5468
  await this.stopWatching();
5464
- // Disconnect persistent connections on both lanes. Use a Set
5465
- // because fastClients can hold an entry an account doesn't have
5466
- // in opsClients (e.g. body fetches happened but no sync did).
5467
- const accountIds = new Set<string>([
5468
- ...this.opsClients.keys(),
5469
- ...this.fastClients.keys(),
5470
- ]);
5469
+ // Disconnect persistent connections on every lane. Union across all
5470
+ // lane maps because a lane can hold an account the others don't (e.g.
5471
+ // body fetches happened on "fast" but no sync ran on "sync").
5472
+ const accountIds = new Set<string>();
5473
+ for (const map of this.laneClients.values()) {
5474
+ for (const k of map.keys()) accountIds.add(k);
5475
+ }
5471
5476
  for (const accountId of accountIds) {
5472
5477
  await this.disconnectOps(accountId);
5473
5478
  }
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@bobfrankston/mailx-imap",
3
- "version": "0.1.103",
3
+ "version": "0.1.105",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "@bobfrankston/mailx-imap",
9
- "version": "0.1.103",
9
+ "version": "0.1.105",
10
10
  "license": "ISC",
11
11
  "dependencies": {
12
12
  "@bobfrankston/iflow-direct": "^0.1.27",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bobfrankston/mailx-imap",
3
- "version": "0.1.103",
3
+ "version": "0.1.105",
4
4
  "type": "module",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",