Skip to content

Commit d349f84

Browse files
authored
Merge pull request #34 from ErKeLost/fix/child_killed
fix: child process kill not be resolved
2 parents ec78e5a + e435ef7 commit d349f84

File tree

8 files changed

+269
-134
lines changed

8 files changed

+269
-134
lines changed

.changeset/plenty-clocks-change.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'farmup': minor
3+
---
4+
5+
fix: child process kill not be resolved

.changeset/thirty-phones-juggle.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'farmup': patch
3+
---
4+
5+
Improve the child process exit logic

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"private": true,
66
"type": "module",
77
"scripts": {
8+
"dev": "pnpm --filter './packages/core' run dev",
89
"release": "pnpm --filter './packages/*' run build && changeset publish",
910
"ready": "farmup ./scripts/ready.ts -o ./dist/ready --no-config"
1011
},
@@ -23,4 +24,4 @@
2324
"execa": "^8.0.1",
2425
"@changesets/cli": "^2.27.3"
2526
}
26-
}
27+
}

packages/core/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,6 @@
5656
"email": "[email protected]"
5757
},
5858
"dependencies": {
59-
"@farmfe/core": "^1.2.0"
59+
"@farmfe/core": "^1.3.6"
6060
}
61-
}
61+
}

packages/core/src/core/executer.ts

+27-6
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,16 @@ export class Executer {
3131
}
3232

3333
async _execute(command: string, name: string, args: string[], logger: Logger) {
34+
3435
if (this.child) {
3536
await this.closeChild();
3637
}
37-
3838
const child = execaCommand([command, ...args].join(' '), {
3939
cwd: process.cwd(),
4040
stdio: 'pipe',
4141
});
4242

43+
4344
child.stdout?.on('data', (data) => logger.debug(trimEndLF(data.toString())));
4445

4546
child.stderr?.on('data', (err) => logger.error(err));
@@ -49,17 +50,37 @@ export class Executer {
4950
process.on('beforeExit', this.closeChild);
5051
process.on('exit', this.closeChild);
5152

52-
child.on('exit', (code) => {
53+
child.once('exit', (code) => {
5354
this.logger.info(`"${name}" PID ${child.pid} ${!code ? 'done' : `exit ${code}`}`);
5455
this.child = undefined;
5556
});
5657
}
5758

5859
async closeChild() {
59-
while (this.child && !this.child.killed) {
60-
this.child.kill();
61-
await delay(50);
60+
if (!this.child) {
61+
return;
62+
}
63+
64+
const child = this.child;
65+
66+
const exitPromise = new Promise<void>((resolve) => {
67+
child.once('exit', () => {
68+
resolve();
69+
});
70+
});
71+
72+
try {
73+
await this.terminateChild(child);
74+
await exitPromise;
75+
} finally {
76+
this.child = undefined;
77+
}
78+
}
79+
80+
private async terminateChild(child: ExecaChildProcess) {
81+
while (child && !child.killed) {
82+
child.kill();
83+
await delay(30);
6284
}
63-
this.child = undefined;
6485
}
6586
}

playground/farm.config.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ export default defineConfig({
55
input: {
66
index: 'index.ts',
77
},
8-
output: {
9-
10-
}
8+
persistentCache: false,
119
},
1210
});

playground/package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,8 @@
1313
"farmup": "workspace:*"
1414
},
1515
"author": "",
16-
"license": "ISC"
16+
"license": "ISC",
17+
"dependencies": {
18+
"@farmfe/core": "^1.3.6"
19+
}
1720
}

0 commit comments

Comments
 (0)