(self)
| 202 | self.proxy._close_connection(self) |
| 203 | |
| 204 | async def handle(self): |
| 205 | try: |
| 206 | if not await self.prepare(): |
| 207 | return |
| 208 | |
| 209 | self.proxy_to_backend_task = asyncio.ensure_future( |
| 210 | self.proxy_to_backend()) |
| 211 | self.proxy_from_backend_task = asyncio.ensure_future( |
| 212 | self.proxy_from_backend()) |
| 213 | |
| 214 | await asyncio.wait( |
| 215 | [self.proxy_to_backend_task, self.proxy_from_backend_task], |
| 216 | return_when=asyncio.FIRST_COMPLETED) |
| 217 | |
| 218 | except ConnectionError: |
| 219 | pass |
| 220 | |
| 221 | finally: |
| 222 | # Relay completion can schedule close() while cleanup is awaiting |
| 223 | # its children. Do not let that callback cancel cleanup itself. |
| 224 | self.is_closed = True |
| 225 | if self.proxy_to_backend_task is not None: |
| 226 | self.proxy_to_backend_task.cancel() |
| 227 | |
| 228 | if self.proxy_from_backend_task is not None: |
| 229 | self.proxy_from_backend_task.cancel() |
| 230 | |
| 231 | await asyncio.gather(*( |
| 232 | task for task in (self.proxy_to_backend_task, |
| 233 | self.proxy_from_backend_task) |
| 234 | if task is not None |
| 235 | ), return_exceptions=True) |
| 236 | |
| 237 | # Asyncio fails to properly remove the readers and writers |
| 238 | # when the task doing recv() or send() is cancelled, so |
| 239 | # we must remove the readers and writers manually before |
| 240 | # closing the sockets. |
| 241 | sockets = [self.backend_sock] |
| 242 | if self.client_writer is None: |
| 243 | sockets.append(self.client_sock) |
| 244 | else: |
| 245 | self.client_writer.close() |
| 246 | with contextlib.suppress(ConnectionError): |
| 247 | await self.client_writer.wait_closed() |
| 248 | for sock in sockets: |
| 249 | if sock.fileno() < 0: |
| 250 | continue |
| 251 | # ProactorEventLoop has no reader/writer registration API. |
| 252 | with contextlib.suppress(NotImplementedError): |
| 253 | self.loop.remove_reader(sock.fileno()) |
| 254 | self.loop.remove_writer(sock.fileno()) |
| 255 | sock.close() |
| 256 | self.proxy.connections.pop(self, None) |
| 257 | |
| 258 | async def _read(self, sock, n): |
| 259 | if sock is self.client_sock and self.client_reader is not None: |
no test coverage detected