@@ -21,7 +21,7 @@ const hook = createHook({
21
21
22
22
asyncResources . set ( asyncId , {
23
23
type,
24
- resource,
24
+ resourceRef : new WeakRef ( resource ) ,
25
25
stacks
26
26
} )
27
27
} ,
@@ -36,7 +36,15 @@ export default function whyIsNodeRunning (logger = console) {
36
36
hook . disable ( )
37
37
38
38
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
+ } )
40
48
41
49
logger . error ( `There are ${ activeAsyncResources . length } handle(s) keeping the process running.` )
42
50
@@ -47,7 +55,7 @@ export default function whyIsNodeRunning (logger = console) {
47
55
48
56
function printStacks ( asyncResource , logger ) {
49
57
const stacks = asyncResource . stacks . filter ( ( stack ) => {
50
- const fileName = stack . getFileName ( )
58
+ const fileName = stack . fileName
51
59
return fileName !== null && ! fileName . startsWith ( 'node:' )
52
60
} )
53
61
@@ -66,8 +74,8 @@ function printStacks (asyncResource, logger) {
66
74
const padding = ' ' . repeat ( maxLength - location . length )
67
75
68
76
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 ( )
71
79
72
80
logger . error ( `${ location } ${ padding } - ${ line } ` )
73
81
} catch ( e ) {
@@ -77,8 +85,8 @@ function printStacks (asyncResource, logger) {
77
85
}
78
86
79
87
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 } `
82
90
}
83
91
84
92
function formatFilePath ( filePath ) {
@@ -92,12 +100,19 @@ function normalizeFilePath (filePath) {
92
100
return filePath . startsWith ( 'file://' ) ? fileURLToPath ( filePath ) : filePath
93
101
}
94
102
103
+ function prepareStackTrace ( error , stackTraces ) {
104
+ return stackTraces . map ( stack => ( {
105
+ lineNumber : stack . getLineNumber ( ) ,
106
+ fileName : stack . getFileName ( )
107
+ } ) )
108
+ }
109
+
95
110
// See: https://v8.dev/docs/stack-trace-api
96
111
function captureStackTraces ( ) {
97
112
const target = { }
98
113
const original = Error . prepareStackTrace
99
114
100
- Error . prepareStackTrace = ( error , stackTraces ) => stackTraces
115
+ Error . prepareStackTrace = prepareStackTrace
101
116
Error . captureStackTrace ( target , captureStackTraces )
102
117
103
118
const capturedTraces = target . stack
0 commit comments