Skip to content

Commit cbe31af

Browse files
authored
fix: clear connection timeout on connection error (#1262)
1 parent 8ffc49e commit cbe31af

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/connection.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1896,6 +1896,7 @@ class Connection extends EventEmitter {
18961896
this.connectOnPort(port, this.config.options.multiSubnetFailover, signal);
18971897
});
18981898
}, (err) => {
1899+
this.clearConnectTimer();
18991900
if (err.name === 'AbortError') {
19001901
// Ignore the AbortError for now, this is still handled by the connectTimer firing
19011902
return;
@@ -1983,6 +1984,7 @@ class Connection extends EventEmitter {
19831984
this.transitionTo(this.STATE.SENT_PRELOGIN);
19841985
});
19851986
}, (err) => {
1987+
this.clearConnectTimer();
19861988
if (err.name === 'AbortError') {
19871989
return;
19881990
}

test/integration/connection-test.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,52 @@ describe('Initiate Connect Test', function() {
278278
connection.connect();
279279
});
280280

281+
it('should clear timeouts when failing to connect', function(done) {
282+
const connection = new Connection({
283+
server: 'something.invalid',
284+
options: { connectTimeout: 30000 },
285+
});
286+
287+
connection.on('connect', (err) => {
288+
try {
289+
assert.instanceOf(err, ConnectionError);
290+
assert.strictEqual(/** @type {ConnectionError} */(err).code, 'ESOCKET');
291+
assert.strictEqual(connection.connectTimer, undefined);
292+
done();
293+
} catch (e) {
294+
done(e);
295+
}
296+
});
297+
298+
connection.on('error', done);
299+
connection.connect();
300+
301+
assert.isOk(connection.connectTimer);
302+
});
303+
304+
it('should clear timeouts when failing to connect to named instance', function(done) {
305+
const connection = new Connection({
306+
server: 'something.invalid',
307+
options: {
308+
instanceName: 'inst',
309+
connectTimeout: 30000,
310+
},
311+
});
312+
313+
connection.on('connect', (err) => {
314+
assert.instanceOf(err, ConnectionError);
315+
assert.strictEqual(/** @type {ConnectionError} */(err).code, 'EINSTLOOKUP');
316+
assert.strictEqual(connection.connectTimer, undefined);
317+
318+
done();
319+
});
320+
321+
connection.on('error', done);
322+
connection.connect();
323+
324+
assert.isOk(connection.connectTimer);
325+
});
326+
281327
it('should fail if no cipher can be negotiated', function(done) {
282328
const config = getConfig();
283329
config.options.encrypt = true;

0 commit comments

Comments
 (0)