diff --git a/lib/imap-notifier.js b/lib/imap-notifier.js index 95876a40..82e36a00 100644 --- a/lib/imap-notifier.js +++ b/lib/imap-notifier.js @@ -393,19 +393,29 @@ class ImapNotifier extends EventEmitter { } let rlkey = 'lim:' + data.service; - this.counters.limitedcounter(rlkey, data.user, 1, data.limit || 15, (err, res) => { - if (err) { - return callback(err); - } + const { closed: socketClosed, connecting, destroyed } = data.session.socket; - if (!res.success) { - return callback(null, false); - } + if (!socketClosed && !connecting && !destroyed) { + // socket alive, not closed + this.counters.limitedcounter(rlkey, data.user, 1, data.limit || 15, (err, res) => { + if (err) { + return callback(err); + } - this.connectionSessions.set(data.session, { service: data.service, user: data.user }); + if (!res.success) { + return callback(null, false); + } - return callback(null, true); - }); + this.connectionSessions.set(data.session, { service: data.service, user: data.user }); + + return callback(null, true); + }); + } else { + // socket dead/closed + const err = new Error('[ALERT] Socket closed unexpectedly before authentication completed.'); + err.response = 'NO'; + return callback(err, false); + } } releaseConnection(data, callback) {