From 8feae388367ca8804bb009a8d745e79c64aa9d25 Mon Sep 17 00:00:00 2001 From: NickOvt Date: Thu, 9 Jan 2025 10:19:34 +0200 Subject: [PATCH] fix(imap-socket-hang): When allocating IMAP connection, check if socket is still alive ZMS-196 (#772) * when allocating IMAP connection, check if socket is still alive * imap notifier add optional chaining + fallback --- lib/imap-notifier.js | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/lib/imap-notifier.js b/lib/imap-notifier.js index 95876a40..86fe462c 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/undefined + const err = new Error('[ALERT] Socket closed unexpectedly before authentication completed.'); + err.response = 'NO'; + return callback(err, false); + } } releaseConnection(data, callback) {