Skip to content

Commit 8d31557

Browse files
steipeteRileyJJY
andauthored
fix(scripts): terminate stalled dependency pin Git commands
Bound both dependency-pin Git subprocesses and terminate SIGTERM-resistant children. Preserve sparse-index reads, Windows-native Git, exact timeout errors, and script declaration contracts. Co-authored-by: RileyJJY <[email protected]>
1 parent fded35d commit 8d31557

2 files changed

Lines changed: 50 additions & 48 deletions

File tree

‎scripts/check-dependency-pins.mjs‎

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,23 @@ const PINNED_GITHUB_TARBALL_PATTERN =
1717
/^https:\/\/codeload\.github\.com\/[^/\s]+\/[^/\s]+\/tar\.gz\/[0-9a-f]{40}$/iu;
1818
const DEFAULT_GIT_TIMEOUT_MS = 60_000;
1919

20-
function formatGitArgs(args) {
21-
return args.join(" ");
22-
}
23-
24-
function createGitError(args, error, timeoutMs) {
25-
const timedOut =
26-
error?.code === "ETIMEDOUT" ||
27-
error?.signal === "SIGTERM" ||
28-
/timed out|timeout/i.test(String(error?.message ?? ""));
29-
return new Error(
30-
timedOut
31-
? `dependency pin guard: git ${formatGitArgs(args)} timed out after ${timeoutMs}ms.`
32-
: `dependency pin guard: git ${formatGitArgs(args)} failed.`,
33-
{ cause: error },
34-
);
35-
}
36-
3720
function runGit(cwd, args, timeoutMs = DEFAULT_GIT_TIMEOUT_MS) {
3821
try {
3922
return execFileSync("git", args, {
4023
cwd,
4124
encoding: "utf8",
4225
timeout: timeoutMs,
26+
// A synchronous child that ignores SIGTERM otherwise keeps its parent blocked.
27+
killSignal: "SIGKILL",
4328
});
4429
} catch (error) {
45-
throw createGitError(args, error, timeoutMs);
30+
if (error?.code === "ETIMEDOUT") {
31+
throw new Error(
32+
`dependency pin guard: git ${args.join(" ")} timed out after ${timeoutMs}ms.`,
33+
{ cause: error },
34+
);
35+
}
36+
throw error;
4637
}
4738
}
4839

@@ -145,20 +136,17 @@ export function collectDependencyPinViolations(
145136
/**
146137
* Builds the full dependency pin audit payload.
147138
*/
148-
function collectDependencyPinAudit(
149-
cwd = process.cwd(),
150-
{ gitTimeoutMs = DEFAULT_GIT_TIMEOUT_MS } = {},
151-
) {
152-
const packageJsonFiles = listTrackedPackageJsonFiles(cwd, gitTimeoutMs);
139+
function collectDependencyPinAudit(cwd = process.cwd()) {
140+
const packageJsonFiles = listTrackedPackageJsonFiles(cwd);
153141
let packageSpecCount = 0;
154142
for (const relativePath of packageJsonFiles) {
155-
const packageJson = readTrackedJson(cwd, relativePath, gitTimeoutMs);
143+
const packageJson = readTrackedJson(cwd, relativePath);
156144
for (const section of PACKAGE_DEPENDENCY_SECTIONS) {
157145
packageSpecCount += Object.keys(packageJson[section] ?? {}).length;
158146
}
159147
}
160148
const workspaceViolations = collectWorkspaceViolations(cwd);
161-
const violations = [...collectPackageJsonViolations(cwd, gitTimeoutMs), ...workspaceViolations];
149+
const violations = [...collectPackageJsonViolations(cwd), ...workspaceViolations];
162150
return {
163151
packageManifestCount: packageJsonFiles.length,
164152
packageSpecCount,
@@ -197,7 +185,7 @@ export async function main() {
197185
if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
198186
main().catch(
199187
/** @param {unknown} error */ (error) => {
200-
console.error(error instanceof Error ? error.message : String(error));
188+
console.error(error);
201189
process.exit(1);
202190
},
203191
);

‎test/scripts/check-dependency-pins.test.ts‎

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { collectDependencyPinViolations } from "../../scripts/check-dependency-p
77
import { cleanupTempDirs, makeTempRepoRoot } from "../helpers/temp-repo.js";
88

99
const tempDirs: string[] = [];
10+
const itUnix = process.platform === "win32" ? it.skip : it;
1011

1112
const nestedGitEnvKeys = [
1213
"GIT_ALTERNATE_OBJECT_DIRECTORIES",
@@ -41,17 +42,21 @@ function writeJson(filePath: string, value: unknown) {
4142
writeFileSync(filePath, `${JSON.stringify(value, null, 2)}\n`, "utf8");
4243
}
4344

44-
function writeGitFixture(binDir: string, body: string): void {
45-
if (process.platform === "win32") {
46-
const fixturePath = path.join(binDir, "git-fixture.mjs");
47-
writeFileSync(fixturePath, body);
48-
writeFileSync(
49-
path.join(binDir, "git.cmd"),
50-
`@echo off\r\n"${process.execPath}" "${fixturePath}" %*\r\n`,
51-
);
52-
return;
53-
}
54-
writeFileSync(path.join(binDir, "git"), `#!${process.execPath}\n${body}`, { mode: 0o755 });
45+
function stubHangingGit(cwd: string, stalledSubcommand: "ls-files" | "show") {
46+
const binDir = path.join(cwd, "fake-git-bin");
47+
mkdirSync(binDir);
48+
const fixture = `#!${process.execPath}
49+
if (process.argv[2] === ${JSON.stringify(stalledSubcommand)}) {
50+
process.on("SIGTERM", () => {});
51+
setInterval(() => {}, 1_000);
52+
} else if (process.argv[2] === "ls-files") {
53+
process.stdout.write("package.json\\0");
54+
} else {
55+
process.exitCode = 1;
56+
}
57+
`;
58+
writeFileSync(path.join(binDir, "git"), fixture, { mode: 0o755 });
59+
vi.stubEnv("PATH", `${binDir}${path.delimiter}${process.env.PATH ?? ""}`);
5560
}
5661

5762
function makeRepo() {
@@ -175,21 +180,30 @@ packageExtensions:
175180
expect(collectDependencyPinViolations(dir)).toEqual([]);
176181
});
177182

178-
it("fails with an actionable timeout when git ls-files hangs", () => {
179-
const tempDir = makeTempRepoRoot(tempDirs, "openclaw-dependency-pins-timeout-");
180-
const binDir = path.join(tempDir, "bin");
181-
mkdirSync(binDir);
182-
writeGitFixture(
183-
binDir,
184-
'if (process.argv[2] === "ls-files") { setTimeout(() => {}, 10_000); }\nelse { process.exit(0); }\n',
185-
);
186-
vi.stubEnv("PATH", binDir);
183+
itUnix("terminates a SIGTERM-resistant git ls-files at the dependency-pin timeout", () => {
184+
const dir = makeTempRepoRoot(tempDirs, "openclaw-dependency-pins-ls-files-timeout-");
185+
stubHangingGit(dir, "ls-files");
187186

188-
expect(() => collectDependencyPinViolations(tempDir, { gitTimeoutMs: 500 })).toThrow(
189-
"dependency pin guard: git ls-files -z -- *package.json timed out after 500ms.",
187+
expect(() => collectDependencyPinViolations(dir, { gitTimeoutMs: 200 })).toThrow(
188+
"dependency pin guard: git ls-files -z -- *package.json timed out after 200ms.",
190189
);
191190
});
192191

192+
itUnix(
193+
"terminates a SIGTERM-resistant sparse-index git show at the dependency-pin timeout",
194+
() => {
195+
const dir = makeRepo();
196+
writeJson(path.join(dir, "package.json"), {});
197+
git(dir, ["add", "package.json"]);
198+
rmSync(path.join(dir, "package.json"));
199+
stubHangingGit(dir, "show");
200+
201+
expect(() => collectDependencyPinViolations(dir, { gitTimeoutMs: 200 })).toThrow(
202+
"dependency pin guard: git show :package.json timed out after 200ms.",
203+
);
204+
},
205+
);
206+
193207
it("rejects floating workspace overrides and package extension dependencies", () => {
194208
const dir = makeRepo();
195209
writeJson(path.join(dir, "package.json"), {});

0 commit comments

Comments
 (0)