Skip to content

Commit bd592ec

Browse files
authored
chore: capture more informative stack trace in session leak checker (#2618)
Since we migrated to acquiring a server session lazily the stack traces for the sesson leak checker haven't been very helpful. This patch records the trace from the call to `Topology#startSession` in order to provide a more high quality trace. NODE-2880
1 parent 2e36815 commit bd592ec

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

test/tools/runner/plugins/session_leak_checker.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@ function getSessionLeakMetadata(currentTest) {
1212
return (currentTest.metadata && currentTest.metadata.sessions) || {};
1313
}
1414

15+
const kTrace = Symbol('trace');
1516
function dumpSessionInfo(which, sessions) {
16-
console.log(which);
17+
console.warn(which);
1718
sessions.forEach(session => {
18-
console.log(` >> ${JSON.stringify(session.id)}`);
19-
console.log(session.trace);
19+
console.warn(` >> ${JSON.stringify(session.id)}`);
20+
if (session[kTrace]) {
21+
console.warn(session[kTrace]);
22+
}
2023
});
2124
}
2225

@@ -32,10 +35,31 @@ beforeEach('Session Leak Before Each - setup session tracking', function () {
3235
return;
3336
}
3437

38+
const _startSession = Topology.prototype.startSession;
39+
sandbox.stub(Topology.prototype, 'startSession').callsFake(function () {
40+
const session = _startSession.apply(this, arguments);
41+
const stackTrace = new Error().stack;
42+
const result = new Proxy(session, {
43+
get: function (target, prop) {
44+
if (prop === 'serverSession') {
45+
const serverSession = target[prop];
46+
if (serverSession[kTrace] == null) {
47+
serverSession[kTrace] = stackTrace;
48+
}
49+
50+
return serverSession;
51+
}
52+
53+
return Reflect.get(...arguments);
54+
}
55+
});
56+
57+
return result;
58+
});
59+
3560
const _acquire = ServerSessionPool.prototype.acquire;
3661
sandbox.stub(ServerSessionPool.prototype, 'acquire').callsFake(function () {
3762
const session = _acquire.apply(this, arguments);
38-
session.trace = new Error().stack;
3963
activeSessions.add(session);
4064
return session;
4165
});

0 commit comments

Comments
 (0)