-
Notifications
You must be signed in to change notification settings - Fork 1.8k
test(NODE-4262): simplify leak checker for startSession fixes #3281
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f7c9172
test: simplify leak checker for startSession fixes
nbbeeken ab5683d
fix: leak in fle test
nbbeeken 3c722e9
skip failures
nbbeeken 5001f3f
Update test/tools/runner/hooks/leak_checker.ts
nbbeeken 7f7cd57
clarify socket finding logic
nbbeeken bbf7e72
fix: ticket and simple init logic
nbbeeken da25c9b
give sockets tracking number
nbbeeken b1eb0ec
fix: lint
nbbeeken File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
/* eslint-disable @typescript-eslint/no-this-alias */ | ||
import { expect } from 'chai'; | ||
import * as chalk from 'chalk'; | ||
import * as net from 'net'; | ||
|
||
import { MongoClient } from '../../../../src/mongo_client'; | ||
import { ServerSessionPool } from '../../../../src/sessions'; | ||
|
||
class LeakChecker { | ||
static originalAcquire: typeof ServerSessionPool.prototype.acquire; | ||
static originalRelease: typeof ServerSessionPool.prototype.release; | ||
static kAcquiredCount: symbol; | ||
|
||
static originalConnect: typeof MongoClient.prototype.connect; | ||
static originalClose: typeof MongoClient.prototype.close; | ||
static kConnectCount: symbol; | ||
|
||
static { | ||
this.originalAcquire = ServerSessionPool.prototype.acquire; | ||
this.originalRelease = ServerSessionPool.prototype.release; | ||
this.kAcquiredCount = Symbol('acquiredCount'); | ||
this.originalConnect = MongoClient.prototype.connect; | ||
this.originalClose = MongoClient.prototype.close; | ||
this.kConnectCount = Symbol('connectedCount'); | ||
} | ||
|
||
clients: Set<MongoClient>; | ||
sessionPools: Set<ServerSessionPool>; | ||
|
||
constructor(public titlePath: string) { | ||
this.clients = new Set<MongoClient>(); | ||
this.sessionPools = new Set<ServerSessionPool>(); | ||
} | ||
|
||
setupSessionLeakChecker() { | ||
const leakChecker = this; | ||
ServerSessionPool.prototype.acquire = function (...args) { | ||
leakChecker.sessionPools.add(this); | ||
|
||
this[LeakChecker.kAcquiredCount] ??= 0; | ||
this[LeakChecker.kAcquiredCount] += 1; | ||
|
||
return LeakChecker.originalAcquire.call(this, ...args); | ||
}; | ||
|
||
ServerSessionPool.prototype.release = function (...args) { | ||
if (!(LeakChecker.kAcquiredCount in this)) { | ||
throw new Error('releasing before acquiring even once??'); | ||
} else { | ||
this[LeakChecker.kAcquiredCount] -= 1; | ||
} | ||
|
||
return LeakChecker.originalRelease.call(this, ...args); | ||
}; | ||
} | ||
|
||
setupClientLeakChecker() { | ||
const leakChecker = this; | ||
MongoClient.prototype.connect = function (...args) { | ||
leakChecker.clients.add(this); | ||
this[LeakChecker.kConnectCount] ??= 0; | ||
|
||
const lastArg = args[args.length - 1]; | ||
const lastArgIsCallback = typeof lastArg === 'function'; | ||
if (lastArgIsCallback) { | ||
const argsWithoutCallback = args.slice(0, args.length - 1); | ||
return LeakChecker.originalConnect.call(this, ...argsWithoutCallback, (error, client) => { | ||
if (error == null) { | ||
this[LeakChecker.kConnectCount] += 1; // only increment on successful connects | ||
} | ||
return lastArg(error, client); | ||
}); | ||
} else { | ||
return LeakChecker.originalConnect.call(this, ...args).then(client => { | ||
this[LeakChecker.kConnectCount] += 1; // only increment on successful connects | ||
return client; | ||
}); | ||
} | ||
}; | ||
|
||
MongoClient.prototype.close = function (...args) { | ||
this[LeakChecker.kConnectCount] ??= 0; // prevents NaN, its fine to call close on a client that never called connect | ||
this[LeakChecker.kConnectCount] -= 1; | ||
return LeakChecker.originalClose.call(this, ...args); | ||
}; | ||
} | ||
|
||
setup() { | ||
this.setupSessionLeakChecker(); | ||
this.setupClientLeakChecker(); | ||
} | ||
|
||
reset() { | ||
for (const sessionPool of this.sessionPools) { | ||
delete sessionPool[LeakChecker.kAcquiredCount]; | ||
} | ||
ServerSessionPool.prototype.acquire = LeakChecker.originalAcquire; | ||
ServerSessionPool.prototype.release = LeakChecker.originalRelease; | ||
this.sessionPools.clear(); | ||
|
||
for (const client of this.clients) { | ||
delete client[LeakChecker.kConnectCount]; | ||
} | ||
MongoClient.prototype.connect = LeakChecker.originalConnect; | ||
MongoClient.prototype.close = LeakChecker.originalClose; | ||
this.clients.clear(); | ||
} | ||
|
||
assert() { | ||
for (const pool of this.sessionPools) { | ||
expect(pool[LeakChecker.kAcquiredCount], 'ServerSessionPool acquired count').to.equal(0); | ||
} | ||
for (const client of this.clients) { | ||
expect(client[LeakChecker.kConnectCount], 'MongoClient connect count').to.be.lessThanOrEqual( | ||
dariakp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
0 | ||
); | ||
} | ||
} | ||
} | ||
|
||
let currentLeakChecker: LeakChecker | null; | ||
|
||
const leakCheckerBeforeEach = async function () { | ||
currentLeakChecker = new LeakChecker(this.currentTest.fullTitle()); | ||
currentLeakChecker.setup(); | ||
}; | ||
|
||
const leakCheckerAfterEach = async function () { | ||
let thrownError: Error | undefined; | ||
try { | ||
currentLeakChecker.assert(); | ||
} catch (error) { | ||
thrownError = error; | ||
} | ||
|
||
currentLeakChecker?.reset(); | ||
currentLeakChecker = null; | ||
|
||
if (thrownError instanceof Error) { | ||
this.test.error(thrownError); | ||
} | ||
}; | ||
|
||
const TRACE_SOCKETS = process.env.TRACE_SOCKETS === 'true' ? true : false; | ||
const kSocketId = Symbol('socketId'); | ||
const originalCreateConnection = net.createConnection; | ||
let socketCounter = 0n; | ||
|
||
const socketLeakCheckBeforeAll = function socketLeakCheckBeforeAll() { | ||
baileympearson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// @ts-expect-error: Typescript says this is readonly, but it is not at runtime | ||
net.createConnection = options => { | ||
const socket = originalCreateConnection(options); | ||
socket[kSocketId] = socketCounter.toString().padStart(5, '0'); | ||
socketCounter++; | ||
return socket; | ||
}; | ||
}; | ||
|
||
const filterHandlesForSockets = function (handle: any): handle is net.Socket { | ||
// Stdio are instanceof Socket so look for fd to be null | ||
return handle?.fd == null && handle instanceof net.Socket && handle?.destroyed !== true; | ||
}; | ||
|
||
const socketLeakCheckAfterEach: Mocha.AsyncFunc = async function socketLeakCheckAfterEach() { | ||
const indent = ' '.repeat(this.currentTest.titlePath().length + 1); | ||
|
||
const handles = (process as any)._getActiveHandles(); | ||
const sockets: net.Socket[] = handles.filter(handle => filterHandlesForSockets(handle)); | ||
|
||
for (const socket of sockets) { | ||
console.log( | ||
chalk.yellow( | ||
`${indent}⚡︎ socket ${socket[kSocketId]} not destroyed [${socket.localAddress}:${socket.localPort} → ${socket.remoteAddress}:${socket.remotePort}]` | ||
) | ||
); | ||
} | ||
}; | ||
|
||
const beforeAll = TRACE_SOCKETS ? [socketLeakCheckBeforeAll] : []; | ||
const beforeEach = [leakCheckerBeforeEach]; | ||
const afterEach = [leakCheckerAfterEach, ...(TRACE_SOCKETS ? [socketLeakCheckAfterEach] : [])]; | ||
module.exports = { mochaHooks: { beforeAll, beforeEach, afterEach } }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
should we leave a comment on Durran's current fle2 spec sync PR (NODE-4251) to rebase so that we can make sure to keep things consistent?
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.
Linked the jira, commented on GH, will comment on JIRA in a sec. I needed to skip these to check that there wasn't something hanging the run related to these changes.