Skip to content

Commit a169686

Browse files
authored
fix(locator): prevent handler deadlocks after action timeouts (#43612)
1 parent c409f8c commit a169686

4 files changed

Lines changed: 45 additions & 3 deletions

File tree

‎packages/playwright-core/src/server/page.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ export class Page extends SdkObject<PageEventMap> {
571571

572572
private async _performLocatorHandlersCheckpoint(progress: Progress) {
573573
// Do not run locator handlers from inside locator handler callbacks to avoid deadlocks.
574-
if (this._locatorHandlerRunningCounter)
574+
if (this._locatorHandlerRunningCounter || [...this._locatorHandlers.values()].some(handler => !!handler.resolved))
575575
return;
576576
for (const [uid, handler] of this._locatorHandlers) {
577577
if (!handler.resolved) {

‎tests/library/multiclient.spec.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,3 +442,33 @@ test('MC-019 should not deliver an action buffered before the second recording s
442442
expect(recorderA.selectors).toEqual(['internal:role=button[name="Before"i]']);
443443
expect(recorderB.selectors).toEqual(['internal:role=button[name="After"i]']);
444444
});
445+
446+
test('MC-020 should run locator handlers after reconnecting with a pending callback', {
447+
annotation: { type: 'issue', description: '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/microsoft/playwright/issues/43607' },
448+
}, async ({ connect, remoteServer, server }) => {
449+
const browser = await connect(remoteServer.wsEndpoint());
450+
const page = await browser.newPage();
451+
await page.goto(server.PREFIX + '/input/handle-locator.html');
452+
453+
let called = 0;
454+
await page.addLocatorHandler(page.getByText('This interstitial covers the button'), async () => {
455+
++called;
456+
await new Promise(() => {});
457+
});
458+
await page.evaluate(() => {
459+
(window as any).setupAnnoyingInterstitial('none', 1);
460+
});
461+
const error = await page.locator('#target').click({ timeout: 1000 }).catch(e => e);
462+
expect(error.message).toContain('Timeout 1000ms exceeded');
463+
expect(called).toBe(1);
464+
465+
await disconnect(page);
466+
467+
const reconnectedBrowser = await connect(remoteServer.wsEndpoint());
468+
const reconnectedPage = reconnectedBrowser.contexts()[0].pages()[0];
469+
await reconnectedPage.addLocatorHandler(reconnectedPage.getByText('This interstitial covers the button'), async () => {
470+
await reconnectedPage.locator('#close').click();
471+
});
472+
await reconnectedPage.locator('#target').click({ timeout: 3000 });
473+
expect(await reconnectedPage.evaluate('window.clicked')).toBe(1);
474+
});

‎tests/page/page-add-locator-handler.spec.ts‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@
1414
* limitations under the License.
1515
*/
1616

17+
import { iso } from '../../packages/playwright-core/lib/coreBundle';
1718
import { test, expect } from './pageTest';
1819

20+
const { ManualPromise } = iso;
21+
1922
test('LH-001 should work', async ({ page, server }) => {
2023
await page.goto(server.PREFIX + '/input/handle-locator.html');
2124

@@ -120,14 +123,19 @@ test('LH-004 should not work with force:true', async ({ page, server }) => {
120123
expect(await page.evaluate('window.clicked')).toBe(undefined);
121124
});
122125

123-
test('LH-005 should throw when handler times out', async ({ page, server }) => {
126+
test('LH-005 should throw when handler times out', {
127+
annotation: { type: 'issue', description: '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/microsoft/playwright/issues/43607' },
128+
}, async ({ page, server }) => {
124129
await page.goto(server.PREFIX + '/input/handle-locator.html');
125130

126131
let called = 0;
132+
const releaseHandler = new ManualPromise();
133+
const handlerFinished = new ManualPromise<Error | void>();
127134
await page.addLocatorHandler(page.getByText('This interstitial covers the button'), async () => {
128135
++called;
129136
// Deliberately timeout.
130-
await new Promise(() => {});
137+
await releaseHandler;
138+
handlerFinished.resolve(await page.locator('#close').click({ timeout: 3000 }).catch(e => e));
131139
});
132140

133141
await page.locator('#aside').hover();
@@ -143,6 +151,9 @@ test('LH-005 should throw when handler times out', async ({ page, server }) => {
143151

144152
// Should not enter the same handler while it is still running.
145153
expect(called).toBe(1);
154+
155+
releaseHandler.resolve();
156+
expect(await handlerFinished).toBeUndefined();
146157
});
147158

148159
test('LH-006 should work with toBeVisible', async ({ page, server }) => {

‎tests/registry.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3727,6 +3727,7 @@ subsystem, add a row to the Subsystems table. Then update this file:
37273727
| `MC-017` | `library/multiclient.spec.ts` | screencast should deliver cached last frame to a new client |
37283728
| `MC-018` | `library/multiclient.spec.ts` | should stop tracing upon disconnect |
37293729
| `MC-019` | `library/multiclient.spec.ts` | should not deliver an action buffered before the second recording started |
3730+
| `MC-020` | `library/multiclient.spec.ts` | should run locator handlers after reconnecting with a pending callback |
37303731

37313732
### MO — Mouse
37323733

0 commit comments

Comments
 (0)