Skip to content

Commit 693216d

Browse files
committed
fix(qqbot): cancel rejected direct-upload body fire-and-forget under debug proxy
1 parent e797e69 commit 693216d

2 files changed

Lines changed: 45 additions & 1 deletion

File tree

‎extensions/qqbot/src/engine/api/media.test.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,4 +451,45 @@ describe("MediaApi.uploadMedia direct URL uploads", () => {
451451
expect(tokenManager["getAccessToken"]).not.toHaveBeenCalled();
452452
expect(client["request"]).not.toHaveBeenCalled();
453453
});
454+
455+
it("does not hang when the rejected response body cancel never settles", async () => {
456+
fetchWithSsrFGuardMock.mockReset();
457+
const { response, release } = mockGuardedResponse("server error", { status: 500 });
458+
const captureClone = response.clone();
459+
let cancelStarted = false;
460+
vi.spyOn(response.body!, "cancel").mockImplementation(() => {
461+
cancelStarted = true;
462+
// Mimic a debug-capture tee branch: cancellation only settles after the
463+
// sibling clone branch is cancelled.
464+
return new Promise<void>((resolve) => {
465+
void captureClone.body
466+
?.cancel()
467+
.catch(() => undefined)
468+
.then(resolve);
469+
});
470+
});
471+
472+
const client = mockApiClient();
473+
const tokenManager = mockTokenManager();
474+
const api = new MediaApi(client, tokenManager);
475+
476+
const start = Date.now();
477+
await expect(
478+
api.uploadMedia(
479+
"c2c",
480+
"user-openid",
481+
MediaFileType.IMAGE,
482+
{ appId: "app-id", clientSecret: "client-secret" },
483+
{ url: "https://cdn.example.com/server-error.png" },
484+
),
485+
).rejects.toThrow("Direct-upload media URL returned HTTP 500");
486+
487+
// The rejection must return promptly; without the fire-and-forget fix this
488+
// would wait indefinitely for the capture sibling branch to cancel.
489+
expect(Date.now() - start).toBeLessThan(1000);
490+
expect(cancelStarted).toBe(true);
491+
expect(release).toHaveBeenCalledOnce();
492+
expect(tokenManager["getAccessToken"]).not.toHaveBeenCalled();
493+
expect(client["request"]).not.toHaveBeenCalled();
494+
});
454495
});

‎extensions/qqbot/src/engine/api/media.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,10 @@ export async function downloadDirectUploadUrl(
161161
const { response, release } = await fetchDirectUploadDownload(parsed.toString());
162162
try {
163163
if (!response.ok) {
164-
await response.body?.cancel().catch(() => undefined);
164+
// A debug-capture clone can keep the tee open, so waiting for cancel would
165+
// hang before the error is returned. Fire-and-forget matches the timeout
166+
// path above and the pattern used across other plugins.
167+
void response.body?.cancel().catch(() => undefined);
165168
throw new Error(`Direct-upload media URL returned HTTP ${response.status}`);
166169
}
167170
return await readDirectUploadResponse(response, opts.maxBytes ?? MAX_UPLOAD_SIZE);

0 commit comments

Comments
 (0)