emailengine-app 2.84.0 → 2.84.1

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.
@@ -1,3 +1,3 @@
1
1
  {
2
- ".": "2.84.0"
2
+ ".": "2.84.1"
3
3
  }
package/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## [2.84.1](https://github.com/postalsys/emailengine/compare/v2.84.0...v2.84.1) (2026-10-15)
4
+
5
+
6
+ ### Bug Fixes
7
+
8
+ * assign accounts again when every IMAP worker restarts after a Redis reconnection ([2200319](https://github.com/postalsys/emailengine/commit/22003191c041f03346031518d3a5a8aec715ba91))
9
+
3
10
  ## [2.84.0](https://github.com/postalsys/emailengine/compare/v2.83.2...v2.84.0) (2026-10-15)
4
11
 
5
12
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "emailengine-app",
3
- "version": "2.84.0",
3
+ "version": "2.84.1",
4
4
  "private": false,
5
5
  "productTitle": "EmailEngine",
6
6
  "description": "Email Sync Engine",
package/server.js CHANGED
@@ -793,6 +793,7 @@ let workerAssigned = new WeakMap(); // Map of worker -> Set of accounts
793
793
  let onlineWorkers = new WeakSet(); // Set of workers that are online
794
794
  let reassignmentTimer = null; // Timer for failsafe reassignment
795
795
  let reassignmentPending = false; // Flag to track if reassignment is pending
796
+ const REASSIGNMENT_FAILSAFE_DELAY = 10 * 1000; // How long an exited IMAP worker gets to restart before its accounts move
796
797
  let assignRetryCount = 0; // Counter for safety-net retries in assignAccounts
797
798
  const MAX_ASSIGN_RETRIES = 3; // Max consecutive safety-net retries before giving up
798
799
 
@@ -1155,7 +1156,7 @@ let spawnWorker = async (type, opts, slot) => {
1155
1156
 
1156
1157
  // A fresh spawn opens its own slot. Counting the crash streak per type made every short-lived
1157
1158
  // exit of ANY IMAP worker double the next respawn delay of all of them, so unrelated exits
1158
- // pushed a restart past the 10s failsafe reassignment.
1159
+ // pushed a restart past the failsafe reassignment (REASSIGNMENT_FAILSAFE_DELAY).
1159
1160
  if (!slot) {
1160
1161
  slot = `${type}:${++spawnSlotCounter}`;
1161
1162
  }
@@ -1297,24 +1298,8 @@ let spawnWorker = async (type, opts, slot) => {
1297
1298
  if (unassigned && unassigned.size > 0) {
1298
1299
  reassignmentPending = true;
1299
1300
 
1300
- // Clear any existing timer
1301
- if (reassignmentTimer) {
1302
- clearTimeout(reassignmentTimer);
1303
- }
1304
-
1305
- // Set a failsafe timer - if worker doesn't restart in 10 seconds, reassign anyway
1306
- reassignmentTimer = setTimeout(() => {
1307
- if (reassignmentPending && unassigned && unassigned.size > 0) {
1308
- logger.warn({
1309
- msg: 'Failsafe reassignment triggered - worker restart timeout',
1310
- unassignedAccounts: unassigned.size,
1311
- currentWorkers: availableIMAPWorkers.size,
1312
- expectedWorkers: config.workers.imap
1313
- });
1314
- reassignmentPending = false;
1315
- assignAccounts().catch(err => logger.error({ msg: 'Unable to reassign accounts (failsafe)', err }));
1316
- }
1317
- }, 10000); // 10 second timeout
1301
+ // If the worker does not restart in time, its accounts go to the others anyway
1302
+ armReassignmentFailsafe();
1318
1303
 
1319
1304
  logger[exitLevel]({
1320
1305
  msg: 'Worker exited, waiting for restart before reassignment',
@@ -1658,6 +1643,11 @@ let spawnWorker = async (type, opts, slot) => {
1658
1643
  expectedWorkers: config.workers.imap,
1659
1644
  unassignedAccounts: unassigned.size
1660
1645
  });
1646
+ // The failsafe found no worker at all and stopped; give the rest
1647
+ // of the fleet the same time to come back from here
1648
+ if (!reassignmentTimer) {
1649
+ armReassignmentFailsafe();
1650
+ }
1661
1651
  }
1662
1652
  } else if (imapInitialWorkersLoaded) {
1663
1653
  // The failsafe timer already gave this worker's accounts to the
@@ -1791,6 +1781,43 @@ async function unassignFromWorker(worker, account) {
1791
1781
  }
1792
1782
  }
1793
1783
 
1784
+ /**
1785
+ * Starts (or restarts) the failsafe timer that gives the accounts an exited IMAP worker left behind
1786
+ * to the remaining workers when it does not come back in time
1787
+ */
1788
+ function armReassignmentFailsafe() {
1789
+ clearTimeout(reassignmentTimer);
1790
+
1791
+ reassignmentTimer = setTimeout(() => {
1792
+ reassignmentTimer = null;
1793
+ if (!reassignmentPending || !unassigned || !unassigned.size) {
1794
+ return;
1795
+ }
1796
+
1797
+ if (!availableIMAPWorkers.size) {
1798
+ // Every worker exited at once (the clean exit after a Redis reconnection) and none is
1799
+ // back yet. A pass now would find no worker and clear the pending flag that makes the
1800
+ // returning workers pick the accounts up, stranding them until a process restart. Keep
1801
+ // it pending instead; the first worker to come back arms this timer again
1802
+ logger.warn({
1803
+ msg: 'Failsafe reassignment postponed - no IMAP worker is available yet',
1804
+ unassignedAccounts: unassigned.size,
1805
+ expectedWorkers: config.workers.imap
1806
+ });
1807
+ return;
1808
+ }
1809
+
1810
+ logger.warn({
1811
+ msg: 'Failsafe reassignment triggered - worker restart timeout',
1812
+ unassignedAccounts: unassigned.size,
1813
+ currentWorkers: availableIMAPWorkers.size,
1814
+ expectedWorkers: config.workers.imap
1815
+ });
1816
+ reassignmentPending = false;
1817
+ assignAccounts().catch(err => logger.error({ msg: 'Unable to reassign accounts (failsafe)', err }));
1818
+ }, REASSIGNMENT_FAILSAFE_DELAY);
1819
+ }
1820
+
1794
1821
  /**
1795
1822
  * Moves a fair share of accounts onto an IMAP worker that became ready while the rest of the fleet
1796
1823
  * already served every account. That is the normal outcome of a restart slower than the failsafe
@@ -1809,9 +1836,6 @@ async function rebalanceOnto(worker) {
1809
1836
  }
1810
1837
 
1811
1838
  let moves = planRebalance({ workerAssigned, availableWorkers: availableIMAPWorkers }, worker);
1812
- if (!moves.length) {
1813
- return;
1814
- }
1815
1839
 
1816
1840
  assigning = true;
1817
1841
  let moved = 0;
@@ -1843,8 +1867,9 @@ async function rebalanceOnto(worker) {
1843
1867
  logger.info({ msg: 'Rebalancing accounts onto a restarted IMAP worker', threadId: worker.threadId, accounts: moved });
1844
1868
  }
1845
1869
  // Also run when nothing moved: an account added while this held the assignment lock found the
1846
- // pass refused and is waiting in unassigned. Every worker is available again, so the pass
1847
- // picks the least loaded one, which is the newcomer.
1870
+ // pass refused and is waiting in unassigned, or nothing was there to move because every worker
1871
+ // had exited and the accounts are all waiting. The pass picks the least loaded worker, which
1872
+ // is the newcomer.
1848
1873
  if (moved || unassigned.size) {
1849
1874
  await assignAccounts();
1850
1875
  }