microsoft / playwright

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
https://playwright.dev
Apache License 2.0
67.12k stars 3.69k forks source link

[Bug]: WebSocket leakage in UI Mode #33641

Closed ruifigueira closed 4 days ago

ruifigueira commented 5 days ago

Version

1.48.2

Steps to reproduce

npx playwright test --ui

Image

Here is a test that catches the leakage (requires runUITest fixture from tests/playwright-test/ui-mode-fixtures.ts):

test('should not leak websocket connections', async ({ runUITest }) => {
  const { page } = await runUITest({
    'a.test.ts': `
      import { test, expect } from '@playwright/test';
      test('test', async () => {});
    `,
  });

  const [ws1] = await Promise.all([
    page.waitForEvent('websocket'),
    page.getByTitle('Reload').click(),
  ]);

  await Promise.all([
    page.waitForEvent('websocket'),
    page.getByTitle('Reload').click(),
  ]);

  await expect.poll(() => ws1.isClosed()).toBe(true);
});

Expected behavior

Existing WebSocket connection should close when reloading.

Actual behavior

Existing WebSocket connections are kept open.

Additional context

testServerConnection is never closed when a reload is performed:

https://github.com/microsoft/playwright/blob/46321e5bf25225762a375514063c55bda130ec9e/packages/trace-viewer/src/ui/uiModeView.tsx#L111-L120

If existing connection is closed in reloadTests, the test above no longer fails:

  const reloadTests = React.useCallback(() => {
    setTestServerConnection(prevConnection => {
      prevConnection?.close();
      return new TestServerConnection(new WebSocketTestServerTransport(wsURL));
    });
  }, []);

Environment

System:
    OS: Windows 11 10.0.22631
    CPU: (12) x64 13th Gen Intel(R) Core(TM) i5-1335U
    Memory: 12.87 GB / 31.66 GB
  Binaries:
    Node: 20.17.0 - ~\scoop\apps\nvm\current\nodejs\nodejs\node.EXE
    Yarn: 1.22.22 - ~\scoop\apps\yarn\current\bin\yarn.CMD
    npm: 10.8.2 - ~\scoop\apps\nvm\current\nodejs\nodejs\npm.CMD
    pnpm: 9.12.3 - ~\scoop\shims\pnpm.EXE
  IDEs:
    VSCode: 1.95.3 - C:\Users\rui.figueira\AppData\Local\Programs\Microsoft VS Code\bin\code.CMD
  Languages:
    Bash: 5.2.37 - C:\Users\rui.figueira\scoop\shims\bash.EXE
  npmPackages:
    @playwright/test: ^1.48.2 => 1.48.2
pavelfeldman commented 5 days ago

True! Do you want to send a fix?