@devries-cameron20 / t3code-1 / commits / 9494dbaff6
Fork 0

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

fix(shared): Windows saves no longer fail while a scanner holds the file (#21062)

Co-authored-by: yaacovcorcos <190350601+yaacovcorcos@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Dara Adedeji committed Oct 18, 2026 at 16:02 UTC 9494dbaff62e99f810259e154ceda612cf52e1ea
3 files changed +219 -9
packages/shared/src/atomicWrite.test.ts
+165
@@ -1,13 +1,17 @@
1 import * as NodeServices from "@effect/platform-node/NodeServices";
2 import { assert, it } from "@effect/vitest";
3 +import * as Deferred from "effect/Deferred";
4 import * as Effect from "effect/Effect";
5 import * as Exit from "effect/Exit";
6 +import * as Fiber from "effect/Fiber";
7 import * as FileSystem from "effect/FileSystem";
8 import * as Layer from "effect/Layer";
9 import * as Path from "effect/Path";
10 import * as PlatformError from "effect/PlatformError";
11 +import * as TestClock from "effect/testing/TestClock";
12
13 import { writeFileStringAtomically } from "./atomicWrite.ts";
14 +import * as HostProcess from "./HostProcess.ts";
15
16 it.layer(NodeServices.layer)("writeFileStringAtomically", (it) => {
17 it.effect("keeps a symlinked file linked and rewrites its destination", () =>
@@ -101,8 +105,169 @@ it.layer(NodeServices.layer)("writeFileStringAtomically", (it) => {
105 assert.strictEqual(yield* fs.readFileString(filePath), "fresh");
106 }),
107 );
108 +
109 + it.effect("waits out a Windows scanner holding the target, then replaces it", () =>
110 + Effect.gen(function* () {
111 + const fs = yield* FileSystem.FileSystem;
112 + const path = yield* Path.Path;
113 + const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-atomic-write-" });
114 + const filePath = path.join(root, "settings.json");
115 + yield* fs.writeFileString(filePath, "before");
116 + const firstAttempt = yield* Deferred.make<void>();
117 + let attempts = 0;
118 + const lockedTwice = FileSystem.make({
119 + ...fs,
120 + rename: (from, to) =>
121 + Effect.suspend(() => {
122 + attempts += 1;
123 + return attempts <= 2
124 + ? Deferred.succeed(firstAttempt, undefined).pipe(
125 + Effect.andThen(Effect.fail(renameError("EBUSY", to))),
126 + )
127 + : fs.rename(from, to);
128 + }),
129 + });
130 +
131 + const writing = yield* writeFileStringAtomically({ filePath, contents: "after" }).pipe(
132 + Effect.provideService(FileSystem.FileSystem, lockedTwice),
133 + Effect.provideService(HostProcess.Platform, "win32"),
134 + Effect.forkChild,
135 + );
136 + yield* Deferred.await(firstAttempt);
137 + yield* advanceUntilDone(writing);
138 +
139 + assert.strictEqual(attempts, 3);
140 + assert.strictEqual(yield* fs.readFileString(filePath), "after");
141 + assert.deepStrictEqual(yield* fs.readDirectory(root), ["settings.json"]);
142 + }),
143 + );
144 +
145 + it.effect("gives up on a Windows lock that does not clear and keeps the old contents", () =>
146 + Effect.gen(function* () {
147 + const fs = yield* FileSystem.FileSystem;
148 + const path = yield* Path.Path;
149 + const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-atomic-write-" });
150 + const filePath = path.join(root, "settings.json");
151 + yield* fs.writeFileString(filePath, "before");
152 + const firstAttempt = yield* Deferred.make<void>();
153 + let attempts = 0;
154 + const locked = FileSystem.make({
155 + ...fs,
156 + rename: (_from, to) =>
157 + Effect.suspend(() => {
158 + attempts += 1;
159 + return Deferred.succeed(firstAttempt, undefined).pipe(
160 + Effect.andThen(Effect.fail(renameError("EPERM", to))),
161 + );
162 + }),
163 + });
164 +
165 + const writing = yield* writeFileStringAtomically({ filePath, contents: "after" }).pipe(
166 + Effect.provideService(FileSystem.FileSystem, locked),
167 + Effect.provideService(HostProcess.Platform, "win32"),
168 + Effect.exit,
169 + Effect.forkChild,
170 + );
171 + yield* Deferred.await(firstAttempt);
172 +
173 + assert.isTrue(Exit.isFailure(yield* advanceUntilDone(writing)));
174 + assert.isAbove(attempts, 1);
175 + assert.strictEqual(yield* fs.readFileString(filePath), "before");
176 + assert.deepStrictEqual(yield* fs.readDirectory(root), ["settings.json"]);
177 + }),
178 + );
179 +
180 + it.effect("does not replace a newer save that landed while the lock was held", () =>
181 + Effect.gen(function* () {
182 + const fs = yield* FileSystem.FileSystem;
183 + const path = yield* Path.Path;
184 + const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-atomic-write-" });
185 + const filePath = path.join(root, "settings.json");
186 + yield* fs.writeFileString(filePath, "before");
187 + const firstAttempt = yield* Deferred.make<void>();
188 + let attempts = 0;
189 + const lockedOnce = FileSystem.make({
190 + ...fs,
191 + rename: (from, to) =>
192 + Effect.suspend(() => {
193 + attempts += 1;
194 + return attempts === 1
195 + ? Deferred.succeed(firstAttempt, undefined).pipe(
196 + Effect.andThen(Effect.fail(renameError("EBUSY", to))),
197 + )
198 + : fs.rename(from, to);
199 + }),
200 + });
201 +
202 + const stale = yield* writeFileStringAtomically({ filePath, contents: "stale" }).pipe(
203 + Effect.provideService(FileSystem.FileSystem, lockedOnce),
204 + Effect.provideService(HostProcess.Platform, "win32"),
205 + Effect.exit,
206 + Effect.forkChild,
207 + );
208 + yield* Deferred.await(firstAttempt);
209 + // Another process saves once the lock clears, before the waiting write retries.
210 + yield* writeFileStringAtomically({ filePath, contents: "newer" });
211 +
212 + assert.isTrue(Exit.isFailure(yield* advanceUntilDone(stale)));
213 + assert.strictEqual(yield* fs.readFileString(filePath), "newer");
214 + assert.deepStrictEqual(yield* fs.readDirectory(root), ["settings.json"]);
215 + }),
216 + );
217 +
218 + it.effect("fails at once on a read-only Windows target", () =>
219 + Effect.gen(function* () {
220 + const fs = yield* FileSystem.FileSystem;
221 + const path = yield* Path.Path;
222 + const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-atomic-write-" });
223 + const filePath = path.join(root, "settings.json");
224 + yield* fs.writeFileString(filePath, "before");
225 + yield* Effect.acquireRelease(fs.chmod(filePath, 0o444), () =>
226 + fs.chmod(filePath, 0o644).pipe(Effect.ignore),
227 + );
228 + let attempts = 0;
229 + const denied = FileSystem.make({
230 + ...fs,
231 + rename: (_from, to) =>
232 + Effect.suspend(() => {
233 + attempts += 1;
234 + return Effect.fail(renameError("EPERM", to));
235 + }),
236 + });
237 +
238 + const writing = yield* writeFileStringAtomically({ filePath, contents: "after" }).pipe(
239 + Effect.provideService(FileSystem.FileSystem, denied),
240 + Effect.provideService(HostProcess.Platform, "win32"),
241 + Effect.exit,
242 + Effect.forkChild,
243 + );
244 + assert.isTrue(Exit.isFailure(yield* advanceUntilDone(writing)));
245 + assert.strictEqual(attempts, 1);
246 + assert.strictEqual(yield* fs.readFileString(filePath), "before");
247 + }),
248 + );
249 });
250
251 +// Retries stat the real target between waits, and that I/O finishes outside the
252 +// test clock, so the clock moves one wait at a time until the write settles.
253 +const advanceUntilDone = <A, E>(fiber: Fiber.Fiber<A, E>) =>
254 + Effect.gen(function* () {
255 + while (fiber.pollUnsafe() === undefined) {
256 + yield* TestClock.adjust("100 millis");
257 + yield* Effect.promise(() => new Promise((resolve) => setImmediate(resolve)));
258 + }
259 + return yield* Fiber.join(fiber);
260 + });
261 +
262 +const renameError = (code: string, path: string) =>
263 + PlatformError.systemError({
264 + _tag: "Unknown",
265 + module: "FileSystem",
266 + method: "rename",
267 + pathOrDescriptor: path,
268 + cause: Object.assign(new Error(`${code}, rename`), { code }),
269 + });
270 +
271 it.effect("surfaces an unreadable link instead of writing over it", () =>
272 Effect.gen(function* () {
273 const readLinkFailure = PlatformError.systemError({
packages/shared/src/atomicWrite.ts
+52 -1
@@ -1,8 +1,27 @@
1 import * as Effect from "effect/Effect";
2 import * as FileSystem from "effect/FileSystem";
3 +import * as Option from "effect/Option";
4 import * as Path from "effect/Path";
5 +import type * as PlatformError from "effect/PlatformError";
6 +import * as Result from "effect/Result";
7 +import * as HostProcess from "@t3tools/shared/HostProcess";
8 import { resolveSymlinkTarget } from "@t3tools/shared/symlink";
9
10 +/**
11 + * Windows refuses to rename over or away from a file another process holds open
12 + * without delete sharing, such as a virus scanner or the search indexer. The same
13 + * codes also mean a denial that never clears, so retries need a bound.
14 + */
15 +export const isWindowsRenameLock = (error: PlatformError.PlatformError) => {
16 + const code = (error.reason.cause as NodeJS.ErrnoException | undefined)?.code;
17 + return code === "EBUSY" || code === "EPERM" || code === "EACCES";
18 +};
19 +
20 +// About 2 seconds in total. Settings and keybinding saves wait on this while holding
21 +// their write lock, so a denial that is not a brief scan has to surface quickly.
22 +const LOCKED_RENAME_RETRIES = 20;
23 +const LOCKED_RENAME_RETRY_DELAY = "100 millis";
24 +
25 /**
26 * A symlinked target is resolved first so the link survives and its destination is
27 * rewritten, since renaming over the link itself would swap it for a regular file.
@@ -15,6 +34,7 @@ export const writeFileStringAtomically = (input: {
34 Effect.gen(function* () {
35 const fs = yield* FileSystem.FileSystem;
36 const path = yield* Path.Path;
37 + const platform = yield* HostProcess.Platform;
38 const targetPath = yield* resolveSymlinkTarget(input.filePath);
39 const targetDirectory = path.dirname(targetPath);
40
@@ -32,6 +52,37 @@ export const writeFileStringAtomically = (input: {
52 const tempPath = path.join(tempDirectory, "contents.tmp");
53
54 yield* fs.writeFileString(tempPath, input.contents);
35 - yield* fs.rename(tempPath, targetPath);
55 + if (platform !== "win32") return yield* fs.rename(tempPath, targetPath);
56 +
57 + // A retry must not replace a save another process landed while this one
58 + // waited, so it gives up once the target is no longer the file seen here.
59 + // A read-only target fails with the same EPERM as a held one but never clears.
60 + const targetVersion = fs.stat(targetPath).pipe(
61 + Effect.map((info) => ({
62 + id: `${Option.getOrUndefined(info.ino)}:${info.size}:${Option.getOrUndefined(info.mtime)?.getTime()}`,
63 + readOnly: (info.mode & 0o200) === 0,
64 + })),
65 + Effect.catchIf(
66 + (error) => error.reason._tag === "NotFound",
67 + () => Effect.succeed({ id: "missing", readOnly: false }),
68 + ),
69 + Effect.option,
70 + );
71 + const before = yield* targetVersion;
72 + const firstAttempt = yield* Effect.result(fs.rename(tempPath, targetPath));
73 + if (Result.isSuccess(firstAttempt)) return;
74 + const lockError = firstAttempt.failure;
75 + if (!isWindowsRenameLock(lockError) || Option.isNone(before) || before.value.readOnly) {
76 + return yield* lockError;
77 + }
78 + for (let retry = 0; retry < LOCKED_RENAME_RETRIES; retry += 1) {
79 + yield* Effect.sleep(LOCKED_RENAME_RETRY_DELAY);
80 + const current = yield* targetVersion;
81 + if (Option.isNone(current) || current.value.id !== before.value.id) break;
82 + const attempt = yield* Effect.result(fs.rename(tempPath, targetPath));
83 + if (Result.isSuccess(attempt)) return;
84 + if (!isWindowsRenameLock(attempt.failure)) return yield* attempt.failure;
85 + }
86 + return yield* lockError;
87 }),
88 );
packages/shared/src/relayClient.ts
+2 -8
@@ -21,6 +21,7 @@ import * as Schedule from "effect/Schedule";
21 import * as Semaphore from "effect/Semaphore";
22 import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http";
23 import { ChildProcess, ChildProcessSpawner } from "effect/process";
24 +import { isWindowsRenameLock } from "./atomicWrite.ts";
25 import * as HostProcess from "./HostProcess.ts";
26
27 export const CLOUDFLARED_VERSION = "2026.10.0";
@@ -199,13 +200,6 @@ function isAlreadyExists(error: PlatformError.PlatformError): boolean {
200 return error.reason._tag === "AlreadyExists";
201 }
202
202 -// Windows refuses to rename a file another process briefly holds open, such as a virus
203 -// scanner reading the binary that just ran. These codes clear once it lets go.
204 -function isTransientWindowsLock(error: PlatformError.PlatformError): boolean {
205 - const code = (error.reason.cause as NodeJS.ErrnoException | undefined)?.code;
206 - return code === "EBUSY" || code === "EPERM" || code === "EACCES";
207 -}
208 -
203 const wrapInstallFailure =
204 (
205 reason: RelayClientInstallError["reason"],
@@ -400,7 +394,7 @@ export const makeCloudflaredRelayClient = Effect.fn("cloudflared.make")(function
394 Effect.retry({
395 times: ACTIVATE_RETRY_COUNT,
396 schedule: Schedule.spaced(ACTIVATE_RETRY_DELAY),
403 - while: (error) => platform === "win32" && isTransientWindowsLock(error),
397 + while: (error) => platform === "win32" && isWindowsRenameLock(error),
398 }),
399 );
400