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

fix(api-server): prevent race condition in server restart process #11731

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
45 changes: 27 additions & 18 deletions packages/api-server/src/serverManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,24 +94,33 @@ export class ServerManager {

// Try to gracefully close the server
// If it doesn't close within 2 seconds, forcefully close it
await Promise.race([
new Promise<void>((resolve) => {
console.log(chalk.yellow('Shutting down API server.'))
this.httpServerProcess!.on('exit', () => resolve())
this.httpServerProcess!.kill()
}),
new Promise<void>((resolve) =>
setTimeout(() => {
console.log(
chalk.yellow(
'API server did not exit within 2 seconds, forcefully closing it.',
),
)
this.httpServerProcess!.kill('SIGKILL')
resolve()
}, 2000),
),
])
await new Promise<void>((resolve) => {
console.log(chalk.yellow('Shutting down API server.'))

const cleanup = () => {
this.httpServerProcess?.removeAllListeners('exit')
clearTimeout(forceKillTimeout)
}

this.httpServerProcess!.on('exit', () => {
console.log(chalk.yellow('API server exited.'))
cleanup()
resolve()
})

const forceKillTimeout = setTimeout(() => {
console.log(
chalk.yellow(
'API server did not exit within 2 seconds, forcefully closing it.',
),
)
cleanup()
this.httpServerProcess!.kill('SIGKILL')
resolve()
}, 2000)

this.httpServerProcess!.kill()
})
}
}

Expand Down
Loading