Skip to content

Commit 15c823b

Browse files
allensquareSquirtle Squadclaude
committed
fix(github): cap media renewal reads per panel
A load reset the retry count, and media that loaded and then failed on the same link read again at once, so a partly loading video could read GitHub in a loop. Each source now reads at once only on its first failure, and the 3 retries never refill, so a panel reads at most once per signed source plus three times. Co-authored-by: Squirtle Squad <ec57954349ef49ce554b9cda062c34933dd3a085cf82278bb0e955efea492c7e@buzz.block.builderlab.xyz> Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Allen Tsai <[email protected]>
1 parent db501c7 commit 15c823b

2 files changed

Lines changed: 96 additions & 13 deletions

File tree

‎src/bundled/github/GitHubBody.test.tsx‎

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ it("reads again 30 seconds after a failed renewal, three times at most", async (
358358
);
359359
});
360360

361-
it("stops retrying once a renewed link loads, and retries afresh after", async () => {
361+
it("stops retrying once a renewed link loads, without refilling its retries", async () => {
362362
vi.useFakeTimers();
363363
let reads = 0;
364364
const refresh = vi.fn(async () => {
@@ -392,12 +392,90 @@ it("stops retrying once a renewed link loads, and retries afresh after", async (
392392
fireEvent.load(screen.getByAltText("Screenshot"));
393393
await wait(5 * 60_000);
394394
expect(refresh).toHaveBeenCalledTimes(2);
395-
// When it expires later it reads at once, then has three retries again.
395+
// When it expires later it already had its read at once; it waits and
396+
// uses only the two retries left.
396397
fireEvent.error(screen.getByAltText("Screenshot"));
398+
expect(refresh).toHaveBeenCalledTimes(2);
399+
await wait(30_000);
397400
expect(refresh).toHaveBeenCalledTimes(3);
398-
await settle();
399401
await wait(5 * 60_000);
400-
expect(refresh).toHaveBeenCalledTimes(6);
402+
expect(refresh).toHaveBeenCalledTimes(4);
403+
expect(vi.getTimerCount()).toBe(0);
404+
});
405+
406+
it("caps reads for media that loads and then fails on each link", async () => {
407+
vi.useFakeTimers();
408+
const privateVideo = (jwt: string) =>
409+
`<video src="${signedVideo(jwt)}"></video>`;
410+
let reads = 0;
411+
// Every read brings a new link, which also loads and then fails.
412+
const refresh = vi.fn(async () => {
413+
reads += 1;
414+
view.rerender(
415+
<GitHubBody
416+
url={url}
417+
body={before}
418+
bodyHtml={privateVideo(String(reads + 1))}
419+
refresh={refresh}
420+
/>,
421+
);
422+
});
423+
const view = render(
424+
<GitHubBody
425+
url={url}
426+
body={before}
427+
bodyHtml={privateVideo("1")}
428+
refresh={refresh}
429+
/>,
430+
);
431+
const video = () => view.container.querySelector("video");
432+
fireEvent.error(video() as HTMLVideoElement);
433+
expect(video()).toHaveAttribute("src", signedVideo("1"));
434+
// Each link loads, then fails, which leaves it unavailable until a newer
435+
// link replaces it.
436+
let cycles = 0;
437+
for (let shown = video(); cycles < 8 && shown; shown = video()) {
438+
fireEvent.loadedData(shown);
439+
fireEvent.error(shown);
440+
cycles += 1;
441+
await settle();
442+
await wait(30_000);
443+
}
444+
expect(cycles).toBe(5);
445+
// One read at once for the source, then three retries; then none.
446+
expect(refresh).toHaveBeenCalledTimes(4);
447+
await wait(10 * 60_000);
448+
expect(refresh).toHaveBeenCalledTimes(4);
449+
expect(vi.getTimerCount()).toBe(0);
450+
});
451+
452+
it("does not refill retries when other media loads", async () => {
453+
vi.useFakeTimers();
454+
const working = `https://private-user-images.githubusercontent.com/1888043/2-dd1f7e4f-e2a4-4aec-b661-cb45032f29c5.png?jwt=1`;
455+
const refresh = vi.fn(() => Promise.reject(new Error("Not Found")));
456+
render(
457+
<GitHubBody
458+
url={url}
459+
body={`${privateBody}\n\n![Working](${after})`}
460+
bodyHtml={`${privateImage("1")}<img src="${working}" alt="Working">`}
461+
refresh={refresh}
462+
/>,
463+
);
464+
failTwice();
465+
await settle();
466+
await wait(30_000);
467+
expect(refresh).toHaveBeenCalledTimes(2);
468+
// Another image loads its signed link, then loads again (as a fullscreen
469+
// viewer's copy would), between retries.
470+
fireEvent.error(screen.getByAltText("Working"));
471+
expect(screen.getByAltText("Working")).toHaveAttribute("src", working);
472+
for (let load = 0; load < 3; load++) {
473+
fireEvent.load(screen.getByAltText("Working"));
474+
await wait(30_000);
475+
}
476+
await wait(10 * 60_000);
477+
expect(refresh).toHaveBeenCalledTimes(4);
478+
expect(vi.getTimerCount()).toBe(0);
401479
});
402480

403481
it("cancels a pending retry on unmount, and schedules none after it", async () => {

‎src/bundled/github/GitHubBody.tsx‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,10 @@ const MAX_RETRIES = 3;
267267
/** A private repository's media links need a GitHub sign-in; `bodyHtml` has
268268
* signed links for them that expire within minutes and change on every read.
269269
* Media whose own link fails takes its signed link, and when that fails reads
270-
* GitHub again with `refresh` and takes the link read. When a read leaves
271-
* media failed with no newer link to try, it reads again after 30 seconds, at
272-
* most three times until a signed link loads. */
270+
* GitHub again with `refresh` and takes the link read. Each source reads at
271+
* once only on its first failure; when a read leaves media failed with no
272+
* newer link to try, it reads again after 30 seconds, at most three times.
273+
* So a mount reads at most once per signed source plus three times. */
273274
export const GitHubBody = memo(function GitHubBody({
274275
body,
275276
bodyHtml,
@@ -289,9 +290,12 @@ export const GitHubBody = memo(function GitHubBody({
289290
() => (bounded ? attachmentMetadata(bodyHtml) : new Map()),
290291
[bounded, bodyHtml],
291292
);
292-
// One read at a time. Media whose signed link failed since it last loaded
293-
// (kept with that link) does not read again at once; it waits for a retry.
293+
// One read at a time, and at most once per signed source plus three
294+
// retries per mount: a source that already read at once (`tried`, kept
295+
// through loads) waits for a retry. `failed` keeps each source's failed
296+
// signed link until a signed link for it loads.
294297
const failed = useRef(new Map<string, string>());
298+
const tried = useRef(new Set<string>());
295299
const reading = useRef(false);
296300
const retry = useRef<{
297301
timer?: ReturnType<typeof setTimeout> | undefined;
@@ -352,13 +356,14 @@ export const GitHubBody = memo(function GitHubBody({
352356
if (!refresh) return;
353357
if (loaded) {
354358
failed.current.delete(source);
355-
retry.current.count = 0;
356359
return;
357360
}
358-
const known = failed.current.has(source);
359361
failed.current.set(source, link);
360-
if (known || reading.current) later();
361-
else read();
362+
if (tried.current.has(source) || reading.current) later();
363+
else {
364+
tried.current.add(source);
365+
read();
366+
}
362367
};
363368

364369
const media: Media = (source, label, image = false, description) => {

0 commit comments

Comments
 (0)