Skip to content

Commit 2d08b0d

Browse files
betalbjonkoops
andauthored
Store the resource in a WeakRef to avoid keeping a reference (#87)
Closes #84 Co-authored-by: Jon Koops <jonkoops@gmail.com>
1 parent 01906a9 commit 2d08b0d

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

index.js

+23-8
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const hook = createHook({
2121

2222
asyncResources.set(asyncId, {
2323
type,
24-
resource,
24+
resourceRef: new WeakRef(resource),
2525
stacks
2626
})
2727
},
@@ -36,7 +36,15 @@ export default function whyIsNodeRunning (logger = console) {
3636
hook.disable()
3737

3838
const activeAsyncResources = Array.from(asyncResources.values())
39-
.filter(({ resource }) => resource.hasRef?.() ?? true)
39+
.filter(({ resourceRef }) => {
40+
const resource = resourceRef.deref()
41+
42+
if (resource === undefined) {
43+
return false
44+
}
45+
46+
return resource.hasRef?.() ?? true
47+
})
4048

4149
logger.error(`There are ${activeAsyncResources.length} handle(s) keeping the process running.`)
4250

@@ -47,7 +55,7 @@ export default function whyIsNodeRunning (logger = console) {
4755

4856
function printStacks (asyncResource, logger) {
4957
const stacks = asyncResource.stacks.filter((stack) => {
50-
const fileName = stack.getFileName()
58+
const fileName = stack.fileName
5159
return fileName !== null && !fileName.startsWith('node:')
5260
})
5361

@@ -66,8 +74,8 @@ function printStacks (asyncResource, logger) {
6674
const padding = ' '.repeat(maxLength - location.length)
6775

6876
try {
69-
const lines = readFileSync(normalizeFilePath(stack.getFileName()), 'utf-8').split(/\n|\r\n/)
70-
const line = lines[stack.getLineNumber() - 1].trim()
77+
const lines = readFileSync(normalizeFilePath(stack.fileName), 'utf-8').split(/\n|\r\n/)
78+
const line = lines[stack.lineNumber - 1].trim()
7179

7280
logger.error(`${location}${padding} - ${line}`)
7381
} catch (e) {
@@ -77,8 +85,8 @@ function printStacks (asyncResource, logger) {
7785
}
7886

7987
function formatLocation (stack) {
80-
const filePath = formatFilePath(stack.getFileName())
81-
return `${filePath}:${stack.getLineNumber()}`
88+
const filePath = formatFilePath(stack.fileName)
89+
return `${filePath}:${stack.lineNumber}`
8290
}
8391

8492
function formatFilePath (filePath) {
@@ -92,12 +100,19 @@ function normalizeFilePath (filePath) {
92100
return filePath.startsWith('file://') ? fileURLToPath(filePath) : filePath
93101
}
94102

103+
function prepareStackTrace(error, stackTraces) {
104+
return stackTraces.map(stack => ({
105+
lineNumber: stack.getLineNumber(),
106+
fileName: stack.getFileName()
107+
}))
108+
}
109+
95110
// See: https://v8.dev/docs/stack-trace-api
96111
function captureStackTraces () {
97112
const target = {}
98113
const original = Error.prepareStackTrace
99114

100-
Error.prepareStackTrace = (error, stackTraces) => stackTraces
115+
Error.prepareStackTrace = prepareStackTrace
101116
Error.captureStackTrace(target, captureStackTraces)
102117

103118
const capturedTraces = target.stack

0 commit comments

Comments
 (0)