Skip to content

Commit 0d18bde

Browse files
YigtwxxTakhoffman
andauthored
fix(media): release rejected BytePlus and Runway video downloads under debug capture (#119257)
…r debug capture When the debug proxy is enabled it patches global fetch and clones every response, so the caller-facing body is one branch of a live tee. Cancelling such a branch settles only after both branches cancel, so awaiting it in the rejected-download path leaves the download pending instead of surfacing the malformed-response error. Both providers read the completed media URL through the global fetch, so they reach the capture clone. Switch them to the fire-and-forget release the other generated-media owners already use, and cover each with a regression that keeps a capture clone live while the download is rejected. Co-authored-by: Tak Hoffman <[email protected]>
1 parent c81aee5 commit 0d18bde

4 files changed

Lines changed: 114 additions & 2 deletions

File tree

‎extensions/byteplus/video-generation-provider.test.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,60 @@ describe("byteplus video generation provider", () => {
337337
expect(canceled).toHaveBeenCalledOnce();
338338
});
339339

340+
it("releases a rejected download body without awaiting a debug-capture tee branch", async () => {
341+
postJsonRequestMock.mockResolvedValue({
342+
response: streamedJsonResponse({ id: "task-captured-response" }),
343+
release: vi.fn(async () => {}),
344+
});
345+
// The debug proxy clones every captured response, so the caller-facing body is one
346+
// branch of a live tee. Cancelling such a branch settles only once both branches
347+
// cancel, so awaiting it here would hang the download instead of surfacing the error.
348+
const response = new Response(
349+
new ReadableStream({
350+
start(controller) {
351+
controller.enqueue(new TextEncoder().encode('{"error":"still streaming"}'));
352+
},
353+
}),
354+
{ headers: { "content-type": "application/json" } },
355+
);
356+
const captureClone = response.clone();
357+
const captureReader = captureClone.body?.getReader();
358+
await captureReader?.read();
359+
fetchWithTimeoutMock
360+
.mockResolvedValueOnce(
361+
streamedJsonResponse({
362+
id: "task-captured-response",
363+
status: "succeeded",
364+
content: { video_url: "https://example.com/invalid.mp4" },
365+
}),
366+
)
367+
.mockResolvedValueOnce(response);
368+
369+
let timeout: ReturnType<typeof setTimeout> | undefined;
370+
try {
371+
await expect(
372+
Promise.race([
373+
buildBytePlusVideoGenerationProvider().generateVideo({
374+
provider: "byteplus",
375+
model: "seedance-1-0-pro-250528",
376+
prompt: "captured invalid response",
377+
cfg: {},
378+
}),
379+
new Promise<never>((_resolve, reject) => {
380+
timeout = setTimeout(() => {
381+
reject(new Error("BytePlus download waited for a captured response clone"));
382+
}, 500);
383+
}),
384+
]),
385+
).rejects.toThrow("BytePlus generated video download: malformed video response");
386+
} finally {
387+
if (timeout !== undefined) {
388+
clearTimeout(timeout);
389+
}
390+
await captureReader?.cancel().catch(() => undefined);
391+
}
392+
});
393+
340394
it("rejects generated video downloads that exceed the configured media cap", async () => {
341395
postJsonRequestMock.mockResolvedValue({
342396
response: streamedJsonResponse({ id: "task_too_large" }),

‎extensions/byteplus/video-generation-provider.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,9 @@ async function downloadBytePlusVideo(params: {
201201
assertProviderBinaryResponseContent(response, "BytePlus generated video download", "video");
202202
} catch (error) {
203203
// A rejected binary response still owns a live socket until its unread body is canceled.
204-
await response.body?.cancel().catch(() => undefined);
204+
// A debug-capture clone can keep the tee open, so waiting for cancel would hang
205+
// before the rejected response and its dispatcher can be released.
206+
void response.body?.cancel().catch(() => undefined);
205207
throw error;
206208
}
207209
const mimeType = normalizeOptionalString(response.headers.get("content-type")) ?? "video/mp4";

‎extensions/runway/video-generation-provider.test.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,60 @@ describe("runway video generation provider", () => {
210210
expect(canceled).toHaveBeenCalledOnce();
211211
});
212212

213+
it("releases a rejected download body without awaiting a debug-capture tee branch", async () => {
214+
postJsonRequestMock.mockResolvedValue({
215+
response: streamedJsonResponse({ id: "task-captured-response" }),
216+
release: vi.fn(async () => {}),
217+
});
218+
// The debug proxy clones every captured response, so the caller-facing body is one
219+
// branch of a live tee. Cancelling such a branch settles only once both branches
220+
// cancel, so awaiting it here would hang the download instead of surfacing the error.
221+
const response = new Response(
222+
new ReadableStream({
223+
start(controller) {
224+
controller.enqueue(new TextEncoder().encode('{"error":"still streaming"}'));
225+
},
226+
}),
227+
{ headers: { "content-type": "application/json" } },
228+
);
229+
const captureClone = response.clone();
230+
const captureReader = captureClone.body?.getReader();
231+
await captureReader?.read();
232+
fetchWithTimeoutMock
233+
.mockResolvedValueOnce(
234+
streamedJsonResponse({
235+
id: "task-captured-response",
236+
status: "SUCCEEDED",
237+
output: ["https://example.com/invalid.mp4"],
238+
}),
239+
)
240+
.mockResolvedValueOnce(response);
241+
242+
let timeout: ReturnType<typeof setTimeout> | undefined;
243+
try {
244+
await expect(
245+
Promise.race([
246+
buildRunwayVideoGenerationProvider().generateVideo({
247+
provider: "runway",
248+
model: "gen4.5",
249+
prompt: "captured invalid response",
250+
cfg: {},
251+
}),
252+
new Promise<never>((_resolve, reject) => {
253+
timeout = setTimeout(() => {
254+
reject(new Error("Runway download waited for a captured response clone"));
255+
}, 500);
256+
}),
257+
]),
258+
).rejects.toThrow("Runway generated video download: malformed video response");
259+
} finally {
260+
if (timeout !== undefined) {
261+
clearTimeout(timeout);
262+
}
263+
await captureReader?.cancel().catch(() => undefined);
264+
}
265+
});
266+
213267
it("rejects generated video downloads that exceed the configured media cap", async () => {
214268
postJsonRequestMock.mockResolvedValue({
215269
response: streamedJsonResponse({ id: "task-too-large" }),

‎extensions/runway/video-generation-provider.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,9 @@ async function downloadRunwayVideos(params: {
320320
assertProviderBinaryResponseContent(response, "Runway generated video download", "video");
321321
} catch (error) {
322322
// A rejected binary response still owns a live socket until its unread body is canceled.
323-
await response.body?.cancel().catch(() => undefined);
323+
// A debug-capture clone can keep the tee open, so waiting for cancel would hang
324+
// before the rejected response and its dispatcher can be released.
325+
void response.body?.cancel().catch(() => undefined);
324326
throw error;
325327
}
326328
const mimeType = normalizeOptionalString(response.headers.get("content-type")) ?? "video/mp4";

0 commit comments

Comments
 (0)