Skip to content

Commit 74916dd

Browse files
committed
fix: stop profiler synchronously
1 parent cd04391 commit 74916dd

3 files changed

Lines changed: 12 additions & 58 deletions

File tree

packages/nuxi/src/commands/build.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { overrideEnv } from '../utils/env'
1010
import { clearBuildDir } from '../utils/fs'
1111
import { loadKit } from '../utils/kit'
1212
import { logger } from '../utils/logger'
13-
import { installSignalHandlers, startCpuProfile, stopCpuProfile } from '../utils/profile'
13+
import { startCpuProfile, stopCpuProfile } from '../utils/profile'
1414
import { cwdArgs, dotEnvArgs, envNameArgs, extendsArgs, legacyRootDirArgs, logLevelArgs, profileArgs } from './_shared'
1515

1616
export default defineCommand({
@@ -44,7 +44,6 @@ export default defineCommand({
4444
const perfValue = profileArg === 'verbose' ? true : profileArg ? 'quiet' : undefined
4545
if (profileArg) {
4646
await startCpuProfile()
47-
installSignalHandlers(cwd)
4847
}
4948

5049
intro(colors.cyan('Building Nuxt for production...'))
@@ -103,9 +102,7 @@ export default defineCommand({
103102

104103
await kit.buildNuxt(nuxt)
105104

106-
if (profileArg) {
107-
await stopCpuProfile(cwd).catch(() => {})
108-
}
105+
stopCpuProfile(cwd)
109106

110107
if (ctx.args.prerender) {
111108
if (!nuxt.options.ssr) {

packages/nuxi/src/dev/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { NuxtDevContext, NuxtDevIPCMessage, NuxtParentIPCMessage } from './
55
import process from 'node:process'
66
import defu from 'defu'
77
import { overrideEnv } from '../utils/env.ts'
8-
import { installSignalHandlers, startCpuProfile, stopCpuProfile } from '../utils/profile.ts'
8+
import { startCpuProfile, stopCpuProfile } from '../utils/profile.ts'
99
import { NuxtDevServer } from './utils'
1010

1111
const start = Date.now()
@@ -64,7 +64,6 @@ export async function initialize(devContext: NuxtDevContext, ctx: InitializeOpti
6464

6565
if (profileArg) {
6666
await startCpuProfile()
67-
installSignalHandlers(devContext.cwd)
6867
}
6968

7069
const devServer = new NuxtDevServer({
@@ -120,6 +119,10 @@ export async function initialize(devContext: NuxtDevContext, ctx: InitializeOpti
120119
console.debug(`Dev server (internal) initialized in ${Date.now() - start}ms`)
121120
}
122121

122+
if (profileArg) {
123+
process.once('exit', () => stopCpuProfile(devContext.cwd))
124+
}
125+
123126
return {
124127
listener: devServer.listener,
125128
close: async () => {
@@ -128,9 +131,6 @@ export async function initialize(devContext: NuxtDevContext, ctx: InitializeOpti
128131
devServer.listener.close(),
129132
devServer.close(),
130133
])
131-
if (profileArg) {
132-
await stopCpuProfile(devContext.cwd).catch(() => {})
133-
}
134134
},
135135
onReady: (callback: (address: string) => void) => {
136136
if (address) {

packages/nuxi/src/utils/profile.ts

Lines changed: 5 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import type { Session } from 'node:inspector'
22
import { mkdirSync, writeFileSync } from 'node:fs'
3-
import { mkdir, writeFile } from 'node:fs/promises'
4-
import process from 'node:process'
53
import { colors } from 'consola/utils'
64
import { join } from 'pathe'
75
import { logger } from './logger'
@@ -10,8 +8,7 @@ let session: Session | undefined
108
let profileCount = 0
119

1210
export async function startCpuProfile(): Promise<void> {
13-
// Adopt session started in bin/nuxi.mjs
14-
const cli = globalThis.__nuxt_cli__ as typeof globalThis.__nuxt_cli__ & { cpuProfileSession?: import('node:inspector').Session }
11+
const cli = globalThis.__nuxt_cli__ as Record<string, any> | undefined
1512
if (cli?.cpuProfileSession) {
1613
session = cli.cpuProfileSession
1714
delete cli.cpuProfileSession
@@ -26,38 +23,15 @@ export async function startCpuProfile(): Promise<void> {
2623
if (err) {
2724
rej(err)
2825
}
29-
else { res() }
26+
else {
27+
res()
28+
}
3029
})
3130
})
3231
})
3332
}
3433

35-
export function stopCpuProfile(outDir: string): Promise<string | undefined> {
36-
if (!session) {
37-
return Promise.resolve(undefined)
38-
}
39-
const s = session
40-
session = undefined
41-
return new Promise((res, rej) => {
42-
s.post('Profiler.stop', (err, { profile }) => {
43-
if (err) {
44-
return rej(err)
45-
}
46-
const outPath = join(outDir, `profile-${profileCount++}.cpuprofile`)
47-
mkdir(outDir, { recursive: true })
48-
.then(() => writeFile(outPath, JSON.stringify(profile)))
49-
.then(() => {
50-
logger.info(`CPU profile written to ${colors.cyan(outPath)}`)
51-
logger.info(`Open it in ${colors.cyan('https://www.speedscope.app')} or Chrome DevTools`)
52-
s.disconnect()
53-
res(outPath)
54-
})
55-
.catch(rej)
56-
})
57-
})
58-
}
59-
60-
function stopCpuProfileSync(outDir: string): string | undefined {
34+
export function stopCpuProfile(outDir: string): string | undefined {
6135
if (!session) {
6236
return
6337
}
@@ -80,20 +54,3 @@ function stopCpuProfileSync(outDir: string): string | undefined {
8054
})
8155
return outPath
8256
}
83-
84-
/**
85-
* Install signal handlers that flush the CPU profile before exit.
86-
* Returns a cleanup function to remove the handlers.
87-
*/
88-
export function installSignalHandlers(outDir: string): () => void {
89-
const onSignal = (signal: NodeJS.Signals) => {
90-
stopCpuProfileSync(outDir)
91-
process.kill(process.pid, signal)
92-
}
93-
process.once('SIGINT', onSignal)
94-
process.once('SIGTERM', onSignal)
95-
return () => {
96-
process.off('SIGINT', onSignal)
97-
process.off('SIGTERM', onSignal)
98-
}
99-
}

0 commit comments

Comments
 (0)