Skip to content

Commit b8cd6aa

Browse files
Properly kill dev api-server (#11691)
Changes are: - await the process where we kill the server process when restarting. So that we don't race the new instance of httpServer with existing ones (while being killed). - try to gracefully kill the api server, if it's not killed within 2sec, SIGTERM it. - unrelated: moved a comment to the right place. I accidentally moved it wrong place while refactoring in the last PR.
1 parent 3e556a1 commit b8cd6aa

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

.changesets/11691.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- Await and properly kill dev api-server (#11691) by @callingmedic911
2+
3+
Sometime the api-server doesn't get killed in time before the new instance is started. This change makes sure that we wait for the process. If it's not killed within 2 seconds with SIGTERM, we send a SIGKILL to it.

packages/api-server/src/buildManager.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ export type BuildAndRestartOptions = {
55
clean?: boolean
66
}
77

8-
// We want to delay execution when multiple files are modified on the filesystem,
9-
// this usually happens when running RedwoodJS generator commands.
10-
// Local writes are very fast, but writes in e2e environments are not,
11-
// so allow the default to be adjusted with an env-var.
12-
//
138
class BuildManager {
149
private shouldRebuild: boolean
1510
private shouldClean: boolean
@@ -34,6 +29,11 @@ class BuildManager {
3429
this.shouldClean = false
3530
}
3631
},
32+
// We want to delay execution when multiple files are modified on the filesystem,
33+
// this usually happens when running RedwoodJS generator commands.
34+
// Local writes are very fast, but writes in e2e environments are not,
35+
// so allow the default to be adjusted with an env-var.
36+
//
3737
process.env.RWJS_DELAY_RESTART
3838
? parseInt(process.env.RWJS_DELAY_RESTART, 10)
3939
: 500,

packages/api-server/src/serverManager.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { fork } from 'child_process'
33
import fs from 'fs'
44
import path from 'path'
55

6+
import chalk from 'chalk'
67
import yargs from 'yargs'
78
import { hideBin } from 'yargs/helpers'
89

@@ -82,13 +83,35 @@ export class ServerManager {
8283
}
8384

8485
async restartApiServer() {
85-
this.killApiServer()
86+
await this.killApiServer()
8687
await this.startApiServer()
8788
}
8889

89-
killApiServer() {
90-
this.httpServerProcess?.emit('exit')
91-
this.httpServerProcess?.kill()
90+
async killApiServer() {
91+
if (!this.httpServerProcess) {
92+
return
93+
}
94+
95+
// Try to gracefully close the server
96+
// If it doesn't close within 2 seconds, forcefully close it
97+
await Promise.race([
98+
new Promise<void>((resolve) => {
99+
console.log(chalk.yellow('Shutting down API server.'))
100+
this.httpServerProcess!.on('exit', () => resolve())
101+
this.httpServerProcess!.kill()
102+
}),
103+
new Promise<void>((resolve) =>
104+
setTimeout(() => {
105+
console.log(
106+
chalk.yellow(
107+
'API server did not exit within 2 seconds, forcefully closing it.',
108+
),
109+
)
110+
this.httpServerProcess!.kill('SIGKILL')
111+
resolve()
112+
}, 2000),
113+
),
114+
])
92115
}
93116
}
94117

0 commit comments

Comments
 (0)