parent-death-guard-lease-host.ts 771 B

1234567891011121314151617181920
  1. import { spawn } from "node:child_process";
  2. import { writeFileSync } from "node:fs";
  3. import { armParentDeathGuard } from "../../src/utils/shell.ts";
  4. const marker = process.argv[2];
  5. if (!marker) throw new Error("marker path is required");
  6. const target = spawn("/bin/bash", ["-c", "while true; do sleep 0.1; done"], {
  7. detached: true,
  8. stdio: "ignore",
  9. });
  10. if (!target.pid) throw new Error("target pid is required");
  11. const firstLease = armParentDeathGuard(target.pid);
  12. const secondLease = armParentDeathGuard(target.pid);
  13. if (!firstLease || !secondLease) throw new Error("guardian leases are required");
  14. await Promise.all([firstLease.ready, secondLease.ready]);
  15. await firstLease.disarm();
  16. writeFileSync(marker, String(target.pid));
  17. setInterval(() => undefined, 60_000);