Skip to content

Commit 468f21b

Browse files
fix(trace-viewer): handle out-of-order screencast frames (#43648)
1 parent ad5210f commit 468f21b

3 files changed

Lines changed: 41 additions & 6 deletions

File tree

‎packages/isomorphic/trace/snapshotRenderer.ts‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,18 @@ import type { PageEntry } from './entries';
2121
import type { LRUCache } from '../lruCache';
2222

2323
function findClosest<T>(items: T[], metric: (v: T) => number, target: number) {
24-
return items.find((item, index) => {
25-
if (index === items.length - 1)
26-
return true;
27-
const next = items[index + 1];
28-
return Math.abs(metric(item) - target) < Math.abs(metric(next) - target);
29-
});
24+
let closest: T | undefined;
25+
let closestDistance = Infinity;
26+
// Chromium may deliver screencast frames out of order by swap wall time,
27+
// see https://issues.chromium.org/issues/578117966.
28+
for (const item of items) {
29+
const distance = Math.abs(metric(item) - target);
30+
if (distance <= closestDistance) {
31+
closest = item;
32+
closestDistance = distance;
33+
}
34+
}
35+
return closest;
3036
}
3137

3238
function isNodeNameAttributesChildNodesSnapshot(n: NodeSnapshot): n is NodeNameAttributesChildNodesSnapshot {

‎tests/library/snapshot-renderer.spec.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,34 @@ function makeSnapshot(overrides: Partial<FrameSnapshot> = {}): FrameSnapshot {
3636
};
3737
}
3838

39+
test('TV-136 snapshot renderer selects the nearest out-of-order screencast frame', {
40+
annotation: [
41+
{ type: 'issue', description: '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/microsoft/playwright/issues/43573' },
42+
{ type: 'issue', description: 'https://issues.chromium.org/issues/578117966' },
43+
],
44+
}, () => {
45+
// Chromium encodes screencast frames in parallel and sends each one when
46+
// its encoding finishes. Frame 3 was swapped before frame 2, but took
47+
// longer to encode, so it was received later.
48+
const frameSwapWallTimes = [1000, 1017, 1050, 1033, 1067];
49+
const frames = frameSwapWallTimes.map((frameSwapWallTime, index) => ({
50+
file: `${index}.jpeg`,
51+
width: 1280,
52+
height: 720,
53+
timestamp: 1000 + index * 17,
54+
frameSwapWallTime,
55+
}));
56+
const snapshots = [
57+
makeSnapshot({ timestamp: 1030, wallTime: 1030 }),
58+
makeSnapshot({ timestamp: 1070, wallTime: 1070 }),
59+
];
60+
const renderer1 = new SnapshotRenderer(new LRUCache(1_000_000), [], snapshots, frames, 0);
61+
expect(renderer1.closestScreenshot()).toBe('3.jpeg');
62+
// Every later snapshot should not be stuck on the frame before the reversed pair.
63+
const renderer2 = new SnapshotRenderer(new LRUCache(1_000_000), [], snapshots, frames, 1);
64+
expect(renderer2.closestScreenshot()).toBe('4.jpeg');
65+
});
66+
3967
for (const [name, overrides] of [
4068
['callId', { callId: '</script><script>window.__pwned__=true;//' }],
4169
['snapshotName', { callId: 'call-1', snapshotName: '</script><img src=x onerror=alert(1)>' }],

‎tests/registry.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5235,6 +5235,7 @@ subsystem, add a row to the Subsystems table. Then update this file:
52355235
| `TV-133` | `library/snapshot-renderer.spec.ts` | snapshot renderer still renders ordinary custom elements |
52365236
| `TV-134` | `library/snapshot-renderer.spec.ts` | stripAnsiEscapes should not exhibit polynomial backtracking |
52375237
| `TV-135` | `library/snapshot-renderer.spec.ts` | stripAnsiEscapes handles common ANSI sequences after fix |
5238+
| `TV-136` | `library/snapshot-renderer.spec.ts` | snapshot renderer selects the nearest out-of-order screencast frame |
52385239

52395240
### TZ — Timezone
52405241

0 commit comments

Comments
 (0)