[RPC] Fix for non-rpc users (#4143)

* [RPC] Fix for non-rpc users

  - RPC probably needs rewriting.
  - Also, I noticed some extremely sharp edges
  and a potential crash point (unrelated to the fixed issue)
  on windows that the side effects from have been mitigated here
  partially.

* sysexit on initialization failure
This commit is contained in:
Michael H 2020-07-31 20:45:51 -04:00 committed by GitHub
parent bbd08eda3e
commit 3b5183de43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 7 deletions

View File

@ -1370,7 +1370,8 @@ class RedBase(
await super().logout() await super().logout()
await drivers.get_driver_class().teardown() await drivers.get_driver_class().teardown()
try: try:
await self.rpc.close() if self.rpc_enabled:
await self.rpc.close()
except AttributeError: except AttributeError:
pass pass

View File

@ -1,4 +1,5 @@
import asyncio import asyncio
import sys
from typing import Optional from typing import Optional
from aiohttp import web from aiohttp import web
@ -68,23 +69,38 @@ class RPC:
self._runner = web.AppRunner(self.app) self._runner = web.AppRunner(self.app)
self._site: Optional[web.TCPSite] = None self._site: Optional[web.TCPSite] = None
self._started = False
async def initialize(self, port: int): async def initialize(self, port: int):
""" """
Finalizes the initialization of the RPC server and allows it to begin Finalizes the initialization of the RPC server and allows it to begin
accepting queries. accepting queries.
""" """
await self._runner.setup() try:
self._site = web.TCPSite(self._runner, host="127.0.0.1", port=port, shutdown_timeout=0) # This ensures self._started can't be assigned
await self._site.start() # except with both other functions
log.debug("Created RPC server listener on port %s", port) # and isn't subject to a really really stupid but complex
# issue on windows with catching specific
# exceptions related to shutdown conditions in asyncio applications.
self._started, _discard, self._site = (
True,
await self._runner.setup(),
web.TCPSite(self._runner, host="127.0.0.1", port=port, shutdown_timeout=0),
)
except Exception as exc:
log.exception("RPC setup failure", exc_info=exc)
sys.exit(1)
else:
await self._site.start()
log.debug("Created RPC server listener on port %s", port)
async def close(self): async def close(self):
""" """
Closes the RPC server. Closes the RPC server.
""" """
await self.app.shutdown() if self._started:
await self._runner.cleanup() await self.app.shutdown()
await self._runner.cleanup()
def add_method(self, method, prefix: str = None): def add_method(self, method, prefix: str = None):
if prefix is None: if prefix is None: