refactor(filesystem): share directory cleanup ownership

Reuse the existing directory close state machine for artifact and reference readers. ArtifactResourceScope can retain the exact directory owner when cleanup fails.
This commit is contained in:
morluto
2026-10-11 06:10:10 +08:00
parent 9515d2fcd3
commit 215001a1ce
3 changed files with 41 additions and 33 deletions
+7
View File
@@ -1,4 +1,5 @@
import type { OwnedFileHandle } from "../filesystem/OwnedFileHandle.js";
import type { OwnedDirectoryHandle } from "../filesystem/OwnedDirectoryHandle.js";
import type { ArtifactReader } from "./ArtifactReader.js";
import { ArtifactReaderFailure } from "./ArtifactReader.js";
@@ -6,6 +7,11 @@ import type { SafeOutputTree } from "./SafeOutputTree.js";
/** One artifact resource and its format-specific cleanup owner. */
export type ArtifactResourceOwner =
| {
readonly kind: "directory-handle";
readonly resource: string;
readonly handle: OwnedDirectoryHandle;
}
| {
readonly kind: "reader";
readonly resource: string;
@@ -132,6 +138,7 @@ const cleanup = async (owner: ArtifactResourceOwner): Promise<void> => {
await owner.reader.close();
return;
case "file-handle":
case "directory-handle":
await owner.handle.close();
return;
case "output-tree":
+2 -33
View File
@@ -1,4 +1,4 @@
import { constants, type Dir, type Stats } from "node:fs";
import { constants, type Stats } from "node:fs";
import {
lstat,
open,
@@ -14,6 +14,7 @@ import {
type ArtifactEntry,
type ArtifactReader,
} from "./ArtifactReader.js";
import { OwnedDirectoryHandle } from "../filesystem/OwnedDirectoryHandle.js";
import { OwnedFileHandle } from "../filesystem/OwnedFileHandle.js";
import { readFileHandleChunks } from "../filesystem/readFileHandleChunks.js";
@@ -287,38 +288,6 @@ const artifactEntry = (
};
};
type DirectoryCloseState =
| { readonly kind: "open" }
| { readonly kind: "closing"; readonly attempt: Promise<void> }
| { readonly kind: "closed" }
| { readonly kind: "failed"; readonly cause: unknown };
/** Dir.close invalidates Node's private handle before its native close settles. */
class OwnedDirectoryHandle {
#state: DirectoryCloseState = { kind: "open" };
constructor(readonly handle: Dir) {}
close(): Promise<void> {
if (this.#state.kind === "closed") return Promise.resolve();
if (this.#state.kind === "closing") return this.#state.attempt;
if (this.#state.kind === "failed") return Promise.reject(this.#state.cause);
const attempt = Promise.resolve().then(async () => {
try {
await this.handle.close();
this.#state = { kind: "closed" };
} catch (cause: unknown) {
// Dir exposes no public validity signal; a second close can only lie
// with ERR_DIR_CLOSED after Node has invalidated the first owner.
this.#state = { kind: "failed", cause };
throw cause;
}
});
this.#state = { kind: "closing", attempt };
return attempt;
}
}
const directoryIoFailure = (
operation: string,
path: string,
+32
View File
@@ -0,0 +1,32 @@
import type { Dir } from "node:fs";
type CloseState =
| { readonly kind: "open" }
| { readonly kind: "closing"; readonly attempt: Promise<void> }
| { readonly kind: "closed" }
| { readonly kind: "failed"; readonly cause: unknown };
/** Dir.close invalidates Node's private handle before its native close settles. */
export class OwnedDirectoryHandle {
#state: CloseState = { kind: "open" };
constructor(readonly handle: Dir) {}
close(): Promise<void> {
if (this.#state.kind === "closed") return Promise.resolve();
if (this.#state.kind === "closing") return this.#state.attempt;
if (this.#state.kind === "failed") return Promise.reject(this.#state.cause);
const attempt = Promise.resolve().then(async () => {
try {
await this.handle.close();
this.#state = { kind: "closed" };
} catch (cause: unknown) {
// Dir has no public validity signal with which to authorize a retry.
this.#state = { kind: "failed", cause };
throw cause;
}
});
this.#state = { kind: "closing", attempt };
return attempt;
}
}