@@ -41,8 +41,10 @@ def _get_stderr_fileno() -> Optional[int]:
4141
4242
4343class Transport (ABC ):
44- def __init__ (self ) -> None :
44+ def __init__ (self , loop : asyncio .AbstractEventLoop ) -> None :
45+ self ._loop = loop
4546 self .on_message = lambda _ : None
47+ self .on_error_future : asyncio .Future = loop .create_future ()
4648
4749 @abstractmethod
4850 def request_stop (self ) -> None :
@@ -55,9 +57,9 @@ def dispose(self) -> None:
5557 async def wait_until_stopped (self ) -> None :
5658 pass
5759
60+ @abstractmethod
5861 async def run (self ) -> None :
59- self ._loop = asyncio .get_running_loop ()
60- self .on_error_future : asyncio .Future = asyncio .Future ()
62+ pass
6163
6264 @abstractmethod
6365 def send (self , message : Dict ) -> None :
@@ -78,11 +80,12 @@ def deserialize_message(self, data: bytes) -> Any:
7880
7981
8082class PipeTransport (Transport ):
81- def __init__ (self , driver_executable : Path ) -> None :
82- super ().__init__ ()
83+ def __init__ (
84+ self , loop : asyncio .AbstractEventLoop , driver_executable : Path
85+ ) -> None :
86+ super ().__init__ (loop )
8387 self ._stopped = False
8488 self ._driver_executable = driver_executable
85- self ._loop : asyncio .AbstractEventLoop
8689
8790 def request_stop (self ) -> None :
8891 self ._stopped = True
@@ -93,17 +96,21 @@ async def wait_until_stopped(self) -> None:
9396 await self ._proc .wait ()
9497
9598 async def run (self ) -> None :
96- await super ().run ()
9799 self ._stopped_future : asyncio .Future = asyncio .Future ()
98100
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- )
101+ try :
102+ self ._proc = proc = await asyncio .create_subprocess_exec (
103+ str (self ._driver_executable ),
104+ "run-driver" ,
105+ stdin = asyncio .subprocess .PIPE ,
106+ stdout = asyncio .subprocess .PIPE ,
107+ stderr = _get_stderr_fileno (),
108+ limit = 32768 ,
109+ )
110+ except Exception as exc :
111+ self .on_error_future .set_exception (exc )
112+ return
113+
107114 assert proc .stdout
108115 assert proc .stdin
109116 self ._output = proc .stdin
@@ -138,16 +145,17 @@ def send(self, message: Dict) -> None:
138145
139146class WebSocketTransport (AsyncIOEventEmitter , Transport ):
140147 def __init__ (
141- self , ws_endpoint : str , timeout : float = None , headers : Dict [str , str ] = None
148+ self ,
149+ loop : asyncio .AbstractEventLoop ,
150+ ws_endpoint : str ,
151+ headers : Dict [str , str ] = None ,
142152 ) -> None :
143- super ().__init__ ()
144- Transport .__init__ (self )
153+ super ().__init__ (loop )
154+ Transport .__init__ (self , loop )
145155
146156 self ._stopped = False
147157 self .ws_endpoint = ws_endpoint
148- self .timeout = timeout
149158 self .headers = headers
150- self ._loop : asyncio .AbstractEventLoop
151159
152160 def request_stop (self ) -> None :
153161 self ._stopped = True
@@ -160,15 +168,13 @@ async def wait_until_stopped(self) -> None:
160168 await self ._connection .wait_closed ()
161169
162170 async def run (self ) -> None :
163- await super ().run ()
164-
165- options : Dict [str , Any ] = {}
166- if self .timeout is not None :
167- options ["close_timeout" ] = self .timeout / 1000
168- options ["ping_timeout" ] = self .timeout / 1000
169- if self .headers is not None :
170- options ["extra_headers" ] = self .headers
171- self ._connection = await websockets .connect (self .ws_endpoint , ** options )
171+ try :
172+ self ._connection = await websockets .connect (
173+ self .ws_endpoint , extra_headers = self .headers
174+ )
175+ except Exception as exc :
176+ self .on_error_future .set_exception (Error (f"websocket.connect: { str (exc )} " ))
177+ return
172178
173179 while not self ._stopped :
174180 try :
@@ -188,8 +194,8 @@ async def run(self) -> None:
188194 )
189195 break
190196 except Exception as exc :
191- print (f"Received unhandled exception: { exc } " )
192197 self .on_error_future .set_exception (exc )
198+ break
193199
194200 def send (self , message : Dict ) -> None :
195201 if self ._stopped or self ._connection .closed :
0 commit comments