Skip to content
TypeScript

test(desktop): Keep WSL busy-runtime fixtures visible when sh is bash (#14351)

Mike Olson38969148a23e
diff --git a/apps/desktop/src/wsl/DesktopWslEnvironment.test.ts b/apps/desktop/src/wsl/DesktopWslEnvironment.test.ts
index e28a9c6c780..e261524f9ac 100644
--- a/apps/desktop/src/wsl/DesktopWslEnvironment.test.ts
+++ b/apps/desktop/src/wsl/DesktopWslEnvironment.test.ts
@@ -69,6 +69,28 @@ const runShell = (script: string) => {
const sh = (value: string) => `'${value.replaceAll("'", "'\\''")}'`;
+// Polls for up to five seconds until a background holder has taken effect.
+const awaitHolder = (condition: string) =>
+ `i=0; until ${condition}; do i=$((i + 1)); [ "$i" -lt 500 ] || exit 1; sleep 0.01; done`;
+
+// Holds a cache busy the way a running server does: the holder's argv[0] is the
+// runtime entry, which the prune and install scripts look for in /proc cmdline.
+// It reads a pipe the script keeps open on fd 7, so it exits with the script
+// instead of outliving it on a timer.
+const holdRuntimeBusy = (entry: string) =>
+ [
+ `exec 7> >(exec -a ${entry} cat >/dev/null 2>&1)`,
+ awaitHolder(`grep -qF -- ${entry} /proc/[0-9]*/cmdline 2>/dev/null`),
+ ].join("\n");
+
+// Holds a cache's install lock the way a concurrent install does, and likewise
+// releases it when the script exits.
+const holdInstallLock = (lock: string) =>
+ [
+ `exec 6> >(exec 9> ${lock}; flock -x 9; cat >/dev/null 2>&1)`,
+ awaitHolder(`! flock -n ${lock} true`),
+ ].join("\n");
+
const readField = (stdout: string, field: string) => {
const line = stdout.split("\n").find((candidate) => candidate.startsWith(`${field}:`));
if (line === undefined) throw new Error(`missing ${field} in fixture output: ${stdout}`);
@@ -712,9 +734,7 @@ describe.skipIf(posixShellRunner === null)("WSL runtime install script (executed
`runtime_root=${sh(fixture.runtimeRoot)}`,
`runtime_parent=${sh(fixture.runtimeParent)}`,
'rm "$runtime_root/.t3code-wsl-runtime-ready"',
- 'sh -c "sleep 30" "$runtime_root/t3" >/dev/null 2>&1 &',
- "active_pid=$!",
- "sleep 0.1",
+ holdRuntimeBusy('"$runtime_root/t3"'),
fixture.installScript(),
'stale=$(find "$runtime_parent" -maxdepth 1 -type d -name ".sha256-*.stale.*" -print -quit)',
'test -n "$stale"',
@@ -723,8 +743,6 @@ describe.skipIf(posixShellRunner === null)("WSL runtime install script (executed
"export HOME",
buildWslRuntimePruneScript(fixture.runtimeId),
'test ! -e "$stale"',
- "kill $active_pid",
- "wait $active_pid 2>/dev/null || true",
].join("\n"),
);
@@ -750,15 +768,8 @@ describe.skipIf(posixShellRunner === null)("WSL runtime install script (executed
'touch -d "4 minutes ago" "$runtime_parent/sha256-active"',
'touch -d "3 minutes ago" "$runtime_parent/sha256-old"',
'touch -d "2 minutes ago" "$runtime_parent/sha256-locked"',
- 'sh -c "sleep 30" "$runtime_parent/sha256-active/t3" >/dev/null 2>&1 &',
- "active_pid=$!",
- "(",
- ' exec 9> "$runtime_parent/.sha256-locked.install.lock"',
- " flock -x 9",
- " sleep 30",
- ") >/dev/null 2>&1 &",
- "lock_pid=$!",
- "sleep 0.1",
+ holdRuntimeBusy('"$runtime_parent/sha256-active/t3"'),
+ holdInstallLock('"$runtime_parent/.sha256-locked.install.lock"'),
`HOME="$home"`,
"export HOME",
buildWslRuntimePruneScript("sha256-current"),
@@ -769,9 +780,6 @@ describe.skipIf(posixShellRunner === null)("WSL runtime install script (executed
'test -d "$runtime_parent/versions"',
'test ! -e "$runtime_parent/sha256-old"',
'test ! -e "$runtime_parent/sha256-markerless"',
- "kill $active_pid $lock_pid",
- "wait $active_pid 2>/dev/null || true",
- "wait $lock_pid 2>/dev/null || true",
'rm -rf "$work"',
].join("\n"),
);