fix(browser-files): resolve bounded object URL source inside the result boundary (R-2.1)

boundedObjectUrlHandoff called resolveByteSource outside its try/finally.
When the authorized source open threw or was aborted, deliver() rejected
with an AbortError instead of returning {ok:false, ABORTED}, the observer
saw nothing, and the holder was never latched closed, so a lease that
arrived later was never closed by compensateLateSource.

Source resolution now runs inside the existing try/catch/finally, the same
shape promptAndStream already uses. The explicit closeHeldSource calls on
the early-return paths are covered by the finally.

Verified: "resolves ABORTED and closes a late lease when a bounded source
open is aborted" failed before the fix (promise rejected with AbortError)
and passes now. check:architecture, check:types:app, check:types:test,
lint, test:component pass. test:unit passes except removal-fixture, which
needs generated artifacts/release files that a fresh worktree lacks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
DongHyeonka
2026-09-24 12:15:58 +09:00
co-authored by Claude Opus 5.5
parent 997761f1ba
commit 54abaec12a
2 changed files with 88 additions and 24 deletions
@@ -731,34 +731,34 @@ async function boundedObjectUrlHandoff(context: Readonly<{
);
}
const sourceHolder = createSourceHolder();
const sourceResult = await resolveByteSource(
context.input,
context.input.signal,
context.options,
sourceHolder,
);
if (!sourceResult.ok) {
closeHeldSource(sourceHolder);
return observeResult(sourceResult, context.options.observer);
}
const source = sourceResult.value;
if (
source.byteLength !== null &&
(source.byteLength > context.input.maxBufferedBytes ||
source.byteLength > context.input.maxTransferBytes)
) {
closeHeldSource(sourceHolder);
return observeResult(
browserDataFailure("LIMIT_EXCEEDED", "DOWNLOAD"),
context.options.observer,
source.byteLength,
);
}
const chunks: ArrayBuffer[] = [];
let transferred = 0;
let lease: ObjectUrlLease | undefined;
try {
// Inside the try: an open that throws or is aborted must still resolve to
// a Result and reach the finally that latches the holder for late leases.
const sourceResult = await resolveByteSource(
context.input,
context.input.signal,
context.options,
sourceHolder,
);
if (!sourceResult.ok) {
return observeResult(sourceResult, context.options.observer);
}
const source = sourceResult.value;
if (
source.byteLength !== null &&
(source.byteLength > context.input.maxBufferedBytes ||
source.byteLength > context.input.maxTransferBytes)
) {
return observeResult(
browserDataFailure("LIMIT_EXCEEDED", "DOWNLOAD"),
context.options.observer,
source.byteLength,
);
}
context.progress.report("PREPARING", 0, source.byteLength, true);
for await (const chunkResult of abortableFileChunks(
source,
+64
View File
@@ -746,6 +746,70 @@ describe("browser download delivery", () => {
expect(handoff).not.toHaveBeenCalled();
});
it("resolves ABORTED and closes a late lease when a bounded source open is aborted", async () => {
const controller = new AbortController();
let releaseOpen: ((value: unknown) => void) | undefined;
const close = vi.fn();
const handoff = vi.fn();
const record = vi.fn();
const adapter = createDownloadDeliveryAdapter({
...HARD_LIMITS,
policies,
host: { handoff },
browserManagedCapabilities: capabilityResolver(),
userActivation: { isActive: true },
observer: { record },
// Ignores the signal and resolves only when the test says so.
openAuthorizedSource: () =>
new Promise((resolve) => {
releaseOpen = resolve;
}) as never,
});
const capability = Object.freeze({
capabilityReceipt: "capability-bounded-1",
method: "GET" as const,
binding: { kind: "DOWNLOAD" as const, resourceId: "resource-1" },
mediaType: "application/pdf",
byteLength: 3,
maxBytes: 3,
expectedSha256: "a".repeat(64),
expiresAtEpochMs: Number.MAX_SAFE_INTEGER,
});
const delivering = adapter.deliver(
deliveryInput(
{
kind: "AUTHORIZED_STREAM_RESOURCE",
resourceId: "resource-1",
capability: capability as never,
},
"BOUNDED_OBJECT_URL",
controller.signal,
),
);
await new Promise((resolve) => setTimeout(resolve, 0));
controller.abort();
await expect(delivering).resolves.toMatchObject({
ok: false,
error: { code: "ABORTED" },
});
expect(record).toHaveBeenCalled();
releaseOpen?.({
ok: true,
value: {
byteLength: 3,
integrity: "VERIFIED_ON_SUCCESSFUL_EXHAUSTION",
capability,
close,
async *stream() {},
},
});
await new Promise((resolve) => setTimeout(resolve, 0));
expect(close).toHaveBeenCalledOnce();
expect(handoff).not.toHaveBeenCalled();
});
it("rejects caller limits above runtime hard caps before side effects", async () => {
const handoff = vi.fn();
const showSaveFilePicker = vi.fn(async () => writableHandle([]));