Skip to content

Commit

Permalink
fix(logout): Fixed race condition for logout and TCP close. Fixes #161
Browse files Browse the repository at this point in the history
andris9 committed Jan 16, 2024

Verified

This commit was signed with the committer’s verified signature.
andris9 Andris Reinman
1 parent 93034a7 commit 39a7333
Showing 2 changed files with 22 additions and 10 deletions.
3 changes: 3 additions & 0 deletions lib/commands/logout.js
Original file line number Diff line number Diff line change
@@ -18,6 +18,9 @@ module.exports = async connection => {
response = await connection.exec('LOGOUT');
return true;
} catch (err) {
if (err.code === 'NoConnection') {
return true;
}
connection.log.warn({ err, cid: connection.id });
return false;
} finally {
29 changes: 19 additions & 10 deletions lib/imap-flow.js
Original file line number Diff line number Diff line change
@@ -371,7 +371,9 @@ class ImapFlow extends EventEmitter {
let request = this.requestTagMap.get(data.tag);
if (request) {
this.requestTagMap.delete(request.tag);
request.reject(new Error('Connection not available'));
const error = new Error('Connection not available');
error.code = 'NoConnection';
request.reject(error);
}
}
return;
@@ -595,6 +597,7 @@ class ImapFlow extends EventEmitter {

default: {
let err = new Error('Invalid server response');
err.code = 'InvalidResponse';
err.response = parsed;
request.reject(err);
break;
@@ -1393,7 +1396,7 @@ class ImapFlow extends EventEmitter {
* await client.logout();
*/
async logout() {
await this.run('LOGOUT');
return await this.run('LOGOUT');
}

/**
@@ -1432,9 +1435,11 @@ class ImapFlow extends EventEmitter {
// reject command that is currently processed
if (this.currentRequest && this.requestTagMap.has(this.currentRequest.tag)) {
let request = this.requestTagMap.get(this.currentRequest.tag);
if (request) {
if (request && ['LOGOUT'].includes(request.command)) {
this.requestTagMap.delete(request.tag);
request.reject(new Error('Connection not available'));
const error = new Error('Connection not available');
error.code = 'NoConnection';
request.reject(error);
}
this.currentRequest = false;
}
@@ -1446,7 +1451,9 @@ class ImapFlow extends EventEmitter {
let request = this.requestTagMap.get(req.tag);
if (request) {
this.requestTagMap.delete(request.tag);
request.reject(new Error('Connection not available'));
const error = new Error('Connection not available');
error.code = 'NoConnection';
request.reject(error);
}
}
}
@@ -2728,8 +2735,9 @@ class ImapFlow extends EventEmitter {
}

if (this.socket.destroyed) {
let err = new Error('Connection not available');
throw err;
const error = new Error('Connection not available');
error.code = 'NoConnection';
throw error;
}

clearTimeout(this.idleStartTimer);
@@ -2771,10 +2779,11 @@ class ImapFlow extends EventEmitter {
const { resolve, reject, path, options, lockId } = this.locks.shift();

if (!this.usable || this.socket.destroyed) {
// reject all
let err = new Error('Connection not available');
this.log.trace({ msg: 'Failed to acquire mailbox lock', path, lockId });
reject(err);
// reject all
let error = new Error('Connection not available');
error.code = 'NoConnection';
reject(error);
return await this.processLocks(true);
}

0 comments on commit 39a7333

Please sign in to comment.