Skip to content

Commit be292a0

Browse files
merge(harden/407): keep audio recordings when permission denies fsync
origin/harden/407-audio-fsync-permission e927412 is one commit that is not on main. Since Node 26.9.0, FileHandle.sync() throws ERR_ACCESS_DENIED whenever the Permission Model is on (nodejs/node#65431). AudioRecorder treated that as a finalize failure, so every recording under --permission was discarded. Finalize now catches only ERR_ACCESS_DENIED from sync() and still closes the handle. Any other sync error, including the injected EIO case, still fails finalize, removes the temp file, and leaves the destination untouched. The denied path stops cleanly, writes the PCM16 WAV, and leaves the fd at -1. The audio-capture doc says that on Node.js 26.9.0 or later with --permission the file is not forced to disk before the rename. Issue 407. Merge-tree 690ba0c is clean, 0 path deletions, 3 files changed, 21 insertions(+), 10 deletions(-) across 3 files. Files: packages/core/src/audio.ts, packages/core/src/tests/audio.test.ts, packages/web/src/content/docs/application-apis/audio-capture.mdx. The OpenAI File Downloader user-agent lines stay in AGENTS.md and packages/web/AGENTS.md. The harden ref is left in place. anomalyco/opentui is not pushed. Co-Authored-By: Claude <[email protected]>
2 parents 6a40eb4 + e927412 commit be292a0

3 files changed

Lines changed: 21 additions & 10 deletions

File tree

‎packages/core/src/audio.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2519,7 +2519,10 @@ export class AudioRecorder extends EventEmitter<AudioRecorderEvents> {
25192519
"finalize",
25202520
)
25212521
if (this.terminationRequest != null) return
2522-
await this.fileHandle!.sync()
2522+
// Node 26.9+ denies FileHandle.sync() under the Permission Model (nodejs/node#65431); close without it.
2523+
await this.fileHandle!.sync().catch((cause: unknown) => {
2524+
if ((cause as NodeJS.ErrnoException).code !== "ERR_ACCESS_DENIED") throw cause
2525+
})
25232526
if (this.terminationRequest != null) return
25242527
await this.fileHandle!.close()
25252528
this.fileHandle = null

‎packages/core/src/tests/audio.test.ts‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2104,7 +2104,7 @@ test("AudioRecorder stop drains every buffered frame while a file write is delay
21042104
})
21052105

21062106
test("AudioRecorder final header and sync failures remove the temp and preserve the destination", async () => {
2107-
for (const mode of ["header", "sync"] as const) {
2107+
for (const mode of ["header", "sync", "sync-denied"] as const) {
21082108
const ring = replaceCaptureRing([0.25, -0.25])
21092109
const directory = await createRecorderTempDirectory()
21102110
const filePath = join(directory, `${mode}.wav`)
@@ -2113,18 +2113,21 @@ test("AudioRecorder final header and sync failures remove the temp and preserve
21132113
const probe = await openFile(probePath, "w")
21142114
const fileHandlePrototype = Object.getPrototypeOf(probe) as object
21152115
const originalWrite = (fileHandlePrototype as { write: (...args: unknown[]) => Promise<unknown> }).write
2116-
const originalSync = (fileHandlePrototype as { sync(): Promise<void> }).sync
21172116
await probe.close()
21182117
await unlinkFile(probePath)
21192118
let headerWrites = 0
2119+
let handle: { fd: number } | undefined
21202120
const restore =
21212121
mode === "header"
2122-
? replaceMethod(fileHandlePrototype, "write", function (this: unknown, ...args: unknown[]) {
2122+
? replaceMethod(fileHandlePrototype, "write", function (this: { fd: number }, ...args: unknown[]) {
2123+
handle = this
21232124
if (args[3] === 0 && ++headerWrites === 2) return Promise.reject(new Error("injected header failure"))
21242125
return originalWrite.apply(this, args)
21252126
})
2126-
: replaceMethod(fileHandlePrototype, "sync", function (this: unknown) {
2127-
return Promise.reject(new Error("injected sync failure"))
2127+
: replaceMethod(fileHandlePrototype, "sync", function (this: { fd: number }) {
2128+
handle = this
2129+
const code = mode === "sync" ? "EIO" : "ERR_ACCESS_DENIED"
2130+
return Promise.reject(Object.assign(new Error("injected sync failure"), { code }))
21282131
})
21292132
try {
21302133
const audio = Audio.create({ autoStart: false })
@@ -2134,13 +2137,16 @@ test("AudioRecorder final header and sync failures remove the temp and preserve
21342137
recorder.on("error", (error) => errors.push(error))
21352138
recorder.stop()
21362139
await recorder.closed
2137-
expect(recorder.state).toBe("errored")
2138-
expect(errors[0]?.context.action).toBe("finalize")
2139-
expect(await readFile(filePath, "utf8")).toBe("existing")
2140+
const denied = mode === "sync-denied"
2141+
expect(recorder.state).toBe(denied ? "stopped" : "errored")
2142+
expect(errors.map((error) => error.context.action)).toEqual(denied ? [] : ["finalize"])
2143+
expect(
2144+
(await readFile(filePath)).equals(denied ? buildPcm16Wav([0.25, -0.25], 1) : Buffer.from("existing")),
2145+
).toBe(true)
21402146
expect(await readdir(directory)).toEqual([`${mode}.wav`])
2147+
expect(handle?.fd).toBe(-1)
21412148
} finally {
21422149
restore()
2143-
void originalSync
21442150
ring.restore()
21452151
}
21462152
}

‎packages/web/src/content/docs/application-apis/audio-capture.mdx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,8 @@ Recording first writes a placeholder header to that temporary file. It does not
287287

288288
`stop()` stops input, drains all buffered frames, and writes the final WAV header. It then syncs and closes the temporary file.
289289

290+
On Node.js 26.9.0 or later with `--permission`, Node.js blocks `FileHandle.sync()`. The recorder then closes the file without a sync, so the data is not forced to disk before the rename.
291+
290292
After those steps, OpenTUI calls a same-directory filesystem `rename()` from the temporary path to the destination. The host filesystem defines replacement and rename guarantees.
291293

292294
OpenTUI adds no guarantee beyond that rename behavior. Code that needs a specific durability or replacement contract must account for the target filesystem.

0 commit comments

Comments
 (0)