emailengine-app 2.79.2 → 2.79.3
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/.github/workflows/deploy.yml +92 -39
- package/.github/workflows/release.yaml +94 -34
- package/.release-please-manifest.json +1 -1
- package/CHANGELOG.md +45 -0
- package/copy-static-files.sh +23 -2
- package/lib/account.js +43 -0
- package/lib/email-client/base-client.js +133 -46
- package/lib/email-client/gmail-client.js +12 -0
- package/lib/email-client/imap-client.js +74 -68
- package/lib/email-client/outlook-client.js +12 -0
- package/lib/logger.js +43 -3
- package/package.json +10 -10
- package/sbom.json +1 -1
- package/server.js +21 -9
- package/static/licenses.html +68 -138
- package/translations/messages.pot +65 -65
|
@@ -486,9 +486,14 @@ class BaseClient {
|
|
|
486
486
|
/**
|
|
487
487
|
* Sets error state and manages error tracking/notifications
|
|
488
488
|
* Implements logic to disable IMAP after repeated authentication failures
|
|
489
|
+
*
|
|
490
|
+
* The return value drives whether the caller sends a webhook. That is normally "is this a new
|
|
491
|
+
* error", so a run of identical failures is reported once - but it is also true when this call
|
|
492
|
+
* is the one that switches the account off, which is not a new error and still has to be heard.
|
|
493
|
+
*
|
|
489
494
|
* @param {string} event - Error event type
|
|
490
495
|
* @param {Object} data - Error details
|
|
491
|
-
* @returns {boolean} Whether
|
|
496
|
+
* @returns {boolean} Whether the caller should notify
|
|
492
497
|
*/
|
|
493
498
|
async setErrorState(event, data) {
|
|
494
499
|
// Retrieve previous error state for comparison
|
|
@@ -504,7 +509,7 @@ class BaseClient {
|
|
|
504
509
|
this.state = event;
|
|
505
510
|
await this.setStateVal();
|
|
506
511
|
|
|
507
|
-
let
|
|
512
|
+
let shouldNotify = true;
|
|
508
513
|
|
|
509
514
|
// Store current error state
|
|
510
515
|
await this.redis.hSetExists(this.getAccountKey(), 'lastErrorState', JSON.stringify(data));
|
|
@@ -516,12 +521,12 @@ class BaseClient {
|
|
|
516
521
|
|
|
517
522
|
if (data.serverResponseCode && data.serverResponseCode === prevLastErrorState.serverResponseCode) {
|
|
518
523
|
// error code did not change, assume it is the same error
|
|
519
|
-
|
|
524
|
+
shouldNotify = false;
|
|
520
525
|
} else {
|
|
521
526
|
try {
|
|
522
527
|
deepEqual(data, prevLastErrorState);
|
|
523
528
|
// nothing changed
|
|
524
|
-
|
|
529
|
+
shouldNotify = false;
|
|
525
530
|
} catch (err) {
|
|
526
531
|
// seems different
|
|
527
532
|
}
|
|
@@ -529,7 +534,7 @@ class BaseClient {
|
|
|
529
534
|
}
|
|
530
535
|
|
|
531
536
|
// Track error occurrences
|
|
532
|
-
if (
|
|
537
|
+
if (shouldNotify) {
|
|
533
538
|
const txn = createTransaction(this.redis, { logger: this.logger });
|
|
534
539
|
txn.add('hSetExists', this.getAccountKey(), 'lastError:errorCount', 1);
|
|
535
540
|
txn.add('hSetExists', this.getAccountKey(), 'lastError:first', new Date().toISOString());
|
|
@@ -560,53 +565,135 @@ class BaseClient {
|
|
|
560
565
|
switch (event) {
|
|
561
566
|
case 'authenticationError':
|
|
562
567
|
if (errorCount > 0 && firstError && Date.now() - firstError.getTime() > MAX_IMAP_AUTH_FAILURE_TIME) {
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
}
|
|
572
|
-
}
|
|
573
|
-
if (imapData && !imapData.disabled) {
|
|
574
|
-
imapData.disabled = true;
|
|
575
|
-
|
|
576
|
-
// Disable IMAP and reset error counters
|
|
577
|
-
const disableTxn = createTransaction(this.redis, { logger: this.logger });
|
|
578
|
-
disableTxn.add('hSetExists', this.getAccountKey(), 'imap', JSON.stringify(imapData));
|
|
579
|
-
disableTxn.add('hdel', this.getAccountKey(), 'lastError:errorCount', 'lastError:first');
|
|
580
|
-
disableTxn.add(
|
|
581
|
-
'hSetExists',
|
|
582
|
-
this.getAccountKey(),
|
|
583
|
-
'lastErrorState',
|
|
584
|
-
JSON.stringify({
|
|
585
|
-
description: 'IMAP was disabled for the account due to exceeding the authentication error threshold',
|
|
586
|
-
response: data.response
|
|
587
|
-
})
|
|
588
|
-
);
|
|
589
|
-
await disableTxn.exec();
|
|
590
|
-
|
|
591
|
-
this.logger.warn({
|
|
592
|
-
msg: 'IMAP was disabled for the account due to exceeding the authentication error threshold',
|
|
593
|
-
errorEvent: event,
|
|
594
|
-
firstError: firstError.toISOString(),
|
|
595
|
-
timeThreshold: MAX_IMAP_AUTH_FAILURE_TIME,
|
|
596
|
-
errorCount
|
|
597
|
-
});
|
|
598
|
-
|
|
599
|
-
// Close the connection after disabling
|
|
600
|
-
setImmediate(() => {
|
|
601
|
-
this.close();
|
|
602
|
-
});
|
|
568
|
+
const disabled = await this.disableAfterAuthFailures({ event, firstError, errorCount, data });
|
|
569
|
+
if (disabled) {
|
|
570
|
+
// Report it. This branch only runs for a repeat of an error the caller
|
|
571
|
+
// has already been told about, and would normally suppress the webhook
|
|
572
|
+
// as unchanged - but an account going offline until a human
|
|
573
|
+
// re-authenticates it is the one thing here an operator has to hear
|
|
574
|
+
// about, and until now it happened in silence.
|
|
575
|
+
shouldNotify = true;
|
|
603
576
|
}
|
|
604
577
|
}
|
|
605
578
|
break;
|
|
606
579
|
}
|
|
607
580
|
}
|
|
608
581
|
|
|
609
|
-
return
|
|
582
|
+
return shouldNotify;
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
/**
|
|
586
|
+
* Whether mail sync has been switched off for this account, by the operator or by the
|
|
587
|
+
* auth-failure safety net in setErrorState().
|
|
588
|
+
*
|
|
589
|
+
* Reads the flag straight out of Redis rather than through loadAccountData(), so a client can
|
|
590
|
+
* ask before it does any credential work. That ordering is the point: an account is switched
|
|
591
|
+
* off precisely because that work keeps failing, and for an OAuth2 account the failing work is
|
|
592
|
+
* a token refresh against the provider.
|
|
593
|
+
*
|
|
594
|
+
* @returns {Promise<Boolean>} True when the account must not connect
|
|
595
|
+
*/
|
|
596
|
+
async isSyncDisabled() {
|
|
597
|
+
const imapInfo = await this.redis.hget(this.getAccountKey(), 'imap');
|
|
598
|
+
if (!imapInfo) {
|
|
599
|
+
return false;
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
try {
|
|
603
|
+
return !!JSON.parse(imapInfo).disabled;
|
|
604
|
+
} catch (err) {
|
|
605
|
+
// An unreadable blob is not a disable; stay noisy and let the connection proceed.
|
|
606
|
+
this.logger.error({ msg: 'Failed parsing IMAP data', err });
|
|
607
|
+
return false;
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
/**
|
|
612
|
+
* Stops an account retrying after it has failed authentication for longer than
|
|
613
|
+
* MAX_IMAP_AUTH_FAILURE_TIME, until someone re-authenticates it.
|
|
614
|
+
*
|
|
615
|
+
* `imap.disabled` is the flag every gate in the codebase already reads, so an OAuth2 account -
|
|
616
|
+
* which has no `imap` configuration of its own, because getImapConfig() builds its connection
|
|
617
|
+
* from the provider table instead - gets one written holding nothing but the flag. Both kinds
|
|
618
|
+
* of account then take the identical path through the connection gates and the admin UI's
|
|
619
|
+
* disabled-account handling. Writing it cannot disturb an OAuth2 connection config:
|
|
620
|
+
* getImapConfig() branches on `oauth2` first and only reads `accountData.imap` for password
|
|
621
|
+
* accounts.
|
|
622
|
+
*
|
|
623
|
+
* Before this, the whole block was gated on an `imap` blob already being present. OAuth2
|
|
624
|
+
* accounts never have one, so on a deployment that is mostly Gmail and Microsoft the documented
|
|
625
|
+
* three-day safety net was a silent no-op: an account whose refresh token had been revoked kept
|
|
626
|
+
* asking for a new one forever.
|
|
627
|
+
*
|
|
628
|
+
* @param {Object} opts
|
|
629
|
+
* @param {String} opts.event - The error event that tripped the threshold
|
|
630
|
+
* @param {Date} opts.firstError - When the current run of failures started
|
|
631
|
+
* @param {Number} opts.errorCount - How many failures have been counted since
|
|
632
|
+
* @param {Object} opts.data - Error payload for the account's stored error state
|
|
633
|
+
* @returns {Promise<Boolean>} True when this call is what disabled the account
|
|
634
|
+
*/
|
|
635
|
+
async disableAfterAuthFailures({ event, firstError, errorCount, data }) {
|
|
636
|
+
const accountKey = this.getAccountKey();
|
|
637
|
+
|
|
638
|
+
let imapData;
|
|
639
|
+
const imapInfo = await this.redis.hget(accountKey, 'imap');
|
|
640
|
+
if (imapInfo) {
|
|
641
|
+
try {
|
|
642
|
+
imapData = JSON.parse(imapInfo);
|
|
643
|
+
} catch (err) {
|
|
644
|
+
// Rewriting a blob that does not parse would replace the account's IMAP
|
|
645
|
+
// configuration with a bare flag, so leave it alone and stay noisy instead.
|
|
646
|
+
this.logger.error({ msg: 'Failed parsing IMAP data', err });
|
|
647
|
+
return false;
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
if (!imapData) {
|
|
652
|
+
// No IMAP configuration: an OAuth2 account. Only synthesize the flag when there is an
|
|
653
|
+
// OAuth2 credential for the failures to be about, so an account still being created
|
|
654
|
+
// cannot be marked disabled on its way in.
|
|
655
|
+
const hasOAuth2 = await this.redis.hexists(accountKey, 'oauth2');
|
|
656
|
+
if (!hasOAuth2) {
|
|
657
|
+
return false;
|
|
658
|
+
}
|
|
659
|
+
imapData = {};
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
if (imapData.disabled) {
|
|
663
|
+
return false;
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
imapData.disabled = true;
|
|
667
|
+
|
|
668
|
+
// Disable IMAP and reset error counters
|
|
669
|
+
const disableTxn = createTransaction(this.redis, { logger: this.logger });
|
|
670
|
+
disableTxn.add('hSetExists', accountKey, 'imap', JSON.stringify(imapData));
|
|
671
|
+
disableTxn.add('hdel', accountKey, 'lastError:errorCount', 'lastError:first');
|
|
672
|
+
disableTxn.add(
|
|
673
|
+
'hSetExists',
|
|
674
|
+
accountKey,
|
|
675
|
+
'lastErrorState',
|
|
676
|
+
JSON.stringify({
|
|
677
|
+
description: 'IMAP was disabled for the account due to exceeding the authentication error threshold',
|
|
678
|
+
response: data.response
|
|
679
|
+
})
|
|
680
|
+
);
|
|
681
|
+
await disableTxn.exec();
|
|
682
|
+
|
|
683
|
+
this.logger.warn({
|
|
684
|
+
msg: 'IMAP was disabled for the account due to exceeding the authentication error threshold',
|
|
685
|
+
errorEvent: event,
|
|
686
|
+
firstError: firstError.toISOString(),
|
|
687
|
+
timeThreshold: MAX_IMAP_AUTH_FAILURE_TIME,
|
|
688
|
+
errorCount
|
|
689
|
+
});
|
|
690
|
+
|
|
691
|
+
// Close the connection after disabling
|
|
692
|
+
setImmediate(() => {
|
|
693
|
+
this.close();
|
|
694
|
+
});
|
|
695
|
+
|
|
696
|
+
return true;
|
|
610
697
|
}
|
|
611
698
|
|
|
612
699
|
/**
|
|
@@ -313,6 +313,18 @@ class GmailClient extends BaseClient {
|
|
|
313
313
|
await this.setStateVal();
|
|
314
314
|
|
|
315
315
|
await this.getAccount();
|
|
316
|
+
|
|
317
|
+
// The auth-failure safety net switches an account off by setting `imap.disabled`, and
|
|
318
|
+
// an API-backed account reaches it the same way an IMAP one does - its failures run
|
|
319
|
+
// through the same setErrorState(). Checked before any credential work, because the
|
|
320
|
+
// work that keeps failing for these accounts is the token refresh below.
|
|
321
|
+
if (await this.isSyncDisabled()) {
|
|
322
|
+
this.logger.info({ msg: 'Skipped connection setup', account: this.account, reason: 'disabled' });
|
|
323
|
+
this.state = 'unset';
|
|
324
|
+
await this.setStateVal();
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
|
|
316
328
|
await this.getClient(true);
|
|
317
329
|
|
|
318
330
|
// Ensure access token exists to get scopes
|
|
@@ -415,19 +415,18 @@ class IMAPClient extends BaseClient {
|
|
|
415
415
|
await commandClient.connect();
|
|
416
416
|
commandClient.log.info({ msg: 'Command channel connected', cid: commandCid, channel: 'command', account: this.account });
|
|
417
417
|
} catch (err) {
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
418
|
+
// untrackConnection() swallows its own Redis failure, so the connect error is
|
|
419
|
+
// what the caller sees - a hiccup while updating the count must not replace it
|
|
420
|
+
await this.untrackConnection(commandClient);
|
|
421
421
|
commandClient.log.warn({ msg: 'Failed to connect command client', cid: commandCid, channel: 'command', account: this.account, err });
|
|
422
422
|
throw err;
|
|
423
423
|
}
|
|
424
424
|
|
|
425
|
-
// Clean up when connection closes
|
|
425
|
+
// Clean up when connection closes. EventEmitter never observes what an async listener
|
|
426
|
+
// returns, so nothing in this body may escape - see the primary connection's handler
|
|
426
427
|
commandClient.on('close', async () => {
|
|
427
428
|
try {
|
|
428
|
-
|
|
429
|
-
await this.redis.hSetExists(this.getAccountKey(), 'connections', this.connections.size.toString());
|
|
430
|
-
}
|
|
429
|
+
await this.untrackConnection(commandClient);
|
|
431
430
|
commandClient.log.info({ msg: 'Connection closed', cid: commandCid, channel: 'command', account: this.account });
|
|
432
431
|
} catch (err) {
|
|
433
432
|
this.logger.error({ msg: 'Error in command client close handler', cid: commandCid, account: this.account, err });
|
|
@@ -1663,81 +1662,88 @@ class IMAPClient extends BaseClient {
|
|
|
1663
1662
|
metricsMeta({}, this.logger, 'imapBytesReceived', 'inc', imapStats.received);
|
|
1664
1663
|
});
|
|
1665
1664
|
|
|
1666
|
-
// Handle connection close events
|
|
1665
|
+
// Handle connection close events. EventEmitter never observes what an async
|
|
1666
|
+
// listener returns, so anything that escapes this body becomes an unhandled
|
|
1667
|
+
// rejection and takes the whole worker down. The steps below carry their own
|
|
1668
|
+
// catches; this outer one is what keeps that true for an await added outside them.
|
|
1667
1669
|
imapClient.on('close', async () => {
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
+
try {
|
|
1671
|
+
await this.untrackConnection(imapClient);
|
|
1672
|
+
imapClient?.log.info({ msg: 'Connection closed', type: 'imapClient', account: this.account, disabled: imapClient.disabled });
|
|
1670
1673
|
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1674
|
+
// Update state on unexpected disconnect
|
|
1675
|
+
if (['init', 'connecting', 'syncing', 'connected'].includes(this.state)) {
|
|
1676
|
+
this.state = 'disconnected';
|
|
1677
|
+
try {
|
|
1678
|
+
await this.setStateVal();
|
|
1679
|
+
await emitChangeEvent(this.logger, this.account, 'state', this.state);
|
|
1680
|
+
} catch (err) {
|
|
1681
|
+
this.logger.error({ msg: 'Failed to update state on connection close', account: this.account, err });
|
|
1682
|
+
}
|
|
1679
1683
|
}
|
|
1680
|
-
}
|
|
1681
1684
|
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1685
|
+
try {
|
|
1686
|
+
// Handle cleanup for all mailboxes
|
|
1687
|
+
for (let [, mailbox] of this.mailboxes) {
|
|
1688
|
+
if (mailbox.syncing) {
|
|
1689
|
+
try {
|
|
1690
|
+
// set failure flag
|
|
1691
|
+
await this.redis.hSetNew(
|
|
1692
|
+
this.getAccountKey(),
|
|
1693
|
+
'syncError',
|
|
1694
|
+
JSON.stringify({
|
|
1695
|
+
path: mailbox.path,
|
|
1696
|
+
time: new Date().toISOString(),
|
|
1697
|
+
error: {
|
|
1698
|
+
error: 'Connection closed unexpectedly'
|
|
1699
|
+
}
|
|
1700
|
+
})
|
|
1701
|
+
);
|
|
1702
|
+
} catch (err) {
|
|
1703
|
+
// ignore
|
|
1704
|
+
}
|
|
1701
1705
|
}
|
|
1702
|
-
}
|
|
1703
1706
|
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
+
if (mailbox.selected) {
|
|
1708
|
+
// should be at most one though
|
|
1709
|
+
await mailbox.onClose();
|
|
1710
|
+
}
|
|
1707
1711
|
}
|
|
1712
|
+
} catch (err) {
|
|
1713
|
+
imapClient.log.error({ msg: 'Connection close error', err });
|
|
1708
1714
|
}
|
|
1709
|
-
} catch (err) {
|
|
1710
|
-
imapClient.log.error({ msg: 'Connection close error', err });
|
|
1711
|
-
}
|
|
1712
1715
|
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1716
|
+
try {
|
|
1717
|
+
// Attempt reconnection if this was an unexpected close
|
|
1718
|
+
if (!imapClient.disabled && imapClient === this.imapClient && !this._connecting) {
|
|
1719
|
+
if (this.validateConnectionAge(imapClient)) {
|
|
1720
|
+
// A proven-stable connection dropped - reconnect right away
|
|
1721
|
+
if (!this.reconnectTimer) {
|
|
1722
|
+
imapClient.log.debug({ msg: 'Requesting reconnection due to unexpected close', type: 'imapClient', account: this.account });
|
|
1723
|
+
await this.reconnect();
|
|
1724
|
+
}
|
|
1725
|
+
} else {
|
|
1726
|
+
// The connection died soon after it was set up. An
|
|
1727
|
+
// immediate retry against a server that drops
|
|
1728
|
+
// connections right after setup turns into an
|
|
1729
|
+
// unthrottled login loop (the setup itself
|
|
1730
|
+
// "succeeds", so the sync-reset backoffs never
|
|
1731
|
+
// grow), so schedule the reconnect with a growing
|
|
1732
|
+
// delay instead
|
|
1733
|
+
this.scheduleReconnect(this.closeReconnectBackoff, 'Throttling reconnection of a short-lived connection');
|
|
1721
1734
|
}
|
|
1722
|
-
} else {
|
|
1723
|
-
// The connection died soon after it was set up. An
|
|
1724
|
-
// immediate retry against a server that drops
|
|
1725
|
-
// connections right after setup turns into an
|
|
1726
|
-
// unthrottled login loop (the setup itself
|
|
1727
|
-
// "succeeds", so the sync-reset backoffs never
|
|
1728
|
-
// grow), so schedule the reconnect with a growing
|
|
1729
|
-
// delay instead
|
|
1730
|
-
this.scheduleReconnect(this.closeReconnectBackoff, 'Throttling reconnection of a short-lived connection');
|
|
1731
1735
|
}
|
|
1736
|
+
} catch (err) {
|
|
1737
|
+
// This is the only path that re-arms the connection after an unexpected close.
|
|
1738
|
+
// If it throws, nothing else reconnects the account, so it stays offline until
|
|
1739
|
+
// the worker restarts - never demote this below error.
|
|
1740
|
+
imapClient.log.error({ msg: 'Reconnection error', account: this.account, err });
|
|
1732
1741
|
}
|
|
1733
1742
|
} catch (err) {
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
imapClient.log.error({ msg: 'Reconnection error', account: this.account, err });
|
|
1743
|
+
this.logger.error({ msg: 'Failed to process connection close', account: this.account, err });
|
|
1744
|
+
} finally {
|
|
1745
|
+
imapClient = null;
|
|
1738
1746
|
}
|
|
1739
|
-
|
|
1740
|
-
imapClient = null;
|
|
1741
1747
|
});
|
|
1742
1748
|
|
|
1743
1749
|
try {
|
|
@@ -238,6 +238,18 @@ class OutlookClient extends BaseClient {
|
|
|
238
238
|
await this.setStateVal();
|
|
239
239
|
|
|
240
240
|
await this.getAccount();
|
|
241
|
+
|
|
242
|
+
// The auth-failure safety net switches an account off by setting `imap.disabled`, and
|
|
243
|
+
// an API-backed account reaches it the same way an IMAP one does - its failures run
|
|
244
|
+
// through the same setErrorState(). Checked before any credential work, because the
|
|
245
|
+
// work that keeps failing for these accounts is the token refresh below.
|
|
246
|
+
if (await this.isSyncDisabled()) {
|
|
247
|
+
this.logger.info({ msg: 'Skipped connection setup', account: this.account, reason: 'disabled' });
|
|
248
|
+
this.state = 'unset';
|
|
249
|
+
await this.setStateVal();
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
|
|
241
253
|
await this.getClient(true);
|
|
242
254
|
await this.prepareDelegatedAccount();
|
|
243
255
|
|
package/lib/logger.js
CHANGED
|
@@ -59,6 +59,14 @@ const FATAL_EXIT_CODES = {
|
|
|
59
59
|
unhandledRejection: 2
|
|
60
60
|
};
|
|
61
61
|
|
|
62
|
+
// How long the process stays alive after a fatal error before the exit lands. This is not the
|
|
63
|
+
// error-tracking delivery window - lib/sentry.js installs notifyError and flushNotifications
|
|
64
|
+
// together, so whenever there is a report to deliver its own Sentry.flush() promise already
|
|
65
|
+
// covers it. What this covers is Node's: a handler that arrives a microtask after a rejection was
|
|
66
|
+
// reported raises 'rejectionHandled' a turn later, and without a timer the exit beats it (see the
|
|
67
|
+
// listener at the bottom of this file). Tune it against event-loop turns, not network latency.
|
|
68
|
+
const FATAL_EXIT_GRACE = 10;
|
|
69
|
+
|
|
62
70
|
// An error that reaches the global handlers leaves the process in an unknown state,
|
|
63
71
|
// so the process must always exit. If error tracking is enabled, report the error
|
|
64
72
|
// first and allow a short flush window for the delivery. The report is tagged with
|
|
@@ -70,11 +78,17 @@ function fatalShutdown(handler, err) {
|
|
|
70
78
|
logger.notifyError(err, { level: 'fatal', tags: { handler } });
|
|
71
79
|
}
|
|
72
80
|
|
|
73
|
-
|
|
81
|
+
// The exit is always taken a timer turn later, never straight off the flush promise. Node
|
|
82
|
+
// decides a rejection went unhandled at the end of the tick it settled in and only emits
|
|
83
|
+
// 'rejectionHandled' once a handler turns up after that, so exiting from a resolved flush's
|
|
84
|
+
// microtask races the event away - and it did, on exactly the installations that have error
|
|
85
|
+
// tracking switched on and therefore report the most crashes.
|
|
86
|
+
let exit = () => setTimeout(() => process.exit(FATAL_EXIT_CODES[handler]), FATAL_EXIT_GRACE);
|
|
87
|
+
|
|
74
88
|
if (logger.flushNotifications) {
|
|
75
89
|
logger.flushNotifications().then(exit, exit);
|
|
76
90
|
} else {
|
|
77
|
-
|
|
91
|
+
exit();
|
|
78
92
|
}
|
|
79
93
|
}
|
|
80
94
|
|
|
@@ -88,14 +102,40 @@ process.on('uncaughtException', err => {
|
|
|
88
102
|
fatalShutdown('uncaughtException', err);
|
|
89
103
|
});
|
|
90
104
|
|
|
91
|
-
|
|
105
|
+
// Promises that already reached the handler below, so a late handler can be recognised. Weak
|
|
106
|
+
// keys, because the entry is only ever looked up from the promise Node hands back.
|
|
107
|
+
const reportedRejections = new WeakMap();
|
|
108
|
+
|
|
109
|
+
process.on('unhandledRejection', (err, promise) => {
|
|
92
110
|
logger.fatal({
|
|
93
111
|
msg: 'unhandledRejection',
|
|
94
112
|
_msg: 'unhandledRejection',
|
|
95
113
|
err
|
|
96
114
|
});
|
|
97
115
|
|
|
116
|
+
reportedRejections.set(promise, Date.now());
|
|
117
|
+
|
|
98
118
|
fatalShutdown('unhandledRejection', err);
|
|
99
119
|
});
|
|
100
120
|
|
|
121
|
+
// Node decides a rejection is unhandled at the end of the tick it settled in, so a handler that
|
|
122
|
+
// arrives one microtask late still counts as unhandled. Such a handler fires 'rejectionHandled'
|
|
123
|
+
// afterwards, and fatalShutdown() always leaves FATAL_EXIT_GRACE before the exit lands, so the
|
|
124
|
+
// event has time to arrive. The worker still dies either way - a rejection that escaped is a bug
|
|
125
|
+
// wherever it came from - but this line is the difference between a genuinely missing catch and a
|
|
126
|
+
// handler that lost a race, which is otherwise impossible to tell apart from a crash log.
|
|
127
|
+
process.on('rejectionHandled', promise => {
|
|
128
|
+
let reportedAt = reportedRejections.get(promise);
|
|
129
|
+
if (!reportedAt) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
reportedRejections.delete(promise);
|
|
133
|
+
|
|
134
|
+
logger.fatal({
|
|
135
|
+
msg: 'unhandledRejection was handled after it was reported',
|
|
136
|
+
_msg: 'unhandledRejection was handled after it was reported',
|
|
137
|
+
delay: Date.now() - reportedAt
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
|
|
101
141
|
module.exports = logger;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "emailengine-app",
|
|
3
|
-
"version": "2.79.
|
|
3
|
+
"version": "2.79.3",
|
|
4
4
|
"private": false,
|
|
5
5
|
"productTitle": "EmailEngine",
|
|
6
6
|
"description": "Email Sync Engine",
|
|
@@ -51,8 +51,8 @@
|
|
|
51
51
|
},
|
|
52
52
|
"homepage": "https://emailengine.app/",
|
|
53
53
|
"dependencies": {
|
|
54
|
-
"@bull-board/api": "9.
|
|
55
|
-
"@bull-board/hapi": "9.
|
|
54
|
+
"@bull-board/api": "9.4.0",
|
|
55
|
+
"@bull-board/hapi": "9.4.0",
|
|
56
56
|
"@elastic/elasticsearch": "8.15.3",
|
|
57
57
|
"@hapi/accept": "6.0.3",
|
|
58
58
|
"@hapi/bell": "13.1.0",
|
|
@@ -65,15 +65,15 @@
|
|
|
65
65
|
"@msgpack/msgpack": "3.1.3",
|
|
66
66
|
"@phc/pbkdf2": "1.1.14",
|
|
67
67
|
"@postalsys/bounce-classifier": "3.0.0",
|
|
68
|
-
"@postalsys/certs": "1.0.
|
|
68
|
+
"@postalsys/certs": "1.0.18",
|
|
69
69
|
"@postalsys/ee-client": "1.7.0",
|
|
70
|
-
"@postalsys/email-ai-tools": "1.13.
|
|
70
|
+
"@postalsys/email-ai-tools": "1.13.19",
|
|
71
71
|
"@postalsys/email-content": "0.1.3",
|
|
72
|
-
"@postalsys/email-text-tools": "2.4.
|
|
72
|
+
"@postalsys/email-text-tools": "2.4.14",
|
|
73
73
|
"@postalsys/gettext": "4.1.1",
|
|
74
74
|
"@postalsys/joi-messages": "1.0.5",
|
|
75
75
|
"@postalsys/templates": "2.0.2",
|
|
76
|
-
"@sentry/node": "10.
|
|
76
|
+
"@sentry/node": "10.71.0",
|
|
77
77
|
"@simplewebauthn/browser": "13.3.0",
|
|
78
78
|
"@simplewebauthn/server": "13.3.2",
|
|
79
79
|
"@zone-eu/mailsplit": "5.4.15",
|
|
@@ -93,15 +93,15 @@
|
|
|
93
93
|
"html-to-text": "10.0.1",
|
|
94
94
|
"ical.js": "1.5.0",
|
|
95
95
|
"iconv-lite": "0.7.3",
|
|
96
|
-
"imapflow": "1.7.
|
|
97
|
-
"ioredfour": "1.4.
|
|
96
|
+
"imapflow": "1.7.6",
|
|
97
|
+
"ioredfour": "1.4.3",
|
|
98
98
|
"ioredis": "6.0.0",
|
|
99
99
|
"ipaddr.js": "2.5.0",
|
|
100
100
|
"joi": "18.2.5",
|
|
101
101
|
"libbase64": "1.3.0",
|
|
102
102
|
"libmime": "5.4.2",
|
|
103
103
|
"libqp": "2.1.1",
|
|
104
|
-
"mailparser": "3.9.
|
|
104
|
+
"mailparser": "3.9.16",
|
|
105
105
|
"marked": "15.0.12",
|
|
106
106
|
"minimist": "1.2.8",
|
|
107
107
|
"murmurhash": "2.0.1",
|