From 98704378fd83e0d0ae689dcf1d0d967f91d1571f Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Tue, 17 Nov 2026 14:27:48 +0100 Subject: [PATCH 1/3] =?UTF-8?q?test:=20reproduce=20#1662=20=E2=80=94=20emu?= =?UTF-8?q?lator.log=20is=20replaced=20at=20each=20start=20instead=20of=20?= =?UTF-8?q?emptied=20in=20place?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/drivers/android/index-boot.test.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/drivers/android/index-boot.test.ts b/src/drivers/android/index-boot.test.ts index 98e362c16..53e7411e9 100644 --- a/src/drivers/android/index-boot.test.ts +++ b/src/drivers/android/index-boot.test.ts @@ -542,12 +542,13 @@ describe("AndroidDriver", () => { }); }); - it("sends the emulator's output to /emulator.log, mode 0600, truncated at each start", async () => { + it("sends the emulator's output to /emulator.log, mode 0600, emptied in place at each start", async () => { const harness = await provisionedHarness(); const logPath = `${avdDirectory}/simlock_one/simlock.avd/emulator.log`; await harness.filesystem.writeFileAtomic(logPath, "left by an earlier start\n", { mode: 0o644, }); + const earlier = await harness.filesystem.stat(logPath); await harness.driver.makeReady(harness.device); @@ -557,6 +558,10 @@ describe("AndroidDriver", () => { expect(launch?.options.stdio).toEqual({ file: logPath }); await expect(harness.filesystem.readFile(logPath)).resolves.toBe(""); expect((await harness.filesystem.lstat(logPath)).mode & 0o777).toBe(0o600); + expect( + (await harness.filesystem.stat(logPath)).identity, + "emulator.log was replaced by another file, not emptied", + ).toBe(earlier.identity); }); it("an Android boot waits android.bootTimeoutMs, then kills the emulator and fails with BootTimeoutError", async () => { From 1c23252e7210aaebf9b446a4915b04ba82bc3e8a Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Tue, 17 Nov 2026 14:30:43 +0100 Subject: [PATCH 2/3] fix(android): empty the emulator log in place so Windows can start a second boot (#1662) --- src/auth/admin-secret.test.ts | 6 ++++++ src/drivers/android/emulator.ts | 2 +- src/ports/AGENTS.md | 2 ++ src/ports/filesystem.test.ts | 17 +++++++++++++++ src/ports/filesystem.ts | 38 +++++++++++++++++++++++++++++++++ 5 files changed, 64 insertions(+), 1 deletion(-) diff --git a/src/auth/admin-secret.test.ts b/src/auth/admin-secret.test.ts index 84272bb3b..7c9cd8e46 100644 --- a/src/auth/admin-secret.test.ts +++ b/src/auth/admin-secret.test.ts @@ -66,6 +66,9 @@ describe("AdminSecretManager", () => { writeFileAtomic: () => { throw new Error("verify() must not touch the filesystem"); }, + writeFileInPlace: () => { + throw new Error("must not touch the filesystem"); + }, copyFile: () => { throw new Error("verify() must not touch the filesystem"); }, @@ -162,6 +165,9 @@ describe("AdminSecretManager", () => { writeFileAtomic: () => { throw new Error("must not touch the filesystem"); }, + writeFileInPlace: () => { + throw new Error("must not touch the filesystem"); + }, copyFile: () => { throw new Error("verify() must not touch the filesystem"); }, diff --git a/src/drivers/android/emulator.ts b/src/drivers/android/emulator.ts index c1b5ff5f3..86b0351e7 100644 --- a/src/drivers/android/emulator.ts +++ b/src/drivers/android/emulator.ts @@ -248,7 +248,7 @@ export class Emulator { ): Promise { const startedAt = this.#options.clock.now(); const logPath = this.#logPath(data); - await this.#options.filesystem.writeFileAtomic(logPath, "", { mode: 0o600 }); + await this.#options.filesystem.writeFileInPlace(logPath, "", { mode: 0o600 }); // why: the output goes to a file so a launch that dies at once leaves its own error line to quote (#1199); `unref()` because an emulator outlives the daemon by design // (`AndroidDriver` adopts it after a restart) and is reaped through adb and // by pid, never by awaiting its exit. Left referenced, its handle and stdio pipes would hold diff --git a/src/ports/AGENTS.md b/src/ports/AGENTS.md index 4b41b3e94..f300495ab 100644 --- a/src/ports/AGENTS.md +++ b/src/ports/AGENTS.md @@ -47,6 +47,8 @@ Windows: directory. Ask `isPipeEndpoint` first. - No directory guards a pipe. The default pipe ACL lets other accounts only read, and a client needs read and write. A second listener fails `address-in-use`. +- A rename over a file another process holds open fails `EPERM`. `writeFileInPlace` empties such + a file (the emulator's log, #1662) in the same file; `writeFileAtomic` is for the rest. - Every child an adapter starts sets `windowsHide`: the daemon has no console, so a console program would open a window. - A `.bat` or `.cmd` runs through `cmd.exe` (`windows-command.ts`), and an argument holding `%`, diff --git a/src/ports/filesystem.test.ts b/src/ports/filesystem.test.ts index 932f08414..4d2e6e6c9 100644 --- a/src/ports/filesystem.test.ts +++ b/src/ports/filesystem.test.ts @@ -59,6 +59,23 @@ describe.each(implementations)("Filesystem contract: $name", ({ create, link }) ]); }); + it("writes a file in place: the same file, emptied or filled, ending at the mode it is given", async () => { + const filesystem = create(); + const file = `${temporaryDirectory}/emulator.log`; + + await filesystem.mkdirp(temporaryDirectory); + await filesystem.writeFileAtomic(file, "old output", { mode: 0o644 }); + const before = (await filesystem.stat(file)).identity; + await filesystem.writeFileInPlace(file, "", { mode: 0o600 }); + + await expect(filesystem.readFile(file)).resolves.toBe(""); + expect((await filesystem.stat(file)).identity).toBe(before); + expect((await filesystem.stat(file)).mode).toBe(0o600); + + await filesystem.writeFileInPlace(`${temporaryDirectory}/new.log`, "text", { mode: 0o600 }); + await expect(filesystem.readFile(`${temporaryDirectory}/new.log`)).resolves.toBe("text"); + }); + it("copies a file's contents over the target and leaves the source", async () => { const filesystem = create(); await filesystem.mkdirp(temporaryDirectory); diff --git a/src/ports/filesystem.ts b/src/ports/filesystem.ts index b5046c63c..2cc36f8f8 100644 --- a/src/ports/filesystem.ts +++ b/src/ports/filesystem.ts @@ -82,6 +82,16 @@ export interface Filesystem { readFile(path: string): Promise; readFileFrom(path: string, offset: number): Promise; writeFileAtomic(path: string, contents: string, options?: WriteFileAtomicOptions): Promise; + /** + * why: replaces a file's contents in the same file (same identity), then sets its mode, so an + * existing 0644 file ends at `mode`. `writeFileAtomic` renames over the path, which Windows + * refuses with `EPERM` while another process holds the file open (#1662). + */ + writeFileInPlace( + path: string, + contents: string, + options: { readonly mode: number }, + ): Promise; /** * why: Writes a file only when nothing is there, failing `EEXIST` otherwise. The kernel * decides who wins, which is the only way two processes racing to write one file both @@ -283,6 +293,15 @@ export class NodeFilesystem implements Filesystem { } } + async writeFileInPlace( + path: string, + contents: string, + options: { readonly mode: number }, + ): Promise { + await writeFile(path, contents, { encoding: "utf8", mode: options.mode }); + await chmod(path, options.mode); + } + async writeFileExclusive( path: string, contents: string, @@ -592,6 +611,25 @@ export class MemoryFilesystem implements Filesystem { }); } + async writeFileInPlace( + path: string, + contents: string, + options: { readonly mode: number }, + ): Promise { + this.#failIfDefined(path); + path = this.#underResolvedParent(path); + const existing = this.#entries.get(path); + + if (existing?.kind === "file") { + existing.contents = contents; + delete existing.bytes; + existing.mode = options.mode & 0o777; + return; + } + + await this.writeFileAtomic(path, contents, options); + } + async writeFileExclusive( path: string, contents: string, From d9ac2e1724ebc2bcbbb4d62e90ff498164520857 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Tue, 17 Nov 2026 14:51:26 +0100 Subject: [PATCH 3/3] docs(ports): drop the manual line that took the manuals over their size cap (#1662) --- src/ports/AGENTS.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/ports/AGENTS.md b/src/ports/AGENTS.md index f300495ab..4b41b3e94 100644 --- a/src/ports/AGENTS.md +++ b/src/ports/AGENTS.md @@ -47,8 +47,6 @@ Windows: directory. Ask `isPipeEndpoint` first. - No directory guards a pipe. The default pipe ACL lets other accounts only read, and a client needs read and write. A second listener fails `address-in-use`. -- A rename over a file another process holds open fails `EPERM`. `writeFileInPlace` empties such - a file (the emulator's log, #1662) in the same file; `writeFileAtomic` is for the rest. - Every child an adapter starts sets `windowsHide`: the daemon has no console, so a console program would open a window. - A `.bat` or `.cmd` runs through `cmd.exe` (`windows-command.ts`), and an argument holding `%`,