Skip to content

Commit

Permalink
Wait until process exit in esbuild.stop()
Browse files Browse the repository at this point in the history
This changes `esbuild.stop()` to only resolve the returned promise when the child process has actually exited.

This is useful for when you have some resource sanitization in place that checks that the child process has actually been closed.
  • Loading branch information
lucacasonato committed Aug 27, 2024
1 parent 3327274 commit c72c2be
Showing 1 changed file with 15 additions and 5 deletions.
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

0 comments on commit c72c2be

Please sign in to comment.