Skip to content

Commit fd206d1

Browse files
BleedingDevScriptedAlchemyzhoushaw
authored
fix(dts-plugin): handle EXIT before dev server init (#4429)
Co-authored-by: Zack Jackson <25274700+ScriptedAlchemy@users.noreply.github.com> Co-authored-by: 晓 <zhouxiao.shaw@bytedance.com>
1 parent 08f133f commit fd206d1

3 files changed

Lines changed: 103 additions & 14 deletions

File tree

packages/dts-plugin/src/dev-worker/forkDevWorker.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323

2424
import { DevWorkerOptions } from './DevWorker';
2525
import { getIpFromEntry } from './utils';
26+
import { handleDevWorkerMessage } from './handleWorkerMessage';
2627

2728
interface Options extends DevWorkerOptions {
2829
name: string;
@@ -189,20 +190,7 @@ export async function forkDevWorker(
189190
}
190191

191192
process.on('message', (message: rpc.RpcMessage) => {
192-
fileLog(
193-
`ChildProcess(${process.pid}), message: ${JSON.stringify(message)} `,
194-
'forkDevWorker',
195-
'info',
196-
);
197-
if (message.type === rpc.RpcGMCallTypes.EXIT) {
198-
fileLog(
199-
`ChildProcess(${process.pid}) SIGTERM, Federation DevServer will exit...`,
200-
'forkDevWorker',
201-
'error',
202-
);
203-
moduleServer?.exit();
204-
process.exit(0);
205-
}
193+
handleDevWorkerMessage(message, { moduleServer, log: fileLog });
206194
});
207195

208196
rpc.exposeRpc(forkDevWorker);
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { describe, expect, it, vi, afterEach } from 'vitest';
2+
3+
import { RpcGMCallTypes } from '../core/rpc/types';
4+
import { handleDevWorkerMessage } from './handleWorkerMessage';
5+
6+
describe('handleDevWorkerMessage', () => {
7+
afterEach(() => {
8+
vi.restoreAllMocks();
9+
});
10+
11+
it('does not throw when EXIT arrives before module server is initialized', () => {
12+
const processExit = vi.fn();
13+
14+
expect(() =>
15+
handleDevWorkerMessage(
16+
{
17+
type: RpcGMCallTypes.EXIT,
18+
id: 'exit-before-init',
19+
},
20+
{ processExit, log: vi.fn() },
21+
),
22+
).not.toThrow();
23+
24+
expect(processExit).toHaveBeenCalledWith(0);
25+
});
26+
27+
it('calls module server exit when present', () => {
28+
const processExit = vi.fn();
29+
const moduleServer = {
30+
exit: vi.fn(),
31+
};
32+
33+
handleDevWorkerMessage(
34+
{
35+
type: RpcGMCallTypes.EXIT,
36+
id: 'exit-with-server',
37+
},
38+
{ moduleServer, processExit, log: vi.fn() },
39+
);
40+
41+
expect(moduleServer.exit).toHaveBeenCalledTimes(1);
42+
expect(processExit).toHaveBeenCalledWith(0);
43+
});
44+
45+
it('ignores non-exit messages', () => {
46+
const processExit = vi.fn();
47+
const moduleServer = {
48+
exit: vi.fn(),
49+
};
50+
51+
handleDevWorkerMessage(
52+
{
53+
type: RpcGMCallTypes.CALL,
54+
id: 'message',
55+
args: [],
56+
},
57+
{ moduleServer, processExit, log: vi.fn() },
58+
);
59+
60+
expect(moduleServer.exit).not.toHaveBeenCalled();
61+
expect(processExit).not.toHaveBeenCalled();
62+
});
63+
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { RpcGMCallTypes, type RpcMessage } from '../core/rpc/types';
2+
3+
interface HandleDevWorkerMessageOptions {
4+
moduleServer?: {
5+
exit: () => void;
6+
};
7+
processExit?: (code?: number) => void;
8+
pid?: number;
9+
log?: (message: string, scope: string, level: string) => void;
10+
}
11+
12+
export function handleDevWorkerMessage(
13+
message: RpcMessage,
14+
options: HandleDevWorkerMessageOptions = {},
15+
): void {
16+
const {
17+
moduleServer,
18+
processExit = process.exit,
19+
pid = process.pid,
20+
log = () => undefined,
21+
} = options;
22+
23+
log(
24+
`ChildProcess(${pid}), message: ${JSON.stringify(message)} `,
25+
'forkDevWorker',
26+
'info',
27+
);
28+
29+
if (message.type === RpcGMCallTypes.EXIT) {
30+
log(
31+
`ChildProcess(${pid}) SIGTERM, Federation DevServer will exit...`,
32+
'forkDevWorker',
33+
'error',
34+
);
35+
moduleServer?.exit();
36+
processExit(0);
37+
}
38+
}

0 commit comments

Comments
 (0)