diff --git a/.github/workflows/reload-repeat.yml b/.github/workflows/reload-repeat.yml new file mode 100644 index 0000000000..91395fed2f --- /dev/null +++ b/.github/workflows/reload-repeat.yml @@ -0,0 +1,58 @@ +name: Reload repeat (diagnostic, not for main) + +# Diagnostic only, never merged: runs conversation-switch and conversation-reopen-end many times +# under the real load of the app (real browser, WebKit) job, to find why they flake in WebKit. +on: + pull_request: + +permissions: + contents: read + +jobs: + repeat: + if: github.head_ref == 'diag/webkit-reload-repeat' + strategy: + fail-fast: false + matrix: + shard: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] + runs-on: openbot-ubuntu-16core + timeout-minutes: 45 + env: + PLAYWRIGHT_IMAGE: mcr.microsoft.com/playwright:v1.62.1-noble@sha256:dcc5531e97840b9b5e794f2814476b21571c5124a3fca2267d73041f56e7580e + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2.2.0 + with: + bun-version: 1.4.3 + - run: bun install --frozen-lockfile + - run: bun run generate:app-config + - name: Copies of the two files, run alongside CI's own list + run: | + for r in 1 2 3; do + cp app/tests/conversation-switch.test.tsx "app/tests/conversation-switch-r$r.test.tsx" + cp app/tests/conversation-reopen-end.test.tsx "app/tests/conversation-reopen-end-r$r.test.tsx" + done + - name: CI's WebKit-job list, eight at once, with the copies + run: | + set +e + LIST=$(grep -o 'app/tests/chunk-recovery.test.tsx.*artifact-sign-in-refusal-browser.test.tsx' .github/workflows/ci.yml | head -1) + COPIES=$(ls app/tests/conversation-switch-r*.test.tsx app/tests/conversation-reopen-end-r*.test.tsx) + docker run --rm --ipc=host \ + --volume "$GITHUB_WORKSPACE:$GITHUB_WORKSPACE" --workdir "$GITHUB_WORKSPACE" \ + --volume "$(command -v bun):/usr/local/bin/bun:ro" \ + --env OPENBOT_APP_BROWSER=1 --env OPENBOT_APP_ENGINES=chromium,webkit \ + "$PLAYWRIGHT_IMAGE" \ + bun test --parallel=8 --timeout=5000 $LIST $COPIES \ + > run.log 2>&1 + echo "exit $?" + echo "reloads ok: $(grep -c 'RELOADOK' run.log) reload errors: $(grep -c 'RELOADERR' run.log)" + grep -E "RELOADERR|The view's timeline|runs again" run.log || true + echo "--- context of each switch failure" + grep -n "(fail) switching conversations" run.log | cut -d: -f1 | while read -r n; do + sed -n "$((n-45)),${n}p" run.log | grep -vE "^\s+at |conversation\.js:" + done + echo "--- failures in the two files" + grep -E "\(fail\).*(switching conversations|long conversation opened again|opening a long conversation)" run.log || echo "none" + echo "--- passes: switch=$(grep -cE '\(pass\) switching conversations' run.log) reopen=$(grep -cE '\(pass\) a long conversation opened again' run.log)" diff --git a/app/src/components/channels/follow-intent.tsx b/app/src/components/channels/follow-intent.tsx index 7c36156a03..e4949635b2 100644 --- a/app/src/components/channels/follow-intent.tsx +++ b/app/src/components/channels/follow-intent.tsx @@ -41,6 +41,11 @@ export type FollowEvent = previousTop: number; fromEnd: number; pointerDown: boolean; + /* + * The scroller held the view at the end before this scroll and let go of it in it (see + * `holdsEnd`). Absent where nobody looked, which reads as holding on. + */ + letGoOfEnd?: boolean; } | { type: "selection"; active: boolean } | { type: "button" } @@ -101,6 +106,20 @@ export function nextFollow(state: FollowState, event: FollowEvent): FollowStep { switch (event.type) { case "scroll": + /* + * A layout correction the scroller took for the person. When the conversation is laid out + * shorter for a moment (its rows drawn again as a replay lands) the browser pulls the view up + * to fit, with no input at all, and the scroller reads a view that went up as somebody + * scrolling up, stops holding the end and stays where it is as the rows come back below. The + * conversation then rests partway up its history with the scroll-to-end button showing. Only + * input stops following here, so the end is asked for again. + */ + if ( + state.following && + event.letGoOfEnd === true && + event.top < event.previousTop + ) + return { state, scrollToEnd: "auto" }; return state.pausedBy === "scroll" && event.top > event.previousTop && event.fromEnd <= RESUME_PX @@ -199,6 +218,7 @@ export function useFollowIntent( const pointerDownRef = useRef(false); const previousTopRef = useRef(0); + const heldEndRef = useRef(true); const touchYRef = useRef(null); useEffect(() => { @@ -279,12 +299,16 @@ export function useFollowIntent( const top = element.scrollTop; const previousTop = previousTopRef.current; previousTopRef.current = top; + const heldEnd = holdsEnd(element); + const heldBefore = heldEndRef.current; + heldEndRef.current = heldEnd; dispatch({ type: "scroll", top, previousTop, fromEnd: element.scrollHeight - top - element.clientHeight, pointerDown: pointerDownRef.current, + letGoOfEnd: heldBefore && !heldEnd, }); }, }; @@ -292,6 +316,18 @@ export function useFollowIntent( return { following: state.following, dispatch, request, viewportHandlers }; } +/** + * Whether the scroller is holding the view at the end: it marks the viewport `data-scrollable` with + * `end` only once it offers a way down to the end, which it does not while it follows the bottom. + * It writes the mark as it reads each scroll, before the viewport's own `onScroll` here runs, so a + * scroll's handler sees whether that scroll made it let go. + */ +function holdsEnd(viewport: HTMLElement): boolean { + return !(viewport.getAttribute("data-scrollable") ?? "") + .split(" ") + .includes("end"); +} + /** * Carries out a resume's scroll to the end. Renders nothing; it lives inside the scroller to reach * it, and runs in the commit after the resume, when the scroller has already been told to follow, so diff --git a/app/tests/conversation-reopen-end.test.tsx b/app/tests/conversation-reopen-end.test.tsx index 04be531b44..6cc872fb53 100644 --- a/app/tests/conversation-reopen-end.test.tsx +++ b/app/tests/conversation-reopen-end.test.tsx @@ -830,10 +830,33 @@ async function open(engine: string, conversation: string): Promise { viewport: { width: 1280, height: 800 }, }); page.on("pageerror", (error) => console.error(error)); + await page.addInitScript(TIMELINE); await page.goto(`${origin()}/channel/${conversation}`); return page; } +/** + * What the conversation's view did, kept in the page for a landing that misses the end to print + * (`expectAtTheEnd`): every frame its position, its height or the scroll-to-end button changed, each + * scroll, each picture that loaded, and any input. A miss on CI is read from that, not guessed at. + */ +const TIMELINE = `(() => { + const tl = []; window.__tl = tl; + const view = () => document.querySelector("[data-slot=message-scroller-viewport]"); + const push = (kind) => { const v = view(); if (!v || tl.length > 600) return; + const b = document.querySelector("[data-slot=message-scroller-button]"); + tl.push([kind, Math.round(performance.now()), Math.round(v.scrollTop), v.scrollHeight, v.clientHeight, b ? b.getAttribute("data-active") : "-"]); }; + document.addEventListener("scroll", (e) => { if (e.target === view()) push("scroll"); }, true); + document.addEventListener("load", (e) => { if (e.target instanceof HTMLImageElement) push("img"); }, true); + for (const k of ["wheel", "touchmove", "keydown"]) document.addEventListener(k, () => push(k), true); + let last = ""; + const frame = () => { const v = view(); if (v) { const b = document.querySelector("[data-slot=message-scroller-button]"); + const now = [Math.round(v.scrollTop), v.scrollHeight, v.clientHeight, b ? b.getAttribute("data-active") : "-"].join(","); + if (now !== last) { last = now; push("frame"); } } + requestAnimationFrame(frame); }; + requestAnimationFrame(frame); +})();`; + /** Switch to a conversation with the sidebar, as a person does. */ async function switchTo(page: Page, summary: string) { await page @@ -874,6 +897,11 @@ async function restingPlace(page: Page): Promise { async function expectAtTheEnd(page: Page, step: string) { const place = await restingPlace(page); await shoot(page, step); + // [what, ms, scrollTop, scrollHeight, clientHeight, button active], oldest first. + if (JSON.stringify(place) !== JSON.stringify(AT_THE_END)) + console.error( + `The view's timeline, ${step}: ${JSON.stringify(place)} ${JSON.stringify(await page.evaluate(() => (window as unknown as { __tl: unknown }).__tl))}`, + ); expect(place).toEqual(AT_THE_END); } @@ -964,6 +992,46 @@ describe.skipIf(!asked)("a long conversation opened again", () => { } }, 60_000); + /* + * Seen in CI, once in about fifty loads in WebKit: the join's replay drew the conversation's rows + * again, and for a moment it was laid out over a thousand pixels shorter. The browser pulled the + * view up to fit, with nobody scrolling, and the scroller took that for the person scrolling up: + * it let go of the end, and as the rows came back below it the view stayed where it was, partway + * up, with the scroll-to-end button showing. Here the moment is made on purpose, the same way: + * the newest rows laid out away, the layout read (which is when the browser pulls the view up), + * and the rows back, all before the scroll is handled. + */ + test(`laid out shorter for a moment with nobody scrolling, as when a replay draws its rows again, stays at the newest message, in ${engine}`, async () => { + longThread.phone = false; + longThread.lagOnce = false; + const page = await open(engine, "long-conversation"); + try { + await settled(page); + await expectAtTheEnd(page, `before-shorter-${engine}`); + const pulledUp = await page.evaluate(() => { + const view = document.querySelector( + "[data-slot=message-scroller-viewport]", + ); + if (!view) throw new Error("no conversation view"); + const before = view.scrollTop; + const rows = [ + ...view.querySelectorAll("[data-message-id]"), + ].slice(-12); + for (const row of rows) row.style.display = "none"; + // Laid out shorter: reading the layout is when the browser pulls the view up to fit. + const pulled = before - view.scrollTop; + for (const row of rows) row.style.display = ""; + return pulled; + }); + // The moment really did pull the view up, or this would prove nothing. + expect(pulledUp).toBeGreaterThan(600); + await still(page); + await expectAtTheEnd(page, `after-shorter-${engine}`); + } finally { + await page.close(); + } + }, 60_000); + test(`opened from the sidebar, the first time in this page, lands at the newest message, in ${engine}`, async () => { longThread.phone = false; longThread.lagOnce = false; diff --git a/app/tests/conversation-switch.test.tsx b/app/tests/conversation-switch.test.tsx index 51044631e5..d642312599 100644 --- a/app/tests/conversation-switch.test.tsx +++ b/app/tests/conversation-switch.test.tsx @@ -779,7 +779,15 @@ describe.skipIf(!asked)("switching conversations while a Bot answers", () => { // And opened again from nothing, as a reload or a new window does. // The stored copy can be on screen before the page joins the thread again, so the // join is waited for, and the answer read again once the replay has landed on it. - await page.reload(); + try { + await page.reload(); + console.log(`RELOADOK ${name} joins=${gateway.joins}`); + } catch (error) { + console.error( + `RELOADERR ${name} joins=${gateway.joins} finished=${gateway.finished} ${String(error).split("\n")[0]}`, + ); + throw error; + } const joined = Date.now() + 15_000; while (gateway.joins < 4 && Date.now() < joined) await sleep(100); expect(gateway.joins).toBe(4); diff --git a/app/tests/follow-intent.test.ts b/app/tests/follow-intent.test.ts index e59598bc5e..13688443b0 100644 --- a/app/tests/follow-intent.test.ts +++ b/app/tests/follow-intent.test.ts @@ -75,6 +75,31 @@ test("a layout correction is not the person scrolling", () => { ); }); +test("a layout correction the scroller took for the person asks for the end again", () => { + // The browser pulled the view up to fit a moment's shorter layout, and the scroller let go. + const letGo = { ...scroll(400, 1700, 800, false), letGoOfEnd: true }; + expect(nextFollow(FOLLOWING, letGo)).toEqual({ + state: FOLLOWING, + scrollToEnd: "auto", + }); + // Paused, it stays paused: only the person brings it back. + expect(nextFollow(PAUSED, letGo)).toEqual({ + state: PAUSED, + scrollToEnd: null, + }); + // A scrollbar drag up is the person: it stops following, whatever the scroller did. + expect(nextFollow(FOLLOWING, { ...letGo, pointerDown: true }).state).toEqual( + PAUSED, + ); + // Going down, or the scroller still holding on, is nothing to correct. + expect( + nextFollow(FOLLOWING, { ...scroll(1700, 400, 800), letGoOfEnd: true }), + ).toEqual({ state: FOLLOWING, scrollToEnd: null }); + expect( + nextFollow(FOLLOWING, { ...scroll(400, 1700, 800), letGoOfEnd: false }), + ).toEqual({ state: FOLLOWING, scrollToEnd: null }); +}); + test("scrolling back down to within 80px resumes", () => { expect(nextFollow(PAUSED, scroll(900, 850, 60))).toEqual({ state: FOLLOWING, diff --git a/app/tests/renderer-crash.fixture.ts b/app/tests/renderer-crash.fixture.ts index a153143b7b..1e639f3f19 100644 --- a/app/tests/renderer-crash.fixture.ts +++ b/app/tests/renderer-crash.fixture.ts @@ -60,6 +60,36 @@ test("crashes every time", async () => { await crashRenderer(); }); +/* + * WebKit failing a reload inside itself, as Playwright reports it from WebKit on Linux: the words + * are copied from CI (conversation-switch.test.tsx on #2669). WebKit can't be made to do it on + * purpose, so the failure is thrown as Playwright throws it. + */ +function webKitFailsReload(): never { + throw new Error( + 'reload: WebKit encountered an internal error\nCall log:\n - waiting for navigation until "load"\n', + ); +} + +test("WebKit fails a reload once", async () => { + if (go("WebKit fails a reload once") === 1) webKitFailsReload(); + const page = await browser.newPage(); + expect(await page.evaluate(() => 1 + 1)).toBe(2); + await page.close(); +}); + +test("WebKit fails a reload every time", async () => { + go("WebKit fails a reload every time"); + webKitFailsReload(); +}); + +test("a reload fails some other way", async () => { + go("a reload fails some other way"); + throw new Error( + 'reload: net::ERR_CONNECTION_REFUSED\nCall log:\n - waiting for navigation until "load"\n', + ); +}); + test.skipIf(false)("crashes once, through skipIf", async () => { if (go("crashes once, through skipIf") === 1) await crashRenderer(); }); diff --git a/app/tests/renderer-crash.test.ts b/app/tests/renderer-crash.test.ts index ce598c5796..61e7c0b57b 100644 --- a/app/tests/renderer-crash.test.ts +++ b/app/tests/renderer-crash.test.ts @@ -2,7 +2,8 @@ import { describe, expect, test } from "bun:test"; import { join } from "node:path"; /** - * A real-browser test the browser's renderer crashed in runs again, up to twice; nothing else does. + * A real-browser test the browser's renderer crashed in runs again, up to twice, and so does one where + * WebKit failed a navigation inside itself ("WebKit encountered an internal error"); nothing else does. * * The real-browser files take `test` from renderer-crash.ts, because WebKit on Linux crashes its own * renderer under CI's load and failed tests for nothing they check. This runs tests written that way @@ -59,5 +60,16 @@ describe.skipIf(!asked)("a renderer crash in a real-browser test", () => { expect(said).toContain( "The browser's renderer crashed during this test, so it runs again (2 of 2).", ); + // WebKit failing a navigation inside itself gets the same goes, said as what it was. + expect(result("WebKit fails a reload once")).toBe("pass"); + expect(goes("WebKit fails a reload once")).toBe(2); + expect(result("WebKit fails a reload every time")).toBe("fail"); + expect(goes("WebKit fails a reload every time")).toBe(3); + expect(said).toContain( + "WebKit failed a navigation inside itself during this test, so it runs again (1 of 2).", + ); + // Any other failure of a navigation is the test's, at once. + expect(result("a reload fails some other way")).toBe("fail"); + expect(goes("a reload fails some other way")).toBe(1); }, 120_000); }); diff --git a/app/tests/renderer-crash.ts b/app/tests/renderer-crash.ts index 54a09ff607..3ceb1a6e18 100644 --- a/app/tests/renderer-crash.ts +++ b/app/tests/renderer-crash.ts @@ -2,7 +2,8 @@ import { test as bunTest } from "bun:test"; import type { Browser, BrowserContext, Page } from "playwright-core"; /* - * A REAL-BROWSER TEST THAT THE BROWSER ITSELF CRASHED RUNS AGAIN, UP TO TWICE. NOTHING ELSE DOES. + * A REAL-BROWSER TEST THAT THE BROWSER ITSELF CRASHED RUNS AGAIN, UP TO TWICE, AND SO DOES ONE WHERE + * WEBKIT FAILED A NAVIGATION INSIDE ITSELF (`webKitFailedInside`, below). NOTHING ELSE DOES. * * Under CI's load (the WebKit job runs eight files at once, each with Chromium and WebKit), WebKit * on Linux kills its own web process now and then: a segmentation fault in its compositor thread @@ -67,6 +68,23 @@ export function rendererCrashes(): number { /** How many more goes a test gets when the browser's renderer crashed in it: three goes in all. */ export const CRASH_RETRIES = 2; +/* + * WEBKIT FAILING A NAVIGATION INSIDE ITSELF IS THE SAME KIND OF FAULT, AND GETS THE SAME GOES. + * + * Under the same load, WebKit on Linux now and then fails a reload or a page load with its own + * "WebKit encountered an internal error": its network loader's, raised before the page has loaded, + * with nothing the page did in it. It hit conversation-switch.test.tsx's reload on CI (#2584, and on + * #2669), and did not come back in 192 WebKit reloads of that test under CI's load. It is matched on + * WebKit's own words, so any other failure of a navigation (a refused address, a page that never + * loads, a timeout) is still the test's. + */ +const WEBKIT_INTERNAL_ERROR = "WebKit encountered an internal error"; + +/** Whether a test failed because WebKit failed a navigation inside itself. */ +export function webKitFailedInside(error: unknown): boolean { + return String(error).includes(WEBKIT_INTERNAL_ERROR); +} + /** * Run `body`, and run it again if a page's renderer crashed while it ran, up to `CRASH_RETRIES` * times. Any other failure, and a crash on the last go, is the test's. @@ -82,9 +100,13 @@ export async function survivingRendererCrash( } catch (error) { // Playwright says the renderer crashed as the calls waiting on its page fail: let it say so. await new Promise((done) => setTimeout(done, 100)); - if (renderersCrashed === before || go === CRASH_RETRIES) throw error; + const crashed = renderersCrashed !== before; + const failedInside = !crashed && webKitFailedInside(error); + if ((!crashed && !failedInside) || go === CRASH_RETRIES) throw error; console.warn( - `The browser's renderer crashed during this test, so it runs again (${go + 1} of ${CRASH_RETRIES}). The failure it caused: ${error}`, + crashed + ? `The browser's renderer crashed during this test, so it runs again (${go + 1} of ${CRASH_RETRIES}). The failure it caused: ${error}` + : `WebKit failed a navigation inside itself during this test, so it runs again (${go + 1} of ${CRASH_RETRIES}). The failure it caused: ${error}`, ); } }