Skip to content

Commit 79fdf6a

Browse files
committed
try avoinding race conditions of sub processes not being closed
1 parent a4f17cc commit 79fdf6a

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

benchmarks/preview-server/src/local-vs-2.1.7-canary.2-on-startup.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,22 @@ const bench = new Bench({
2121

2222
bench
2323
.add('startup on local', async () => {
24-
const server = await runServer(pathToLocalCliScript);
25-
await fetch(`${server.url}/preview/magic-links/notion-magic-link`);
26-
server.subprocess.kill();
24+
try {
25+
const server = await runServer(pathToLocalCliScript);
26+
await fetch(`${server.url}/preview/magic-links/notion-magic-link`);
27+
if (!server.subprocess.kill()) {
28+
throw new Error('could not close sub process for preview server');
29+
}
30+
} catch (err) {
31+
console.error('Error starting local server:', err);
32+
}
2733
})
2834
.add('startup on 2.1.7-canary.2', async () => {
2935
const server = await runServer(pathToCanaryCliScript);
3036
await fetch(`${server.url}/preview/magic-links/notion-magic-link`);
31-
server.subprocess.kill();
37+
if (!server.subprocess.kill()) {
38+
throw new Error('could not close sub process for preview server');
39+
}
3240
});
3341

3442
await bench.run();

benchmarks/preview-server/src/utils/run-server.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,25 @@ export interface Server {
1111
export function runServer(pathToCliScript: string) {
1212
return new Promise<Server>((resolve, reject) => {
1313
const node = spawn('node', [pathToCliScript, 'dev'], {
14-
cwd: path.resolve(__dirname, '../../../../apps/demo'),
14+
cwd: path.resolve(import.meta.dirname, '../../../../apps/demo'),
15+
});
16+
17+
const kill = () => {
18+
node.kill();
19+
};
20+
21+
process.addListener('exit', kill);
22+
process.addListener('SIGINT', kill);
23+
process.addListener('SIGTERM', kill);
24+
process.addListener('SIGUSR1', kill);
25+
process.addListener('SIGUSR2', kill);
26+
27+
node.on('close', () => {
28+
process.removeListener('exit', kill);
29+
process.removeListener('SIGINT', kill);
30+
process.removeListener('SIGTERM', kill);
31+
process.removeListener('SIGUSR1', kill);
32+
process.removeListener('SIGUSR2', kill);
1533
});
1634

1735
node.stdout.on('data', (data) => {

0 commit comments

Comments
 (0)