Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Windows: a reused pid no longer holds the data-directory lease when wmic is missing (#4071)

Suhania5be3e504a1f
diff --git a/electron/data-dir-lease.mjs b/electron/data-dir-lease.mjs
index d2757bc2e9..14a3978e4e 100644
--- a/electron/data-dir-lease.mjs
+++ b/electron/data-dir-lease.mjs
@@ -238,8 +238,13 @@ function parseWmiDateTime(value) {
return ms - offsetMinutes * 60 * 1000;
}
+/** Windows: when a process started, in ms since the epoch; null when it has exited or neither probe can read it. */
function processCreationTimeMs(pid) {
if (process.platform !== "win32") return null;
+ return wmicCreationTimeMs(pid) ?? cimCreationTimeMs(pid);
+}
+
+function wmicCreationTimeMs(pid) {
try {
const output = execFileSync(
"wmic",
@@ -261,6 +266,28 @@ function processCreationTimeMs(pid) {
return null;
}
+/**
+ * Windows 11 no longer installs wmic, and without a creation time every reused pid reads as the lease's owner, so
+ * the lease can never be recovered. Windows PowerShell ships with every Windows; named by full path, never via PATH.
+ */
+function cimCreationTimeMs(pid) {
+ const powershell = join(process.env.SystemRoot || "C:\\Windows", "System32", "WindowsPowerShell", "v1.0", "powershell.exe");
+ try {
+ const output = execFileSync(
+ powershell,
+ [
+ "-NoLogo", "-NoProfile", "-NonInteractive", "-Command",
+ `$p = Get-CimInstance Win32_Process -Filter 'ProcessId=${pid}'; if ($p) { ([DateTimeOffset]$p.CreationDate).ToUnixTimeMilliseconds() }`,
+ ],
+ { encoding: "utf8", timeout: 15_000, windowsHide: true, stdio: ["ignore", "pipe", "ignore"] },
+ );
+ const ms = Number(output.trim());
+ return output.trim() && Number.isSafeInteger(ms) && ms > 0 ? ms : null;
+ } catch {
+ return null;
+ }
+}
+
/** Pids are reused within a boot: a process that started at another moment, or after the lease, is not its owner. */
function ownerIdentityMatches(owner) {
if (process.platform === "linux") {
diff --git a/electron/data-dir-lease.node-test.mjs b/electron/data-dir-lease.node-test.mjs
index 4a4c07f09f..b35616e3b1 100644
--- a/electron/data-dir-lease.node-test.mjs
+++ b/electron/data-dir-lease.node-test.mjs
@@ -1,5 +1,5 @@
import assert from "node:assert/strict";
-import { execFileSync, spawn } from "node:child_process";
+import { spawn } from "node:child_process";
import { createHash, randomUUID } from "node:crypto";
import { mkdirSync, mkdtempSync, readFileSync, readlinkSync, rmSync, writeFileSync } from "node:fs";
import { hostname, tmpdir, uptime as osUptime } from "node:os";
@@ -60,16 +60,6 @@ async function exitedPid() {
return pid;
}
-function wmicAvailable() {
- if (process.platform !== "win32") return false;
- try {
- execFileSync("wmic", ["/?"], { encoding: "utf8", timeout: 5_000, stdio: ["ignore", "pipe", "ignore"] });
- return true;
- } catch {
- return false;
- }
-}
-
test.afterEach(() => {
for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true });
});
@@ -662,8 +652,8 @@ test("boot identity does not weaken exclusion: only one of many live racers wins
});
test("a live but unrelated Windows pid reused within the same boot is treated as stale", async (t) => {
- if (process.platform !== "win32") return t.skip("Windows-only: wmic process-identity check");
- if (!wmicAvailable()) return t.skip("wmic is not available; the Windows process-identity check cannot be exercised");
+ // With or without wmic: Windows 11 no longer installs it, and the check must still tell a reused pid apart.
+ if (process.platform !== "win32") return t.skip("Windows-only: process-identity check");
const { dataDir } = temporaryDirectory();
const sibling = spawn(process.execPath, ["--eval", "setInterval(()=>{}, 1_000);"], { stdio: "ignore" });
const siblingPid = sibling.pid;