Skip to content

Commit

Permalink
fix(pop3): run socket.destroy() if pop3 socket is not closed in 1.5s
Browse files Browse the repository at this point in the history
  • Loading branch information
andris9 committed Oct 5, 2023
1 parent 8766ab9 commit 2de6c0b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
3 changes: 3 additions & 0 deletions imap-core/lib/imap-connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ class IMAPConnection extends EventEmitter {
this._closing = false;
this._closed = false;

this._closingTimeout = null;

this.logger = {};
['info', 'debug', 'error'].forEach(level => {
this.logger[level] = (...args) => {
Expand Down Expand Up @@ -286,6 +288,7 @@ class IMAPConnection extends EventEmitter {

setImmediate(() => this._onClose());
}, 1500);
this._closingTimeout.unref();
}

this._closing = true;
Expand Down
28 changes: 28 additions & 0 deletions lib/pop3/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class POP3Connection extends EventEmitter {

this._closed = false;
this._closing = false;
this._closingTimeout = null;

this.secured = !!this._server.options.secure;
this._upgrading = false;
Expand Down Expand Up @@ -116,6 +117,7 @@ class POP3Connection extends EventEmitter {
* @event
*/
_onClose(/* hadError */) {
clearTimeout(this._closingTimeout);
if (this._closed) {
return;
}
Expand All @@ -138,6 +140,9 @@ class POP3Connection extends EventEmitter {
this.remoteAddress
);

// clear session
this.session = null;

this.emit('close');
}

Expand Down Expand Up @@ -184,9 +189,32 @@ class POP3Connection extends EventEmitter {
}

close() {
if (this._closed || this._closing) {
return;
}

if (!this._socket.destroyed && this._socket.writable) {
this._socket.end();
}

this._server.connections.delete(this);

// allow socket to close in 1500ms or force it to close
this._closingTimeout = setTimeout(() => {
if (this._closed) {
return;
}

try {
this._socket.destroy();
} catch (err) {
// ignore
}

setImmediate(() => this._onClose());
}, 1500);
this._closingTimeout.unref();

this._closing = true;
}

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"base32.js": "0.1.0",
"bcryptjs": "2.4.3",
"bson": "6.1.0",
"bullmq": "4.12.0",
"bullmq": "4.12.1",
"fido2-lib": "3.4.1",
"gelf": "2.0.1",
"generate-password": "1.7.0",
Expand All @@ -66,7 +66,7 @@
"ioredis": "5.3.2",
"ipaddr.js": "2.1.0",
"isemail": "3.2.0",
"joi": "17.10.2",
"joi": "17.11.0",
"js-yaml": "4.1.0",
"key-fingerprint": "1.1.0",
"libbase64": "1.2.1",
Expand Down

0 comments on commit 2de6c0b

Please sign in to comment.