fix: bound resumable teardown, image concurrency and delivery leases
TR-RR-06. dispose() now bounds its drain with a cleanupDeadlineMs from policy and returns the result, so a non-cooperative mutation lock or provider can no longer make teardown unbounded and an unproved drain is reported as still CLOSING instead of closed over. The checkpoint store stays open in that case, because something can still write to it. An abort is admitted physical work like an upload, so it joins the tracked set rather than being stepped over. TR-RR-07. The verification slot belongs to the raw verifier, not the wrapper. Releasing it when the caller's wait expired let an abandoned verification keep running while a new one was admitted, so repeated aborts produced more concurrent physical work than the configured cap allows. The slot is now released only once the raw tasks settle. TR-RR-04. A presigned byte source owns a fetch reader and a capability lease and its port requires close(); the delivery consumer never called it. The closeable subtype is lost in the FileByteSource projection, so a holder keeps it from the moment the lease exists and the outermost finally closes it exactly once — on success, validation failure, writer failure and abort alike. check:adapter-inventory now also fails if the shared abortable-operation primitive has no production importers. It was safe to add only once the presigned subsystems actually migrated onto it; a gate that fails CI for a documented, unfixed defect reports the wrong thing. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
46e067e555
commit
5a76f95291
@@ -1006,7 +1006,13 @@ describe("production image CDN runtime", () => {
|
||||
expect(abortDeadline.clearTimeout).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it("bounds concurrent capability verification and releases the slot after abort", async () => {
|
||||
/**
|
||||
* TR-RR-07. The concurrency cap exists to bound *physical* verification work.
|
||||
* Releasing the slot when the wrapper's abort resolved let an abandoned
|
||||
* verifier keep running while a new one was admitted, so repeated aborts
|
||||
* produced more concurrent work than the configured cap allows.
|
||||
*/
|
||||
it("holds the verification slot until the raw verifier settles", async () => {
|
||||
const preset = imageCdnPresetReference(
|
||||
"verification-concurrency",
|
||||
"bound-image-verification-concurrency",
|
||||
@@ -1018,10 +1024,13 @@ describe("production image CDN runtime", () => {
|
||||
},
|
||||
});
|
||||
let verificationAttempt = 0;
|
||||
let releaseFirst: ((value: boolean) => void) | undefined;
|
||||
const verify = vi.fn(() => {
|
||||
verificationAttempt += 1;
|
||||
return verificationAttempt === 1
|
||||
? new Promise<boolean>(() => undefined)
|
||||
? new Promise<boolean>((resolve) => {
|
||||
releaseFirst = resolve;
|
||||
})
|
||||
: Promise.resolve(true);
|
||||
});
|
||||
const runtime = createImageCdnRuntime({
|
||||
@@ -1054,9 +1063,24 @@ describe("production image CDN runtime", () => {
|
||||
ok: false,
|
||||
error: { code: "ABORTED" },
|
||||
});
|
||||
|
||||
// The caller's wait ended, but the raw verifier has not. Admitting a second
|
||||
// one here would put two physical verifications under a cap of one.
|
||||
await expect(
|
||||
runtime.assets.acceptBackendIssued(issued),
|
||||
).resolves.toMatchObject({ ok: true });
|
||||
).resolves.toMatchObject({
|
||||
ok: false,
|
||||
error: { code: "LIMIT_EXCEEDED" },
|
||||
});
|
||||
expect(verify).toHaveBeenCalledOnce();
|
||||
|
||||
// Once the raw verifier settles the slot is free again.
|
||||
releaseFirst?.(true);
|
||||
await vi.waitFor(async () => {
|
||||
await expect(
|
||||
runtime.assets.acceptBackendIssued(issued),
|
||||
).resolves.toMatchObject({ ok: true });
|
||||
});
|
||||
expect(verify).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
|
||||
@@ -1975,6 +1975,114 @@ describe("presigned transfer", () => {
|
||||
});
|
||||
expect(written).toEqual([...bytes]);
|
||||
});
|
||||
|
||||
/**
|
||||
* TR-RR-04. A presigned byte source owns a fetch reader and a capability
|
||||
* lease, and its port requires `close()`. The delivery consumer never called
|
||||
* it, so every outcome — success, validation failure, writer failure and
|
||||
* abort — leaked both.
|
||||
*/
|
||||
it.each([
|
||||
{ label: "success", mode: "SUCCESS" as const },
|
||||
{ label: "writer failure", mode: "WRITER_FAILURE" as const },
|
||||
{ label: "abort", mode: "ABORT" as const },
|
||||
])("closes the presigned source exactly once on $label", async ({ mode }) => {
|
||||
const bytes = new Uint8Array([1, 2, 3]);
|
||||
let closes = 0;
|
||||
const controller = new AbortController();
|
||||
const source = {
|
||||
byteLength: bytes.byteLength,
|
||||
integrity: "VERIFIED_ON_SUCCESSFUL_EXHAUSTION" as const,
|
||||
capability: undefined as never,
|
||||
close() {
|
||||
closes += 1;
|
||||
},
|
||||
async *stream() {
|
||||
if (mode === "ABORT") controller.abort();
|
||||
yield { ok: true as const, value: bytes };
|
||||
},
|
||||
};
|
||||
const capability = Object.freeze({
|
||||
capabilityReceipt: "capability-close-1",
|
||||
method: "GET" as const,
|
||||
binding: { kind: "DOWNLOAD" as const, resourceId: "resource-1" },
|
||||
mediaType: "application/octet-stream",
|
||||
byteLength: bytes.byteLength,
|
||||
maxBytes: bytes.byteLength,
|
||||
expectedSha256: "a".repeat(64),
|
||||
expiresAtEpochMs: NOW + 60_000,
|
||||
});
|
||||
source.capability = capability as never;
|
||||
|
||||
const closePolicy = browserFilePolicyReference(
|
||||
"download",
|
||||
"presigned-close",
|
||||
);
|
||||
const policies = new BrowserFilePolicyRegistry({
|
||||
profiles: [
|
||||
{
|
||||
reference: closePolicy,
|
||||
download: {
|
||||
strategy: "PROMPT_AND_STREAM",
|
||||
mediaType: "application/octet-stream",
|
||||
safeExtension: ".bin",
|
||||
maxTransferBytes: 64,
|
||||
maxBufferedBytes: 8,
|
||||
integrity: "REQUIRED",
|
||||
},
|
||||
},
|
||||
],
|
||||
hardLimits: {
|
||||
maxInspectionBytes: 64,
|
||||
maxRetainedFileBytes: 64,
|
||||
maxPreviewBytes: 64,
|
||||
maxObjectUrlBytes: 64,
|
||||
maxTransferBytes: 64,
|
||||
},
|
||||
});
|
||||
const handle: SaveFileHandle = {
|
||||
async createWritable() {
|
||||
return new WritableStream<Uint8Array>({
|
||||
write() {
|
||||
if (mode === "WRITER_FAILURE") {
|
||||
throw new TypeError("writer exploded");
|
||||
}
|
||||
},
|
||||
});
|
||||
},
|
||||
};
|
||||
const downloads = createDownloadDeliveryAdapter({
|
||||
host: { handoff() {} },
|
||||
policies,
|
||||
hardMaxObjectUrlBytes: 64,
|
||||
hardMaxTransferBytes: 64,
|
||||
browserManagedCapabilities: {
|
||||
resolve() {
|
||||
throw new TypeError("not used");
|
||||
},
|
||||
},
|
||||
openAuthorizedSource: async () =>
|
||||
({ ok: true, value: source }) as never,
|
||||
showSaveFilePicker: async () => handle,
|
||||
userActivation: { isActive: true },
|
||||
now: () => NOW,
|
||||
});
|
||||
|
||||
const deliveryResult = await downloads.deliver({
|
||||
policy: closePolicy,
|
||||
source: {
|
||||
kind: "AUTHORIZED_STREAM_RESOURCE",
|
||||
resourceId: "resource-1",
|
||||
capability: capability as never,
|
||||
},
|
||||
suggestedFileName: "artifact.bin",
|
||||
signal: controller.signal,
|
||||
onProgress() {},
|
||||
});
|
||||
|
||||
void deliveryResult;
|
||||
expect(closes).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
|
||||
@@ -1280,3 +1280,66 @@ describe("production resumable upload runtime", () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
/**
|
||||
* TR-RR-06. A non-cooperative mutation lock or provider must not make teardown
|
||||
* unbounded: `dispose()` bounds its drain and reports honestly when the runtime
|
||||
* is still CLOSING, and an abort is admitted physical work it cannot step over.
|
||||
*/
|
||||
describe("TR-RR-06 bounded resumable teardown", () => {
|
||||
it("reports an unproved drain instead of waiting forever", async () => {
|
||||
const checkpoints = new MemoryCheckpointStore();
|
||||
const harness = createControlHarness();
|
||||
const runtime = createResumableUploadRuntime({
|
||||
controlPlane: harness.control,
|
||||
partExecutor: executorFor(harness, { delay: async () => {} }),
|
||||
checkpoints,
|
||||
// A lock that never grants: dispose must still be bounded.
|
||||
mutationLock: Object.freeze({
|
||||
async run<Value>(): Promise<Value> {
|
||||
return await new Promise<never>(() => {});
|
||||
},
|
||||
}),
|
||||
crypto,
|
||||
policy: runtimePolicy({ cleanupDeadlineMs: 20 }),
|
||||
now: () => 1_000,
|
||||
random: () => 0,
|
||||
sleep: async () => {},
|
||||
});
|
||||
|
||||
void runtime.upload({
|
||||
uploadKey: "upload_key_hung",
|
||||
purpose: "attachment",
|
||||
mediaType: "application/octet-stream",
|
||||
source: byteStreamSource(new Uint8Array([1, 2, 3, 4, 5])),
|
||||
signal: activeSignal,
|
||||
});
|
||||
await Promise.resolve();
|
||||
await Promise.resolve();
|
||||
|
||||
const disposed = await runtime.dispose();
|
||||
expect(disposed.ok).toBe(false);
|
||||
// Still CLOSING: physical work the caller must not treat as finished.
|
||||
expect(runtime.lifecycle()).toBe("CLOSING");
|
||||
expect(checkpoints.closed).toBe(false);
|
||||
});
|
||||
|
||||
it("closes once every admitted operation settles", async () => {
|
||||
const checkpoints = new MemoryCheckpointStore();
|
||||
const harness = createControlHarness();
|
||||
const runtime = createResumableUploadRuntime({
|
||||
controlPlane: harness.control,
|
||||
partExecutor: executorFor(harness, { delay: async () => {} }),
|
||||
checkpoints,
|
||||
mutationLock: noContentionLock,
|
||||
crypto,
|
||||
policy: runtimePolicy({ cleanupDeadlineMs: 200 }),
|
||||
now: () => 1_000,
|
||||
random: () => 0,
|
||||
sleep: async () => {},
|
||||
});
|
||||
const disposed = await runtime.dispose();
|
||||
expect(disposed).toMatchObject({ ok: true });
|
||||
expect(runtime.lifecycle()).toBe("CLOSED");
|
||||
expect(checkpoints.closed).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user