Skip to content

Commit

Permalink
fix(imap-socket-hang): When allocating IMAP connection, check if sock…
Browse files Browse the repository at this point in the history
…et is still alive ZMS-196 (#772)

* when allocating IMAP connection, check if socket is still alive

* imap notifier add optional chaining + fallback
  • Loading branch information
NickOvt authored Jan 9, 2025
1 parent 978ce06 commit 8feae38
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions lib/imap-notifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 8feae38

Please sign in to comment.