Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions playwright/async_api/_context_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
class PlaywrightContextManager:
def __init__(self) -> None:
self._connection: Connection
self._exit_was_called = False

async def __aenter__(self) -> AsyncPlaywright:
loop = asyncio.get_running_loop()
Expand All @@ -51,4 +52,7 @@ async def start(self) -> AsyncPlaywright:
return await self.__aenter__()

async def __aexit__(self, *args: Any) -> None:
if self._exit_was_called:
return
self._exit_was_called = True
await self._connection.stop_async()
4 changes: 4 additions & 0 deletions playwright/sync_api/_context_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def __init__(self) -> None:
self._loop: asyncio.AbstractEventLoop
self._own_loop = False
self._watcher: Optional[AbstractChildWatcher] = None
self._exit_was_called = False

def __enter__(self) -> SyncPlaywright:
try:
Expand Down Expand Up @@ -98,6 +99,9 @@ def start(self) -> SyncPlaywright:
return self.__enter__()

def __exit__(self, *args: Any) -> None:
if self._exit_was_called:
return
self._exit_was_called = True
self._connection.stop_sync()
if self._watcher:
self._watcher.close()
Expand Down
6 changes: 6 additions & 0 deletions tests/async/test_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,9 @@ def exception_handlerdler(loop, context) -> None:
assert handler_exception is None

asyncio.get_running_loop().set_exception_handler(None)


async def test_async_playwright_stop_multiple_times() -> None:
playwright = await async_playwright().start()
await playwright.stop()
await playwright.stop()
14 changes: 14 additions & 0 deletions tests/sync/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import multiprocessing
import os

import pytest
Expand Down Expand Up @@ -290,3 +291,16 @@ def test_expect_response_should_use_context_timeout(
pass
assert exc_info.type is TimeoutError
assert "Timeout 1000ms exceeded" in exc_info.value.message


def _test_sync_playwright_stop_multiple_times() -> None:
playwright = sync_playwright().start()
playwright.stop()
playwright.stop()


def test_sync_playwright_stop_multiple_times() -> None:
p = multiprocessing.Process(target=_test_sync_playwright_stop_multiple_times)
p.start()
p.join()
assert p.exitcode == 0