diff --git a/src/artifacts/ArtifactResourceScope.ts b/src/artifacts/ArtifactResourceScope.ts index 8e86e14bd..4aa7b9e37 100644 --- a/src/artifacts/ArtifactResourceScope.ts +++ b/src/artifacts/ArtifactResourceScope.ts @@ -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 => { await owner.reader.close(); return; case "file-handle": + case "directory-handle": await owner.handle.close(); return; case "output-tree": diff --git a/src/artifacts/DirectoryArtifactReader.ts b/src/artifacts/DirectoryArtifactReader.ts index 69beb8e1d..1e70c0e0e 100644 --- a/src/artifacts/DirectoryArtifactReader.ts +++ b/src/artifacts/DirectoryArtifactReader.ts @@ -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 } - | { 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 { - 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, diff --git a/src/filesystem/OwnedDirectoryHandle.ts b/src/filesystem/OwnedDirectoryHandle.ts new file mode 100644 index 000000000..3b8b8894e --- /dev/null +++ b/src/filesystem/OwnedDirectoryHandle.ts @@ -0,0 +1,32 @@ +import type { Dir } from "node:fs"; + +type CloseState = + | { readonly kind: "open" } + | { readonly kind: "closing"; readonly attempt: Promise } + | { 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 { + 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; + } +}