Skip to content

Commit 034c221

Browse files
authored
Revert "fix(transport): handle connection error correctly (microsoft#650) (microsoft#651)" (microsoft#670)
This reverts commit 82885a1. Sorry about the revert. I don't think this handles the connection timeout properly, so let me see if we can get a better fix here.
1 parent 53cf799 commit 034c221

File tree

5 files changed

+16
-86
lines changed

5 files changed

+16
-86
lines changed

playwright/_impl/_browser_type.py

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

15-
import asyncio
1615
from pathlib import Path
1716
from typing import Dict, List, Optional, Union, cast
1817

@@ -180,24 +179,11 @@ async def connect(
180179
self._connection._object_factory,
181180
transport,
182181
)
183-
await connection._transport.start()
184182
connection._is_sync = self._connection._is_sync
185183
connection._loop = self._connection._loop
186184
connection._loop.create_task(connection.run())
187-
obj = asyncio.create_task(
188-
connection.wait_for_object_with_known_name("Playwright")
189-
)
190-
done, pending = await asyncio.wait(
191-
{
192-
obj,
193-
connection._transport.on_error_future, # type: ignore
194-
},
195-
return_when=asyncio.FIRST_COMPLETED,
196-
)
197-
if not obj.done():
198-
obj.cancel()
199-
playwright = next(iter(done)).result()
200185
self._connection._child_ws_connections.append(connection)
186+
playwright = await connection.wait_for_object_with_known_name("Playwright")
201187
pre_launched_browser = playwright._initializer.get("preLaunchedBrowser")
202188
assert pre_launched_browser
203189
browser = cast(Browser, from_channel(pre_launched_browser))

playwright/_impl/_transport.py

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ def _get_stderr_fileno() -> Optional[int]:
4242

4343
class Transport(ABC):
4444
def __init__(self) -> None:
45-
self.on_error_future: asyncio.Future
4645
self.on_message = lambda _: None
4746

4847
@abstractmethod
@@ -56,14 +55,9 @@ def dispose(self) -> None:
5655
async def wait_until_stopped(self) -> None:
5756
pass
5857

59-
async def start(self) -> None:
60-
if not hasattr(self, "on_error_future"):
61-
self.on_error_future = asyncio.Future()
62-
self._loop = asyncio.get_running_loop()
63-
64-
@abstractmethod
6558
async def run(self) -> None:
66-
pass
59+
self._loop = asyncio.get_running_loop()
60+
self.on_error_future: asyncio.Future = asyncio.Future()
6761

6862
@abstractmethod
6963
def send(self, message: Dict) -> None:
@@ -99,28 +93,17 @@ async def wait_until_stopped(self) -> None:
9993
await self._proc.wait()
10094

10195
async def run(self) -> None:
102-
await self.start()
96+
await super().run()
10397
self._stopped_future: asyncio.Future = asyncio.Future()
10498

105-
try:
106-
self._proc = proc = await asyncio.create_subprocess_exec(
107-
str(self._driver_executable),
108-
"run-driver",
109-
stdin=asyncio.subprocess.PIPE,
110-
stdout=asyncio.subprocess.PIPE,
111-
stderr=_get_stderr_fileno(),
112-
limit=32768,
113-
)
114-
except FileNotFoundError:
115-
self.on_error_future.set_exception(
116-
Error(
117-
"playwright's driver is not found, You can read the contributing guide "
118-
"for some guidance on how to get everything setup for working on the code "
119-
"https://github.com/microsoft/playwright-python/blob/master/CONTRIBUTING.md"
120-
)
121-
)
122-
return
123-
99+
self._proc = proc = await asyncio.create_subprocess_exec(
100+
str(self._driver_executable),
101+
"run-driver",
102+
stdin=asyncio.subprocess.PIPE,
103+
stdout=asyncio.subprocess.PIPE,
104+
stderr=_get_stderr_fileno(),
105+
limit=32768,
106+
)
124107
assert proc.stdout
125108
assert proc.stdin
126109
self._output = proc.stdin
@@ -177,22 +160,15 @@ async def wait_until_stopped(self) -> None:
177160
await self._connection.wait_closed()
178161

179162
async def run(self) -> None:
180-
await self.start()
163+
await super().run()
181164

182165
options: Dict[str, Any] = {}
183166
if self.timeout is not None:
184167
options["close_timeout"] = self.timeout / 1000
185168
options["ping_timeout"] = self.timeout / 1000
186-
187169
if self.headers is not None:
188170
options["extra_headers"] = self.headers
189-
try:
190-
self._connection = await websockets.connect(self.ws_endpoint, **options)
191-
except Exception as err:
192-
self.on_error_future.set_exception(
193-
Error(f"playwright's websocket endpoint connection error: {err}")
194-
)
195-
return
171+
self._connection = await websockets.connect(self.ws_endpoint, **options)
196172

197173
while not self._stopped:
198174
try:

playwright/async_api/_context_manager.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,10 @@ async def __aenter__(self) -> AsyncPlaywright:
3232
)
3333
loop = asyncio.get_running_loop()
3434
self._connection._loop = loop
35-
obj = asyncio.create_task(
36-
self._connection.wait_for_object_with_known_name("Playwright")
37-
)
38-
await self._connection._transport.start()
3935
loop.create_task(self._connection.run())
40-
done, pending = await asyncio.wait(
41-
{
42-
obj,
43-
self._connection._transport.on_error_future, # type: ignore
44-
},
45-
return_when=asyncio.FIRST_COMPLETED,
36+
playwright = AsyncPlaywright(
37+
await self._connection.wait_for_object_with_known_name("Playwright")
4638
)
47-
if not obj.done():
48-
obj.cancel()
49-
obj = next(iter(done)).result()
50-
playwright = AsyncPlaywright(obj) # type: ignore
5139
playwright.stop = self.__aexit__ # type: ignore
5240
return playwright
5341

tests/async/test_browsertype_connect.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,3 @@ async def test_prevent_getting_video_path(
182182
== "Path is not available when using browserType.connect(). Use save_as() to save a local copy."
183183
)
184184
remote_server.kill()
185-
186-
187-
async def test_connect_to_closed_server_without_hangs(
188-
browser_type: BrowserType, launch_server
189-
):
190-
remote_server = launch_server()
191-
remote_server.kill()
192-
with pytest.raises(Error) as exc:
193-
await browser_type.connect(remote_server.ws_endpoint)
194-
assert "playwright's websocket endpoint connection error" in exc.value.message

tests/sync/test_browsertype_connect.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,3 @@ def test_browser_type_connect_should_forward_close_events_to_pages(
145145
assert events == ["page::close", "context::close", "browser::disconnected"]
146146
remote.kill()
147147
assert events == ["page::close", "context::close", "browser::disconnected"]
148-
149-
150-
def test_connect_to_closed_server_without_hangs(
151-
browser_type: BrowserType, launch_server
152-
):
153-
remote_server = launch_server()
154-
remote_server.kill()
155-
with pytest.raises(Error) as exc:
156-
browser_type.connect(remote_server.ws_endpoint)
157-
assert "playwright's websocket endpoint connection error" in exc.value.message

0 commit comments

Comments
 (0)