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

Wait until process exit in esbuild.stop() #3897

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
20 changes: 15 additions & 5 deletions lib/npm/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,9 @@ export let analyzeMetafileSync: typeof types.analyzeMetafileSync = (metafile, op
return result!
}

export const stop = () => {
if (stopService) stopService()
export const stop = async () => {
if (stopService) await stopService()
if (workerThreadService) workerThreadService.stop()
return Promise.resolve()
}

let initializeWasCalled = false
Expand All @@ -249,7 +248,7 @@ interface Service {

let defaultWD = process.cwd()
let longLivedService: Service | undefined
let stopService: (() => void) | undefined
let stopService: (() => Promise<void>) | undefined

let ensureServiceIsRunning = (): Service => {
if (longLivedService) return longLivedService
Expand Down Expand Up @@ -285,14 +284,25 @@ let ensureServiceIsRunning = (): Service => {
stdout.on('data', readFromStdout)
stdout.on('end', afterClose)

stopService = () => {
stopService = async () => {
const exited = new Promise<void>(resolve => {
if (child.exitCode !== null || child.signalCode !== null) {
resolve()
} else {
// Re-ref the child so that this process's event loop doesn't starve
// if the user waits for the `stop` promise to resolve.
refs.ref()
child.on('exit', resolve)
}
})
// Close all resources related to the subprocess.
stdin.destroy()
stdout.destroy()
child.kill()
initializeWasCalled = false
longLivedService = undefined
stopService = undefined
await exited
}

let refCount = 0
Expand Down
Loading