-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(NODE-6589): background task does not prune idle connections when minPoolSize=0 #4569
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
409f401
1ac3000
8fed111
ab28f3d
e054d28
b2c9675
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -145,4 +145,6 @@ export async function runServerSelectionLatencyWindowTest(test: ServerSelectionL | |
const observedFrequencies = calculateObservedFrequencies(selectedServers); | ||
|
||
compareResultsToExpected(test.outcome, observedFrequencies); | ||
|
||
await topology.close(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is one of the causes of leaks in unit tests - gotta close the topology. Before the change in this PR, the pool did not run the background thread by default. With this change, the background thread does run by default, which keeps the event loop open unless the pool's minPoolSize timer is explicitly cleared (which happens in pool.close()). This isn't an issue for in real scenarios because the server class always closes its pool. |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -123,7 +123,7 @@ async function executeServerSelectionTest(testDefinition) { | |
const readPreference = readPreferenceFromDefinition(testDefinition.read_preference); | ||
selector = ServerSelectors.readPreferenceServerSelector(readPreference); | ||
} catch (e) { | ||
if (testDefinition.error) return; | ||
if (testDefinition.error) return topology.close(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above. |
||
throw e; | ||
} | ||
} else { | ||
|
@@ -179,6 +179,8 @@ async function executeServerSelectionTest(testDefinition) { | |
// this is another expected error case | ||
if (expectedServers.length === 0 && err instanceof MongoServerSelectionError) return; | ||
throw err; | ||
} finally { | ||
topology.close(); | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ const { TimeoutContext } = require('../../mongodb'); | |
describe('Connection Pool', function () { | ||
let timeoutContext; | ||
let mockMongod; | ||
let pool; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above. |
||
const stubServer = { | ||
topology: { | ||
client: { | ||
|
@@ -50,6 +51,8 @@ describe('Connection Pool', function () { | |
timeoutContext = TimeoutContext.create({ waitQueueTimeoutMS: 0, serverSelectionTimeoutMS: 0 }); | ||
}); | ||
|
||
afterEach(() => pool?.close()); | ||
|
||
it('should destroy connections which have been closed', async function () { | ||
mockMongod.setMessageHandler(request => { | ||
const doc = request.document; | ||
|
@@ -61,7 +64,7 @@ describe('Connection Pool', function () { | |
} | ||
}); | ||
|
||
const pool = new ConnectionPool(stubServer, { | ||
pool = new ConnectionPool(stubServer, { | ||
maxPoolSize: 1, | ||
hostAddress: mockMongod.hostAddress() | ||
}); | ||
|
@@ -81,6 +84,8 @@ describe('Connection Pool', function () { | |
expect(events).to.have.length(1); | ||
const closeEvent = events[0]; | ||
expect(closeEvent).have.property('reason').equal('error'); | ||
|
||
conn.destroy(); | ||
}); | ||
|
||
it('should propagate socket timeouts to connections', async function () { | ||
|
@@ -93,7 +98,7 @@ describe('Connection Pool', function () { | |
} | ||
}); | ||
|
||
const pool = new ConnectionPool(stubServer, { | ||
pool = new ConnectionPool(stubServer, { | ||
maxPoolSize: 1, | ||
socketTimeoutMS: 200, | ||
hostAddress: mockMongod.hostAddress() | ||
|
@@ -117,7 +122,7 @@ describe('Connection Pool', function () { | |
} | ||
}); | ||
|
||
const pool = new ConnectionPool(stubServer, { | ||
pool = new ConnectionPool(stubServer, { | ||
maxPoolSize: 1, | ||
waitQueueTimeoutMS: 200, | ||
hostAddress: mockMongod.hostAddress() | ||
|
@@ -157,7 +162,7 @@ describe('Connection Pool', function () { | |
}); | ||
|
||
it('should respect the minPoolSizeCheckFrequencyMS option', function () { | ||
const pool = new ConnectionPool(stubServer, { | ||
pool = new ConnectionPool(stubServer, { | ||
minPoolSize: 2, | ||
minPoolSizeCheckFrequencyMS: 42, | ||
hostAddress: mockMongod.hostAddress() | ||
|
@@ -193,7 +198,7 @@ describe('Connection Pool', function () { | |
}); | ||
|
||
it('should default minPoolSizeCheckFrequencyMS to 100ms', function () { | ||
const pool = new ConnectionPool(stubServer, { | ||
pool = new ConnectionPool(stubServer, { | ||
minPoolSize: 2, | ||
hostAddress: mockMongod.hostAddress() | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was debugging these tests for a leak and refactored this as a drive-by improvement.