Skip to content

Commit 4d79dfc

Browse files
authored
chore: allow to call Playwright.stop() multiple times (microsoft#1896)
1 parent ebfc27e commit 4d79dfc

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

playwright/async_api/_context_manager.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
class PlaywrightContextManager:
2626
def __init__(self) -> None:
2727
self._connection: Connection
28+
self._exit_was_called = False
2829

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

5354
async def __aexit__(self, *args: Any) -> None:
55+
if self._exit_was_called:
56+
return
57+
self._exit_was_called = True
5458
await self._connection.stop_async()

playwright/sync_api/_context_manager.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def __init__(self) -> None:
3636
self._loop: asyncio.AbstractEventLoop
3737
self._own_loop = False
3838
self._watcher: Optional[AbstractChildWatcher] = None
39+
self._exit_was_called = False
3940

4041
def __enter__(self) -> SyncPlaywright:
4142
try:
@@ -98,6 +99,9 @@ def start(self) -> SyncPlaywright:
9899
return self.__enter__()
99100

100101
def __exit__(self, *args: Any) -> None:
102+
if self._exit_was_called:
103+
return
104+
self._exit_was_called = True
101105
self._connection.stop_sync()
102106
if self._watcher:
103107
self._watcher.close()

tests/async/test_asyncio.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,9 @@ def exception_handlerdler(loop, context) -> None:
4848
assert handler_exception is None
4949

5050
asyncio.get_running_loop().set_exception_handler(None)
51+
52+
53+
async def test_async_playwright_stop_multiple_times() -> None:
54+
playwright = await async_playwright().start()
55+
await playwright.stop()
56+
await playwright.stop()

tests/sync/test_sync.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import multiprocessing
1516
import os
1617

1718
import pytest
@@ -290,3 +291,16 @@ def test_expect_response_should_use_context_timeout(
290291
pass
291292
assert exc_info.type is TimeoutError
292293
assert "Timeout 1000ms exceeded" in exc_info.value.message
294+
295+
296+
def _test_sync_playwright_stop_multiple_times() -> None:
297+
playwright = sync_playwright().start()
298+
playwright.stop()
299+
playwright.stop()
300+
301+
302+
def test_sync_playwright_stop_multiple_times() -> None:
303+
p = multiprocessing.Process(target=_test_sync_playwright_stop_multiple_times)
304+
p.start()
305+
p.join()
306+
assert p.exitcode == 0

0 commit comments

Comments
 (0)