Skip to content
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

Use a WeakRef to store the resource #87

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const hook = createHook({

asyncResources.set(asyncId, {
type,
resource,
resourceRef: new WeakRef(resource),
jonkoops marked this conversation as resolved.
Show resolved Hide resolved
stacks
})
},
Expand All @@ -36,7 +36,15 @@ export default function whyIsNodeRunning (logger = console) {
hook.disable()

const activeAsyncResources = Array.from(asyncResources.values())
.filter(({ resource }) => resource.hasRef?.() ?? true)
.filter(({ resourceRef }) => {
const resource = resourceRef.deref()

if (resource === undefined) {
return false
}

return resource.hasRef?.() ?? true
})

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

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

function printStacks (asyncResource, logger) {
const stacks = asyncResource.stacks.filter((stack) => {
const fileName = stack.getFileName()
const fileName = stack.fileName
return fileName !== null && !fileName.startsWith('node:')
})

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

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

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

function formatLocation (stack) {
const filePath = formatFilePath(stack.getFileName())
return `${filePath}:${stack.getLineNumber()}`
const filePath = formatFilePath(stack.fileName)
return `${filePath}:${stack.lineNumber}`
}

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

function prepareStackTrace(error, stackTraces) {
return stackTraces.map(stack => ({
lineNumber: stack.getLineNumber(),
fileName: stack.getFileName()
}))
}

// See: https://v8.dev/docs/stack-trace-api
function captureStackTraces () {
const target = {}
const original = Error.prepareStackTrace

Error.prepareStackTrace = (error, stackTraces) => stackTraces
Error.prepareStackTrace = prepareStackTrace
Error.captureStackTrace(target, captureStackTraces)

const capturedTraces = target.stack
Expand Down