Skip to content

Commit

Permalink
Merge pull request #34 from ErKeLost/fix/child_killed
Browse files Browse the repository at this point in the history
fix: child process kill not be resolved
  • Loading branch information
ErKeLost authored Jul 21, 2024
2 parents ec78e5a + e435ef7 commit d349f84
Show file tree
Hide file tree
Showing 8 changed files with 269 additions and 134 deletions.
5 changes: 5 additions & 0 deletions .changeset/plenty-clocks-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'farmup': minor
---

fix: child process kill not be resolved
5 changes: 5 additions & 0 deletions .changeset/thirty-phones-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'farmup': patch
---

Improve the child process exit logic
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"private": true,
"type": "module",
"scripts": {
"dev": "pnpm --filter './packages/core' run dev",
"release": "pnpm --filter './packages/*' run build && changeset publish",
"ready": "farmup ./scripts/ready.ts -o ./dist/ready --no-config"
},
Expand All @@ -23,4 +24,4 @@
"execa": "^8.0.1",
"@changesets/cli": "^2.27.3"
}
}
}
4 changes: 2 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@
"email": "sshuang141@163.com"
},
"dependencies": {
"@farmfe/core": "^1.2.0"
"@farmfe/core": "^1.3.6"
}
}
}
33 changes: 27 additions & 6 deletions packages/core/src/core/executer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,16 @@ export class Executer {
}

async _execute(command: string, name: string, args: string[], logger: Logger) {

if (this.child) {
await this.closeChild();
}

const child = execaCommand([command, ...args].join(' '), {
cwd: process.cwd(),
stdio: 'pipe',
});


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

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

child.on('exit', (code) => {
child.once('exit', (code) => {
this.logger.info(`"${name}" PID ${child.pid} ${!code ? 'done' : `exit ${code}`}`);
this.child = undefined;
});
}

async closeChild() {
while (this.child && !this.child.killed) {
this.child.kill();
await delay(50);
if (!this.child) {
return;
}

const child = this.child;

const exitPromise = new Promise<void>((resolve) => {
child.once('exit', () => {
resolve();
});
});

try {
await this.terminateChild(child);
await exitPromise;
} finally {
this.child = undefined;
}
}

private async terminateChild(child: ExecaChildProcess) {
while (child && !child.killed) {
child.kill();
await delay(30);
}
this.child = undefined;
}
}
4 changes: 1 addition & 3 deletions playground/farm.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ export default defineConfig({
input: {
index: 'index.ts',
},
output: {

}
persistentCache: false,
},
});
5 changes: 4 additions & 1 deletion playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@
"farmup": "workspace:*"
},
"author": "",
"license": "ISC"
"license": "ISC",
"dependencies": {
"@farmfe/core": "^1.3.6"
}
}
Loading

0 comments on commit d349f84

Please sign in to comment.