diff --git a/lib/imap-connection.js b/lib/imap-connection.js index 64d9a599..2dc45b8c 100644 --- a/lib/imap-connection.js +++ b/lib/imap-connection.js @@ -579,7 +579,7 @@ class IMAPConnection extends BaseClient { try { await mailbox.onExists(event); } catch (err) { - this.logger.error({ msg: 'Exists error', err }); + imapClient.logger.error({ msg: 'Exists error', err }); } }); @@ -592,7 +592,7 @@ class IMAPConnection extends BaseClient { try { await mailbox.onOpen(event); } catch (err) { - this.logger.error({ msg: 'Open error', err }); + imapClient.logger.error({ msg: 'Open error', err }); } }); @@ -605,7 +605,7 @@ class IMAPConnection extends BaseClient { try { await mailbox.onClose(event); } catch (err) { - this.logger.error({ msg: 'Close error', err }); + imapClient.logger.error({ msg: 'Close error', err }); } }); @@ -618,12 +618,12 @@ class IMAPConnection extends BaseClient { try { await mailbox.onFlags(event); } catch (err) { - this.logger.error({ msg: 'Flags error', err }); + imapClient.logger.error({ msg: 'Flags error', err }); } }); imapClient.on('close', async () => { - this.logger.info({ msg: 'Connection closed', type: 'imapClient', account: this.account, disabled: imapClient.disabled }); + imapClient.logger.info({ msg: 'Connection closed', type: 'imapClient', account: this.account, disabled: imapClient.disabled }); try { for (let [, mailbox] of this.mailboxes) { @@ -653,10 +653,11 @@ class IMAPConnection extends BaseClient { } if (!imapClient.disabled) { + imapClient.logger.debug({ msg: 'Requesting reconnection due to unexpected close', type: 'imapClient', account: this.account }); await this.reconnect(); } } catch (err) { - this.logger.error({ msg: 'Connection close error', err }); + imapClient.logger.error({ msg: 'Connection close error', err }); } }); @@ -679,9 +680,6 @@ class IMAPConnection extends BaseClient { return false; } - this._connecting = true; - this.isClosed = false; - if (force) { this.closeSubconnections(); if (this.connectionClient) { @@ -689,6 +687,9 @@ class IMAPConnection extends BaseClient { } } + this._connecting = true; + this.isClosed = false; + try { this.logger.debug({ msg: 'Initiating connection to IMAP' }); await backOff(() => this.start(), { @@ -1224,16 +1225,17 @@ class IMAPConnection extends BaseClient { let imapConfig = await this.getImapConfig(accountData); imapConfig.id = `${imapConfig.id}:m:${this.connectionCount++}`; - this.imapClient = new ImapFlow( + let imapClient = new ImapFlow( Object.assign({}, imapConfig, { expungeHandler: async payload => await this.expungeHandler(payload) }) ); + this.imapClient = imapClient; - this.imapClient.primaryConnection = true; + imapClient.primaryConnection = true; // if emitLogs option is true then separate log event is fired for every log entry - this.imapClient.on('log', entry => { + imapClient.on('log', entry => { if (!entry) { return false; } @@ -1246,18 +1248,21 @@ class IMAPConnection extends BaseClient { this.accountLogger.log(entry); }); - this.imapClient.on('error', err => { + imapClient.on('error', err => { this.logger.error({ msg: 'IMAP connection error', type: 'imapClient', account: this.account, err }); + if (imapClient !== this.imapClient) { + return; + } this.reconnect().catch(err => { this.logger.error({ msg: 'IMAP reconnection error', account: this.account, err }); }); }); - this.imapClient.on('response', data => { + imapClient.on('response', data => { metricsMeta({}, this.logger, 'imapResponses', 'inc', data); // update byte counters as well - let imapStats = this.imapClient.stats(true); + let imapStats = imapClient.stats(true); metricsMeta({}, this.logger, 'imapBytesSent', 'inc', imapStats.sent); metricsMeta({}, this.logger, 'imapBytesReceived', 'inc', imapStats.received);