Skip to content

Commit 06a2e2c

Browse files
fix(NODE-6469): pool is cleared before connection checkin on error (#4296)
1 parent aa986f8 commit 06a2e2c

14 files changed

+1109
-249
lines changed

src/cmap/connect.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ export async function performInitialHandshake(
113113
}
114114

115115
const start = new Date().getTime();
116-
const response = await conn.command(ns('admin.$cmd'), handshakeDoc, handshakeOptions);
116+
117+
const response = await executeHandshake(handshakeDoc, handshakeOptions);
117118

118119
if (!('isWritablePrimary' in response)) {
119120
// Provide hello-style response document.
@@ -175,6 +176,22 @@ export async function performInitialHandshake(
175176
// Connection establishment is socket creation (tcp handshake, tls handshake, MongoDB handshake (saslStart, saslContinue))
176177
// Once connection is established, command logging can log events (if enabled)
177178
conn.established = true;
179+
180+
async function executeHandshake(handshakeDoc: Document, handshakeOptions: CommandOptions) {
181+
try {
182+
const handshakeResponse = await conn.command(
183+
ns('admin.$cmd'),
184+
handshakeDoc,
185+
handshakeOptions
186+
);
187+
return handshakeResponse;
188+
} catch (error) {
189+
if (error instanceof MongoError) {
190+
error.addErrorLabel(MongoErrorLabel.HandshakeError);
191+
}
192+
throw error;
193+
}
194+
}
178195
}
179196

180197
/**

src/cmap/connection_pool.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,7 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
685685
},
686686
error => {
687687
this[kPending]--;
688+
this[kServer].handleError(error);
688689
this.emitAndLog(
689690
ConnectionPool.CONNECTION_CLOSED,
690691
new ConnectionClosedEvent(
@@ -719,9 +720,6 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
719720
// connection permits because that potentially delays the availability of
720721
// the connection to a checkout request
721722
this.createConnection((err, connection) => {
722-
if (err) {
723-
this[kServer].handleError(err);
724-
}
725723
if (!err && connection) {
726724
this[kConnections].push(connection);
727725
process.nextTick(() => this.processWaitQueue());

test/integration/connection-monitoring-and-pooling/connection_monitoring_and_pooling.spec.test.ts

-3
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,6 @@ describe('Connection Monitoring and Pooling Spec Tests (Integration) - logging',
6969
) {
7070
return 'not applicable: waitQueueSize not supported';
7171
}
72-
if (test.description === 'Connection checkout fails due to error establishing connection') {
73-
return 'TODO(NODE-5230): unskip this once event ordering issue is resolved';
74-
}
7572
return false;
7673
});
7774
});

test/integration/connection-monitoring-and-pooling/connection_monitoring_and_pooling.test.ts

-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { loadSpecTests } from '../../spec';
22
import { type CmapTest, runCmapTestSuite } from '../../tools/cmap_spec_runner';
3-
import { runUnifiedSuite } from '../../tools/unified-spec-runner/runner';
43

54
describe('Connection Monitoring and Pooling (Node Driver)', function () {
65
const cmapTests: CmapTest[] = loadSpecTests(
@@ -17,10 +16,4 @@ describe('Connection Monitoring and Pooling (Node Driver)', function () {
1716
}
1817
]
1918
});
20-
21-
// TODO(NODE-5230): Remove this once the actual unified tests (test/spec/connection-monitoring-and-pooling/logging) are passing
22-
const unifiedTests = loadSpecTests(
23-
'../integration/connection-monitoring-and-pooling/unified-cmap-node-specs'
24-
);
25-
runUnifiedSuite(unifiedTests);
2619
});

test/integration/connection-monitoring-and-pooling/unified-cmap-node-specs/connection-logging.json

-228
This file was deleted.

test/spec/connection-monitoring-and-pooling/cmap-format/pool-create-min-size-error.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@
4949
"type": "ConnectionCreated",
5050
"address": 42
5151
},
52+
{
53+
"type": "ConnectionPoolCleared",
54+
"address": 42
55+
},
5256
{
5357
"type": "ConnectionClosed",
5458
"address": 42,
5559
"connectionId": 42,
5660
"reason": "error"
57-
},
58-
{
59-
"type": "ConnectionPoolCleared",
60-
"address": 42
6161
}
6262
],
6363
"ignore": [

test/spec/connection-monitoring-and-pooling/cmap-format/pool-create-min-size-error.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ events:
3030
address: 42
3131
- type: ConnectionCreated
3232
address: 42
33+
- type: ConnectionPoolCleared
34+
address: 42
3335
- type: ConnectionClosed
3436
address: 42
3537
connectionId: 42
3638
reason: error
37-
- type: ConnectionPoolCleared
38-
address: 42
3939
ignore:
4040
- ConnectionPoolCreated

0 commit comments

Comments
 (0)