@devries-cameron20 / t3code Mirror · read-only / commits / 1379dd0bc6
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 08:30 UTC

fix(server): keep awake blocks Windows sleep when the server runs in WSL (#38072)

Julius Marminge committed Nov 17, 2026 at 03:15 UTC 1379dd0bc6fd02191974afef60b5c4f42dec23bb
1 file changed +41 -6
apps/server/src/background/KeepAwake.ts
+41 -6
@@ -1,5 +1,6 @@
1 import * as Duration from "effect/Duration";
2 import * as Effect from "effect/Effect";
3 +import * as FileSystem from "effect/FileSystem";
4 import * as Layer from "effect/Layer";
5 import * as ChildProcess from "effect/process/ChildProcess";
6 import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
@@ -17,14 +18,38 @@ import * as Settings from "../serverSettings.ts";
18 // Bridges gaps between back-to-back runs, so a long-idle machine does not sleep mid-queue.
19 const RELEASE_GRACE = Duration.seconds(60);
20
21 +// ES_CONTINUOUS | ES_SYSTEM_REQUIRED, held by the calling thread until the script ends.
22 +const WINDOWS_HOLD_SCRIPT = `$t = Add-Type -Name Power -Namespace T3 -PassThru -MemberDefinition '[DllImport("kernel32.dll")] public static extern uint SetThreadExecutionState(uint f);'; [void]$t::SetThreadExecutionState([uint32]2147483649)`;
23 +
24 /** Each command holds its inhibitor until it exits, and exits on its own if the server dies. */
25 function inhibitorCommand(
26 platform: NodeJS.Platform,
27 serverPid: number,
24 -): { readonly command: string; readonly args: ReadonlyArray<string> } | undefined {
28 + isWsl: boolean,
29 +):
30 + | {
31 + readonly command: string;
32 + readonly args: ReadonlyArray<string>;
33 + readonly stdin: "ignore" | "pipe";
34 + }
35 + | undefined {
36 + // logind inside the WSL VM cannot block Windows sleep. Windows cannot wait on a Linux pid, so
37 + // the hold ends when its stdin pipe closes, which also happens if the server dies.
38 + if (isWsl) {
39 + return {
40 + command: "powershell.exe",
41 + args: [
42 + "-NoProfile",
43 + "-NonInteractive",
44 + "-Command",
45 + `${WINDOWS_HOLD_SCRIPT}; [void][Console]::In.ReadToEnd()`,
46 + ],
47 + stdin: "pipe",
48 + };
49 + }
50 switch (platform) {
51 case "darwin":
27 - return { command: "caffeinate", args: ["-i", "-w", String(serverPid)] };
52 + return { command: "caffeinate", args: ["-i", "-w", String(serverPid)], stdin: "ignore" };
53 case "linux":
54 return {
55 command: "systemd-inhibit",
@@ -38,6 +63,7 @@ function inhibitorCommand(
63 "-f",
64 "/dev/null",
65 ],
66 + stdin: "ignore",
67 };
68 case "win32":
69 return {
@@ -46,9 +72,9 @@ function inhibitorCommand(
72 "-NoProfile",
73 "-NonInteractive",
74 "-Command",
49 - // ES_CONTINUOUS | ES_SYSTEM_REQUIRED, held by this thread until Wait-Process returns.
50 - `$t = Add-Type -Name Power -Namespace T3 -PassThru -MemberDefinition '[DllImport("kernel32.dll")] public static extern uint SetThreadExecutionState(uint f);'; [void]$t::SetThreadExecutionState([uint32]2147483649); Wait-Process -Id ${serverPid}`,
75 + `${WINDOWS_HOLD_SCRIPT}; Wait-Process -Id ${serverPid}`,
76 ],
77 + stdin: "ignore",
78 };
79 default:
80 return undefined;
@@ -96,7 +122,16 @@ class KeepAwakeInhibitorExited extends Schema.TaggedError<KeepAwakeInhibitorExit
122
123 export const layer = Layer.effectDiscard(
124 Effect.gen(function* () {
99 - const command = inhibitorCommand(yield* HostProcess.Platform, process.pid);
125 + const platform = yield* HostProcess.Platform;
126 + const fileSystem = yield* FileSystem.FileSystem;
127 + // Services started by systemd lack WSL_DISTRO_NAME and WSL_INTEROP; the kernel release does not.
128 + const isWsl =
129 + platform === "linux" &&
130 + (yield* fileSystem.readFileString("/proc/sys/kernel/osrelease").pipe(
131 + Effect.map((release) => release.toLowerCase().includes("microsoft")),
132 + Effect.orElseSucceed(() => false),
133 + ));
134 + const command = inhibitorCommand(platform, process.pid, isWsl);
135 if (command === undefined) return;
136 const settings = yield* Settings.ServerSettingsService;
137 const events = yield* OrchestrationEventStore.OrchestrationEventStore;
@@ -158,7 +193,7 @@ export const layer = Layer.effectDiscard(
193 ChildProcess.make(command.command, command.args, {
194 detached: false,
195 shell: false,
161 - stdin: "ignore",
196 + stdin: command.stdin,
197 stdout: "ignore",
198 stderr: "ignore",
199 }),