Skip to content

Commit 1aa42fa

Browse files
authored
fix(screenshotter): race cleanup against progress to honor timeout (#40901)
1 parent 8727a66 commit 1aa42fa

2 files changed

Lines changed: 12 additions & 2 deletions

File tree

packages/playwright-core/src/server/screenshotter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ export class Screenshotter {
218218
const viewportRect = options.clip ? trimClipToSize(options.clip, viewportSize) : { x: 0, y: 0, ...viewportSize };
219219
return await this._screenshot(progress, format, undefined, viewportRect, true, options);
220220
} finally {
221-
await this._restorePageAfterScreenshot();
221+
await progress.race(this._restorePageAfterScreenshot()).catch(() => {});
222222
}
223223
});
224224
}
@@ -245,7 +245,7 @@ export class Screenshotter {
245245
documentRect.y += scrollOffset.y;
246246
return await this._screenshot(progress, format, helper.enclosingIntRect(documentRect), undefined, fitsViewport, options);
247247
} finally {
248-
await this._restorePageAfterScreenshot();
248+
await progress.race(this._restorePageAfterScreenshot()).catch(() => {});
249249
}
250250
});
251251
}

tests/library/screenshot.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,16 @@ browserTest.describe('page screenshot', () => {
211211
expect(pixel(0, 999).r).toBeLessThan(128);
212212
expect(pixel(0, 999).b).toBeGreaterThan(128);
213213
});
214+
215+
browserTest('should not hang when event loop is blocked', { annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/36702' } }, async ({ page }) => {
216+
browserTest.setTimeout(5000);
217+
await page.evaluate(() => {
218+
setTimeout(() => {
219+
while (true) {}
220+
}, 10);
221+
});
222+
await expect(page.screenshot({ fullPage: true, timeout: 200 })).rejects.toThrow(/page.screenshot: Timeout 200ms exceeded/);
223+
});
214224
});
215225

216226
browserTest.describe('element screenshot', () => {

0 commit comments

Comments
 (0)