Skip to content

Commit 9b7b1ba

Browse files
authored
core: keep recordings when the Permission Model blocks fsync (anomalyco#1907)
Since Node 26.9.0, `FileHandle.sync()` throws `ERR_ACCESS_DENIED` whenever the Permission Model is on (nodejs/node#65431). `AudioRecorder` finalizes a recording with `sync()` and `close()`, so under `--permission` every recording failed with "Audio recorder finalize failed" and was discarded. OpenTUI's own Node test lane runs with `--permission` and hits this as soon as CI moves past Node 26.7. The recorder now closes the file without the sync when that call is denied, then publishes it as usual. Any other sync error still fails the recording and keeps the destination. The audio capture docs say that under `--permission` the file is not forced to disk before the rename. Tested by rows in the existing sync-failure table (denied: the recording completes and the file is closed; another error code: it still fails) and by the core Node lane on Node 26.7.0 and 26.13.0.
1 parent b9028c3 commit 9b7b1ba

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)