Skip to content

Commit fdaf1c1

Browse files
committed
Multiple attempts to create context, add lock
1 parent e48b509 commit fdaf1c1

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/crawlee/browsers/_playwright_browser_controller.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from __future__ import annotations
44

5+
from asyncio import Lock
56
from datetime import datetime, timedelta, timezone
67
from typing import TYPE_CHECKING, Any, cast
78

@@ -77,6 +78,29 @@ def __init__(
7778

7879
self._total_opened_pages = 0
7980

81+
self._context_creation_lock: Lock | None = None
82+
83+
84+
async def _get_context_creation_lock(self) -> Lock:
85+
"""Context checking and creation should be done with lock to prevent multiple attempts to create context."""
86+
if self._context_creation_lock:
87+
return self._context_creation_lock
88+
self._context_creation_lock= Lock()
89+
return self._context_creation_lock
90+
91+
async def ensure_browser_context(self,
92+
browser_new_context_options: Mapping[str, Any] | None = None,
93+
proxy_info: ProxyInfo | None = None,
94+
) -> None:
95+
async with await self._get_context_creation_lock():
96+
if not self._browser_context:
97+
self._browser_context = await self._create_browser_context(
98+
browser_new_context_options=browser_new_context_options,
99+
proxy_info=proxy_info,
100+
)
101+
102+
103+
80104
@property
81105
@override
82106
def pages(self) -> list[Page]:

0 commit comments

Comments
 (0)