Skip to content

Commit

Permalink
Fix Threading
Browse files Browse the repository at this point in the history
  • Loading branch information
timbrinded committed Oct 13, 2023
1 parent 2c85034 commit cd36fb6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 25 deletions.
5 changes: 5 additions & 0 deletions .changeset/heavy-pots-deny.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@moonwall/cli": patch
---

Fix Threading
41 changes: 16 additions & 25 deletions packages/cli/src/lib/globalContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,37 +336,20 @@ export class MoonwallContext {
}

public static async destroy() {
const ctx = MoonwallContext.getContext();
const ctx = this.instance;

try {
await ctx.disconnect();
} catch {
console.log("🛑 All connections disconnected");
}
const promises = ctx.nodes.map((process) => {
return new Promise((resolve, reject) => {
process.kill(); // SIGTERM

// If the process hasn't exited after 5 seconds, we'll forcefully kill it
const timeout = 5000;
setTimeout(() => {
if (!process.killed) {
// If the process hasn't exited, forcefully kill it
process.kill("SIGKILL");
reject(`process ${process.pid} had to be forcefully killed`);
} else {
resolve(`process ${process.pid} killed gracefully`);
}
}, timeout);
});
// return new Promise((resolve) => {
// process.kill();
// if (process.killed) {
// resolve(`process ${process.pid} killed`);
// }
// });
});

await Promise.all(promises);
for (const node of ctx.nodes) {
node.kill();
while (await isPidRunning(node.pid)) {
await timer(100);
}
}

if (ctx.zombieNetwork) {
console.log("🪓 Killing zombie nodes");
Expand Down Expand Up @@ -401,3 +384,11 @@ export interface IGlobalContextFoundation {
}[];
foundationType: FoundationType;
}

function isPidRunning(pid: number): Promise<boolean> {
return new Promise((resolve) => {
exec(`ps -p ${pid} -o pid=`, (error, stdout, stderr) => {
resolve(!error && stdout.trim() !== "");
});
});
}

0 comments on commit cd36fb6

Please sign in to comment.