emailengine-app 2.72.3 → 2.72.4
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.
- package/CHANGELOG.md +31 -0
- package/data/google-crawlers.json +1 -1
- package/lib/email-client/imap/mailbox.js +137 -30
- package/lib/email-client/imap/subconnection.js +105 -36
- package/lib/email-client/imap-client.js +331 -165
- package/lib/email-client/reconnect-backoff.js +38 -0
- package/package.json +8 -8
- package/sbom.json +1 -1
- package/static/licenses.html +29 -29
- package/translations/messages.pot +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,36 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [2.72.4](https://github.com/postalsys/emailengine/compare/v2.72.3...v2.72.4) (2026-07-11)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Bug Fixes
|
|
7
|
+
|
|
8
|
+
* bump imapflow/bullmq/bull-board/sentry-node/eerawlog/prettier ([8f7b6bf](https://github.com/postalsys/emailengine/commit/8f7b6bf21eae70b1f3c8e0856a6c0f0a0d641317))
|
|
9
|
+
* clear the connecting flag if loading account data fails during reconnect ([8d82167](https://github.com/postalsys/emailengine/commit/8d8216730f809eebdc8a0762e38f93f0084de038))
|
|
10
|
+
* clear the pending changes debounce when closing a subconnection ([a286f89](https://github.com/postalsys/emailengine/commit/a286f89e721336b64a9c16737cf30d161f52fe22))
|
|
11
|
+
* disable subconnection when the monitored mailbox no longer exists ([e1895d6](https://github.com/postalsys/emailengine/commit/e1895d64b5fd17912f582406c7c352b9176364c6))
|
|
12
|
+
* do not report connectError when pause() interrupts a connection attempt ([4d38e4f](https://github.com/postalsys/emailengine/commit/4d38e4f047be16f4f5a16a0274c5240346c2b50f))
|
|
13
|
+
* drop the reconnect timer handle when closing an IMAP connection ([9f46c77](https://github.com/postalsys/emailengine/commit/9f46c77afef8217a8b6b69e736cb0cd38b27429b))
|
|
14
|
+
* ignore \NonExistent folders in mailbox listing ([7ec96cf](https://github.com/postalsys/emailengine/commit/7ec96cfaaf5425c16479151e66ef3285c854b49a))
|
|
15
|
+
* prevent overlapping and revived subconnection reconnect cycles ([5754c07](https://github.com/postalsys/emailengine/commit/5754c074f0f8c89222008e4c1d331b66891b3c16))
|
|
16
|
+
* process refreshed folder listings so new folders emit mailboxNew ([247c325](https://github.com/postalsys/emailengine/commit/247c325cc71f1842cc8083c7fd025792217c77d8))
|
|
17
|
+
* reconcile disabled and active subconnections in setupSubConnections ([85ebb54](https://github.com/postalsys/emailengine/commit/85ebb54bae8d4d7321a7362bb20e570a149fe892))
|
|
18
|
+
* refresh folder listing when a listed mailbox is missing on SELECT ([1e54d32](https://github.com/postalsys/emailengine/commit/1e54d32c1c74cb1f6d3d085a75d5b5ecc1cf5ff2))
|
|
19
|
+
* require an exact path match in the missing-mailbox verification LIST ([54e23c4](https://github.com/postalsys/emailengine/commit/54e23c4f6f13d3d834348df4bf15cf11206dc58b))
|
|
20
|
+
* reset IMAP reconnection backoff only after a successful sync ([7138dab](https://github.com/postalsys/emailengine/commit/7138dabab866483818440478d3584a46dfc8de12))
|
|
21
|
+
* reset subconnection backoff only after the monitored mailbox is opened ([f2bf297](https://github.com/postalsys/emailengine/commit/f2bf297883c38f4d1f61197ae75bdd7ae3976a53))
|
|
22
|
+
* resolve the mailbox sync promise when a concurrent operation already selected the mailbox ([7327dcc](https://github.com/postalsys/emailengine/commit/7327dcc9479a991881d3ddafdd0f36cc69e3411d))
|
|
23
|
+
* resolve the mailbox sync promise when opening fails before the sync starts ([c2268af](https://github.com/postalsys/emailengine/commit/c2268af90bea13d5b804c0a84945801cd9d74e45))
|
|
24
|
+
* retry transient account data load failures during reconnect ([7f18d4b](https://github.com/postalsys/emailengine/commit/7f18d4b16ccdfcfde77eb433530636b8657a354a))
|
|
25
|
+
* revive missing-folder subconnections when the folder is re-created ([254fce4](https://github.com/postalsys/emailengine/commit/254fce4f0eca59bc52ed4a8235f581b8107b97a7))
|
|
26
|
+
* scope the re-entrant open latch to the connection that emitted the event ([627b6ee](https://github.com/postalsys/emailengine/commit/627b6eeaf9ed493f6a4d81adcbe5b766eba54682))
|
|
27
|
+
* skip mailboxes that can not be selected during account sync ([eaf09c1](https://github.com/postalsys/emailengine/commit/eaf09c1700eca77e27fada20371bad193e0665c8))
|
|
28
|
+
* skip re-entrant mailbox open events while onOpen is already running ([6d06b09](https://github.com/postalsys/emailengine/commit/6d06b09c0c573a2f8ca554ba85c75d6b7be87002))
|
|
29
|
+
* stop opening IMAP connections for accounts paused while connecting ([128aef3](https://github.com/postalsys/emailengine/commit/128aef3cf2b7f1485a2e3d291436e88183255c0e))
|
|
30
|
+
* untrack bailed subconnection clients and harden the revival trigger ([086a585](https://github.com/postalsys/emailengine/commit/086a585541d3c6d751ebfbfc34f8f15397d51407))
|
|
31
|
+
* untrack the IMAP client when connect() bails for a paused account ([cface66](https://github.com/postalsys/emailengine/commit/cface665f16d7ad00f7b657e07f3c3f77c3401ef))
|
|
32
|
+
* update connection tracking when replacing a previous IMAP client ([8e143c2](https://github.com/postalsys/emailengine/commit/8e143c281d03d3d226e65eaae32eab96b1db0671))
|
|
33
|
+
|
|
3
34
|
## [2.72.3](https://github.com/postalsys/emailengine/compare/v2.72.2...v2.72.3) (2026-07-06)
|
|
4
35
|
|
|
5
36
|
|
|
@@ -82,6 +82,8 @@ class Mailbox {
|
|
|
82
82
|
|
|
83
83
|
this.synced = false; // Whether initial sync is complete
|
|
84
84
|
this.syncing = false; // Whether currently syncing
|
|
85
|
+
this.processingOpen = false; // Whether onOpen() is currently running for this mailbox
|
|
86
|
+
this.processingOpenClient = null; // The IMAP client whose open event holds the processingOpen latch
|
|
85
87
|
|
|
86
88
|
// Sync operations handler
|
|
87
89
|
this.syncOps = new SyncOperations(this);
|
|
@@ -442,8 +444,10 @@ class Mailbox {
|
|
|
442
444
|
});
|
|
443
445
|
} catch (err) {
|
|
444
446
|
if (err.code === 'NotFound') {
|
|
445
|
-
//
|
|
446
|
-
|
|
447
|
+
// Folder is missing, refresh the folder listing and process the
|
|
448
|
+
// result so deletions get cleared and newly found folders are
|
|
449
|
+
// registered and synced (emitting their mailboxNew notification)
|
|
450
|
+
await this.connection.refreshAndProcessListing();
|
|
447
451
|
return;
|
|
448
452
|
} else {
|
|
449
453
|
throw err;
|
|
@@ -488,13 +492,55 @@ class Mailbox {
|
|
|
488
492
|
}
|
|
489
493
|
}
|
|
490
494
|
|
|
495
|
+
// Re-check after the awaits above: a concurrent operation may have
|
|
496
|
+
// selected this mailbox while we were reading STATUS / stored state.
|
|
497
|
+
// In that case onOpen() owns the sync work, and select(true) below
|
|
498
|
+
// would skip the SELECT without another mailboxOpen event ever firing
|
|
499
|
+
if (this.selected || !this.connection.imapClient) {
|
|
500
|
+
// expect current folder to be already synced
|
|
501
|
+
return false;
|
|
502
|
+
}
|
|
503
|
+
|
|
491
504
|
// Need to sync - create promise that resolves when sync is complete
|
|
492
505
|
let syncedPromise = new Promise((resolve, reject) => {
|
|
493
506
|
this.synced = resolve;
|
|
494
507
|
this.select(true).catch(err => reject(err));
|
|
495
508
|
});
|
|
496
509
|
|
|
497
|
-
|
|
510
|
+
try {
|
|
511
|
+
await syncedPromise;
|
|
512
|
+
} catch (err) {
|
|
513
|
+
if (err.mailboxMissing) {
|
|
514
|
+
// The folder was deleted after it was listed (e.g. by another client
|
|
515
|
+
// between LIST and SELECT). Refresh the listing right away so the
|
|
516
|
+
// deletion gets processed instead of waiting for the next resync cycle,
|
|
517
|
+
// and process the result so folders that appeared in the same change
|
|
518
|
+
// (e.g. a rename) are registered and emit their mailboxNew notification.
|
|
519
|
+
// A failing refresh is a connection-level error and propagates, same
|
|
520
|
+
// as in the NotFound branch above.
|
|
521
|
+
this.logger.info({ msg: 'Mailbox is missing, refreshing folder listing', path: this.path });
|
|
522
|
+
this.synced = false;
|
|
523
|
+
await this.connection.refreshAndProcessListing();
|
|
524
|
+
return;
|
|
525
|
+
}
|
|
526
|
+
if (err.responseStatus === 'NO') {
|
|
527
|
+
// The folder is listed but can not be selected. Either a permanent
|
|
528
|
+
// phantom folder (e.g. a shared namespace root that the server lists
|
|
529
|
+
// without \Noselect) or a transient RFC 5530 condition like INUSE -
|
|
530
|
+
// both are skipped and retried on the next resync cycle instead of
|
|
531
|
+
// failing the entire account sync. Network-level errors do not have
|
|
532
|
+
// responseStatus set and keep propagating.
|
|
533
|
+
this.logger.warn({
|
|
534
|
+
msg: 'Skipped mailbox that can not be selected',
|
|
535
|
+
path: this.path,
|
|
536
|
+
serverResponseCode: err.serverResponseCode,
|
|
537
|
+
responseText: err.responseText
|
|
538
|
+
});
|
|
539
|
+
this.synced = false;
|
|
540
|
+
return;
|
|
541
|
+
}
|
|
542
|
+
throw err;
|
|
543
|
+
}
|
|
498
544
|
}
|
|
499
545
|
|
|
500
546
|
/**
|
|
@@ -521,6 +567,14 @@ class Mailbox {
|
|
|
521
567
|
...(currentLock.options?.description && { description: currentLock.options?.description })
|
|
522
568
|
}
|
|
523
569
|
});
|
|
570
|
+
if (this.synced) {
|
|
571
|
+
// Locks are only granted after the mailbox is open, so no
|
|
572
|
+
// further mailboxOpen event is coming for this path. Settle
|
|
573
|
+
// the resolver armed by sync() instead of leaving it waiting
|
|
574
|
+
// forever. If the open's onOpen() is still running, its own
|
|
575
|
+
// resolve call later is a no-op on the settled promise
|
|
576
|
+
this.synced();
|
|
577
|
+
}
|
|
524
578
|
return;
|
|
525
579
|
}
|
|
526
580
|
// Different mailbox is locked - queue this select to run after lock is released
|
|
@@ -543,6 +597,12 @@ class Mailbox {
|
|
|
543
597
|
msg: 'Mailbox already selected after lock acquired',
|
|
544
598
|
path: this.path
|
|
545
599
|
});
|
|
600
|
+
if (this.synced) {
|
|
601
|
+
// The lock was granted against the already-open mailbox without
|
|
602
|
+
// a new SELECT, so no mailboxOpen event fires - settle the
|
|
603
|
+
// resolver armed by sync() instead of leaving it waiting forever
|
|
604
|
+
this.synced();
|
|
605
|
+
}
|
|
546
606
|
lock.release();
|
|
547
607
|
return;
|
|
548
608
|
}
|
|
@@ -2064,16 +2124,48 @@ class Mailbox {
|
|
|
2064
2124
|
* Called when mailbox is opened/selected
|
|
2065
2125
|
* Determines what type of sync is needed based on current state
|
|
2066
2126
|
*/
|
|
2067
|
-
async onOpen() {
|
|
2127
|
+
async onOpen(event, openedClient) {
|
|
2068
2128
|
clearTimeout(this.runPartialSyncTimer);
|
|
2069
2129
|
this.selected = true;
|
|
2070
2130
|
|
|
2071
|
-
//
|
|
2072
|
-
|
|
2131
|
+
// The latch is scoped to the connection that emitted the open event.
|
|
2132
|
+
// A re-entrant event from the SAME connection must bail out, but an
|
|
2133
|
+
// event from a NEW connection must not be swallowed while a previous
|
|
2134
|
+
// connection's handler is still unwinding (its Redis-heavy phases keep
|
|
2135
|
+
// running after the socket died) - settling the fresh resolver here
|
|
2136
|
+
// would mark the post-reconnect sync as done without any sync work
|
|
2137
|
+
openedClient = openedClient || this.connection.imapClient;
|
|
2138
|
+
|
|
2139
|
+
if (this.processingOpen && this.processingOpenClient === openedClient) {
|
|
2140
|
+
// Re-entrant open event: a sync operation running inside this very
|
|
2141
|
+
// handler (e.g. seedMailboxIndex acquiring its mailbox lock)
|
|
2142
|
+
// re-SELECTed this mailbox. Running the state machine again from
|
|
2143
|
+
// here recurses - on a server where the seeding FETCH keeps
|
|
2144
|
+
// failing it becomes an endless reseed storm of back-to-back
|
|
2145
|
+
// SELECTs. The outer invocation owns the sync work; settle an
|
|
2146
|
+
// armed sync() resolver and bail out
|
|
2147
|
+
if (this.synced) {
|
|
2148
|
+
this.synced();
|
|
2149
|
+
}
|
|
2150
|
+
return false;
|
|
2151
|
+
}
|
|
2152
|
+
// Take the latch (over) - if a previous connection's handler is still
|
|
2153
|
+
// unwinding, it detects the new owner in its finally and backs off
|
|
2154
|
+
this.processingOpen = true;
|
|
2155
|
+
this.processingOpenClient = openedClient;
|
|
2073
2156
|
|
|
2074
|
-
|
|
2157
|
+
// Assigned inside the try - everything that can throw must run there,
|
|
2158
|
+
// because the finally below is the sole resolver of the sync() promise
|
|
2159
|
+
// (this.synced). A throw before the try would skip the finally and
|
|
2160
|
+
// permanently wedge the awaiting sync()
|
|
2161
|
+
let mailboxStatus;
|
|
2075
2162
|
|
|
2076
2163
|
try {
|
|
2164
|
+
// Track connection count to detect reconnects
|
|
2165
|
+
this.previouslyConnected = Number(await this.connection.redis.hget(this.connection.getAccountKey(), `state:count:connected`)) || 0;
|
|
2166
|
+
|
|
2167
|
+
mailboxStatus = this.getMailboxStatus();
|
|
2168
|
+
|
|
2077
2169
|
let storedStatus = await this.getStoredStatus();
|
|
2078
2170
|
|
|
2079
2171
|
// Lost-index recovery: the account has synced in a previous session but this folder
|
|
@@ -2159,36 +2251,51 @@ class Mailbox {
|
|
|
2159
2251
|
if (err.mailboxMissing) {
|
|
2160
2252
|
// this mailbox is missing, refresh listing
|
|
2161
2253
|
try {
|
|
2162
|
-
await this.connection.
|
|
2254
|
+
await this.connection.refreshAndProcessListing();
|
|
2163
2255
|
} catch (E) {
|
|
2164
2256
|
this.logger.error({ msg: 'Missing mailbox', err, E });
|
|
2165
2257
|
}
|
|
2166
2258
|
}
|
|
2167
2259
|
throw err;
|
|
2168
2260
|
} finally {
|
|
2169
|
-
|
|
2170
|
-
|
|
2171
|
-
//
|
|
2172
|
-
|
|
2173
|
-
|
|
2174
|
-
|
|
2175
|
-
|
|
2176
|
-
path: this.listingEntry.path,
|
|
2177
|
-
name: this.listingEntry.name,
|
|
2178
|
-
specialUse: this.listingEntry.specialUse || false,
|
|
2179
|
-
uidValidity: mailboxStatus.uidValidity.toString()
|
|
2180
|
-
})
|
|
2181
|
-
.catch(err => this.logger.error({ msg: 'Failed to send new mailbox notification', err }));
|
|
2182
|
-
}
|
|
2183
|
-
|
|
2184
|
-
// Resolve sync promise or start IDLE
|
|
2185
|
-
if (this.synced) {
|
|
2186
|
-
this.synced();
|
|
2261
|
+
if (this.processingOpenClient !== openedClient) {
|
|
2262
|
+
// A newer connection's open handler took the latch over while
|
|
2263
|
+
// this invocation was unwinding. The new owner runs the actual
|
|
2264
|
+
// sync now, so leave the latch, the isNew notification and the
|
|
2265
|
+
// sync promise settlement to it - settling here would resolve
|
|
2266
|
+
// the new owner's freshly armed promise before its work ran
|
|
2267
|
+
this.logger.debug({ msg: 'Mailbox open handler superseded by a newer connection', path: this.path });
|
|
2187
2268
|
} else {
|
|
2188
|
-
|
|
2189
|
-
|
|
2190
|
-
|
|
2191
|
-
|
|
2269
|
+
this.processingOpen = false;
|
|
2270
|
+
this.processingOpenClient = null;
|
|
2271
|
+
|
|
2272
|
+
// Send new mailbox notification if this is the first sync. When the
|
|
2273
|
+
// open failed before the mailbox state could be read, mailboxStatus
|
|
2274
|
+
// is undefined - keep isNew set so the notification is emitted on
|
|
2275
|
+
// the next successful open instead of sending a broken payload now
|
|
2276
|
+
if (this.listingEntry.isNew && mailboxStatus) {
|
|
2277
|
+
// fully synced, so not new anymore
|
|
2278
|
+
this.listingEntry.isNew = false;
|
|
2279
|
+
this.logger.debug({ msg: 'New mailbox', path: this.listingEntry.path });
|
|
2280
|
+
this.connection
|
|
2281
|
+
.notify(this, MAILBOX_NEW_NOTIFY, {
|
|
2282
|
+
path: this.listingEntry.path,
|
|
2283
|
+
name: this.listingEntry.name,
|
|
2284
|
+
specialUse: this.listingEntry.specialUse || false,
|
|
2285
|
+
uidValidity: mailboxStatus.uidValidity.toString()
|
|
2286
|
+
})
|
|
2287
|
+
.catch(err => this.logger.error({ msg: 'Failed to send new mailbox notification', err }));
|
|
2288
|
+
}
|
|
2289
|
+
|
|
2290
|
+
// Resolve sync promise or start IDLE
|
|
2291
|
+
if (this.synced) {
|
|
2292
|
+
this.synced();
|
|
2293
|
+
} else {
|
|
2294
|
+
try {
|
|
2295
|
+
await this.select();
|
|
2296
|
+
} catch (err) {
|
|
2297
|
+
this.logger.warn({ msg: 'Failed to select after sync', err });
|
|
2298
|
+
}
|
|
2192
2299
|
}
|
|
2193
2300
|
}
|
|
2194
2301
|
}
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
const { ImapFlow } = require('imapflow');
|
|
4
4
|
const { backOff } = require('exponential-backoff');
|
|
5
|
+
const { ReconnectBackoff } = require('../reconnect-backoff');
|
|
5
6
|
const EventEmitter = require('events').EventEmitter;
|
|
6
7
|
|
|
7
8
|
const MAX_WAIT_DELAY = 450; //ms
|
|
@@ -30,10 +31,7 @@ class Subconnection extends EventEmitter {
|
|
|
30
31
|
this.emitTimer = false;
|
|
31
32
|
|
|
32
33
|
// Reconnection tracking for capped exponential backoff
|
|
33
|
-
this.
|
|
34
|
-
this.reconnectBaseDelay = 2000;
|
|
35
|
-
this.reconnectMaxDelay = 30000; // Cap at 30 seconds
|
|
36
|
-
this.lastReconnectTime = 0;
|
|
34
|
+
this.reconnectBackoff = new ReconnectBackoff({ baseDelay: 2000, maxDelay: 30000, jitter: 1000 });
|
|
37
35
|
}
|
|
38
36
|
|
|
39
37
|
isConnected() {
|
|
@@ -63,44 +61,48 @@ class Subconnection extends EventEmitter {
|
|
|
63
61
|
await this.reconnect();
|
|
64
62
|
}
|
|
65
63
|
|
|
66
|
-
async reconnect(
|
|
67
|
-
if (this._connecting || this.isClosing ||
|
|
64
|
+
async reconnect() {
|
|
65
|
+
if (this._connecting || this.isClosing || this.isClosed) {
|
|
68
66
|
return false;
|
|
69
67
|
}
|
|
70
68
|
|
|
71
|
-
//
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
const jitter = Math.random() * 1000; // 0-1 second jitter
|
|
76
|
-
const totalDelay = delay + jitter;
|
|
77
|
-
|
|
78
|
-
this.reconnectAttempts++;
|
|
69
|
+
// Block overlapping cycles right away. The 'error' and 'close' handlers
|
|
70
|
+
// check this flag, so setting it before the backoff sleep prevents them
|
|
71
|
+
// from starting a second reconnection cycle while this one is waiting
|
|
72
|
+
this._connecting = true;
|
|
79
73
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
74
|
+
try {
|
|
75
|
+
// Calculate delay with cap and jitter (never stops trying)
|
|
76
|
+
const totalDelay = this.reconnectBackoff.nextDelay();
|
|
77
|
+
|
|
78
|
+
this.logger.info({
|
|
79
|
+
msg: 'Scheduling subconnection reconnect',
|
|
80
|
+
attempt: this.reconnectBackoff.attempts,
|
|
81
|
+
delay: totalDelay,
|
|
82
|
+
path: this.path
|
|
83
|
+
});
|
|
86
84
|
|
|
87
|
-
|
|
88
|
-
|
|
85
|
+
// Wait before attempting reconnect
|
|
86
|
+
await new Promise(resolve => setTimeout(resolve, totalDelay));
|
|
89
87
|
|
|
90
|
-
|
|
91
|
-
|
|
88
|
+
if (this.isClosing || this.isClosed) {
|
|
89
|
+
// close() was called during the delay. Do not revive a closed
|
|
90
|
+
// subconnection - the parent has already discarded this instance,
|
|
91
|
+
// so a new connection would be unreachable and leak
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
92
94
|
|
|
93
|
-
try {
|
|
94
95
|
await backOff(() => this.start(), {
|
|
95
|
-
maxDelay: this.
|
|
96
|
+
maxDelay: this.reconnectBackoff.maxDelay,
|
|
96
97
|
numOfAttempts: Infinity,
|
|
97
98
|
retry: () => !this.isClosing && !this.isClosed,
|
|
98
99
|
startingDelay: 2000
|
|
99
100
|
});
|
|
100
|
-
|
|
101
|
-
// Success - reset counter
|
|
102
|
-
this.reconnectAttempts = 0;
|
|
103
101
|
} finally {
|
|
102
|
+
// Cleared before the mailbox validation below on purpose: if the
|
|
103
|
+
// server drops the connection while the monitored mailbox is being
|
|
104
|
+
// opened, the 'close' handler must still be able to schedule the
|
|
105
|
+
// single retry cycle
|
|
104
106
|
this._connecting = false;
|
|
105
107
|
}
|
|
106
108
|
|
|
@@ -110,7 +112,61 @@ class Subconnection extends EventEmitter {
|
|
|
110
112
|
|
|
111
113
|
this.state = 'connected';
|
|
112
114
|
this.disabledReason = false;
|
|
115
|
+
|
|
116
|
+
// Reset the backoff counter only after the monitored mailbox is open.
|
|
117
|
+
// Connect/login success alone does not validate the setup - resetting
|
|
118
|
+
// already there would restart the close-triggered retry loop from the
|
|
119
|
+
// base delay on every cycle when the mailbox itself can not be opened
|
|
120
|
+
this.reconnectBackoff.reset();
|
|
113
121
|
} catch (err) {
|
|
122
|
+
if (err.responseStatus === 'NO' && this.imapClient?.usable) {
|
|
123
|
+
// SELECT failed with NO - verify whether the mailbox exists at
|
|
124
|
+
// all, mirroring the verification ImapFlow runs in
|
|
125
|
+
// getMailboxLock(). mailboxOpen() itself does not set the
|
|
126
|
+
// mailboxMissing marker
|
|
127
|
+
try {
|
|
128
|
+
let folders = await this.imapClient.run('LIST', '', this.mailbox.path, { listOnly: true });
|
|
129
|
+
// The path goes into the LIST command as the match pattern
|
|
130
|
+
// verbatim, so IMAP wildcards (% and *) in the folder name
|
|
131
|
+
// can match sibling folders. Only an entry with the exact
|
|
132
|
+
// same path proves the monitored mailbox still exists
|
|
133
|
+
// (monitored paths come from the account listing, so the
|
|
134
|
+
// stored spelling matches what the server returns)
|
|
135
|
+
if (!folders || !folders.some(folder => folder.path === this.mailbox.path)) {
|
|
136
|
+
err.mailboxMissing = true;
|
|
137
|
+
}
|
|
138
|
+
} catch (E) {
|
|
139
|
+
this.logger.debug({ msg: 'Failed to verify missing mailbox', path: this.mailbox.path, err: E });
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (err.mailboxMissing) {
|
|
144
|
+
// The monitored folder does not exist anymore, so retrying can
|
|
145
|
+
// not succeed. Shut the subconnection down and mark it as
|
|
146
|
+
// disabled - setupSubConnections() replaces it with a live
|
|
147
|
+
// connection if the folder is re-created later
|
|
148
|
+
this.logger.warn({ msg: 'Monitored mailbox is missing, disabling subconnection', path: this.mailbox.path, err });
|
|
149
|
+
|
|
150
|
+
this.close();
|
|
151
|
+
this.imapClient = null;
|
|
152
|
+
|
|
153
|
+
// close() resets these, so set the disabled state afterwards.
|
|
154
|
+
// mailboxMissing is the machine-readable marker consumed by the
|
|
155
|
+
// revival trigger in syncMailboxes() - the disabledReason text
|
|
156
|
+
// is display copy and must not carry behavior
|
|
157
|
+
this.disabled = true;
|
|
158
|
+
this.state = 'disabled';
|
|
159
|
+
this.disabledReason = 'Mailbox folder not found';
|
|
160
|
+
this.mailboxMissing = true;
|
|
161
|
+
|
|
162
|
+
// Process the folder deletion (stored listing, webhooks) right away
|
|
163
|
+
this.parent.refreshAndProcessListing().catch(err => {
|
|
164
|
+
this.logger.error({ msg: 'Failed to refresh folder listing', err });
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
|
|
114
170
|
// ended in an unconncted state
|
|
115
171
|
this.logger.warn({ msg: 'Failed to set up subconnection', err });
|
|
116
172
|
}
|
|
@@ -133,6 +189,10 @@ class Subconnection extends EventEmitter {
|
|
|
133
189
|
if (prevImapClient === this.imapClient) {
|
|
134
190
|
this.imapClient = null;
|
|
135
191
|
}
|
|
192
|
+
|
|
193
|
+
// The close handler that maintains the connection count was removed
|
|
194
|
+
// above, so update the tracking for the replaced client manually
|
|
195
|
+
await this.parent.untrackConnection(prevImapClient);
|
|
136
196
|
}
|
|
137
197
|
}
|
|
138
198
|
|
|
@@ -164,8 +224,7 @@ class Subconnection extends EventEmitter {
|
|
|
164
224
|
|
|
165
225
|
imapClient.on('close', async () => {
|
|
166
226
|
try {
|
|
167
|
-
this.parent.
|
|
168
|
-
await this.parent.redis.hSetExists(this.parent.getAccountKey(), 'connections', this.parent.connections.size.toString());
|
|
227
|
+
await this.parent.untrackConnection(imapClient);
|
|
169
228
|
imapClient.log.info({ msg: 'Connection closed', account: this.account });
|
|
170
229
|
} catch (err) {
|
|
171
230
|
this.logger.error({ msg: 'Error updating connection count on close', account: this.account, err });
|
|
@@ -183,7 +242,14 @@ class Subconnection extends EventEmitter {
|
|
|
183
242
|
});
|
|
184
243
|
|
|
185
244
|
try {
|
|
186
|
-
await this.connect();
|
|
245
|
+
let connectResponse = await this.connect();
|
|
246
|
+
if (connectResponse === false) {
|
|
247
|
+
// connect() bailed because close() landed while the connection
|
|
248
|
+
// was being set up - no socket was opened, so the 'close'
|
|
249
|
+
// listener that maintains the connection count never fires.
|
|
250
|
+
// Drop the tracking entry manually
|
|
251
|
+
await this.parent.untrackConnection(imapClient);
|
|
252
|
+
}
|
|
187
253
|
} catch (err) {
|
|
188
254
|
if (err.authenticationFailed) {
|
|
189
255
|
this.logger.error({ msg: 'Failed to authenticate subconnection', account: this.account, err });
|
|
@@ -263,10 +329,13 @@ class Subconnection extends EventEmitter {
|
|
|
263
329
|
this.imapClient.close();
|
|
264
330
|
}
|
|
265
331
|
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
332
|
+
// Drop the pending 'changes' debounce. The mailboxMissing disable path
|
|
333
|
+
// closes the subconnection while the parent's listeners are still
|
|
334
|
+
// attached (they are only detached at the next reconcile), so a timer
|
|
335
|
+
// armed just before close would otherwise still emit on the closed
|
|
336
|
+
// instance and trigger a pointless sync for the removed folder
|
|
337
|
+
clearTimeout(this.emitTimer);
|
|
338
|
+
this.emitTimer = false;
|
|
270
339
|
|
|
271
340
|
this.isClosing = false;
|
|
272
341
|
this.isClosed = true;
|