mirror of
https://github.com/LBRYFoundation/LBRY-Vault.git
synced 2025-08-30 17:01:34 +00:00
interface: catch many common exceptions explicitly
This commit is contained in:
parent
5ef04a039b
commit
0ad504bdf0
3 changed files with 35 additions and 5 deletions
|
@ -36,7 +36,7 @@ from aiorpcx import ClientSession, Notification
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
from .util import PrintError, aiosafe, bfh
|
from .util import PrintError, aiosafe, bfh, AIOSafeSilentException
|
||||||
|
|
||||||
ca_path = requests.certs.where()
|
ca_path = requests.certs.where()
|
||||||
|
|
||||||
|
@ -68,6 +68,10 @@ class NotificationSession(ClientSession):
|
||||||
assert False, request.method
|
assert False, request.method
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class GracefulDisconnect(AIOSafeSilentException): pass
|
||||||
|
|
||||||
|
|
||||||
class Interface(PrintError):
|
class Interface(PrintError):
|
||||||
|
|
||||||
def __init__(self, network, server, config_path, proxy):
|
def __init__(self, network, server, config_path, proxy):
|
||||||
|
@ -134,7 +138,12 @@ class Interface(PrintError):
|
||||||
os.unlink(self.cert_path)
|
os.unlink(self.cert_path)
|
||||||
exists = False
|
exists = False
|
||||||
if not exists:
|
if not exists:
|
||||||
ca_signed = await self.is_server_ca_signed(ca_sslc)
|
try:
|
||||||
|
ca_signed = await self.is_server_ca_signed(ca_sslc)
|
||||||
|
except (ConnectionRefusedError, socket.gaierror) as e:
|
||||||
|
self.print_error('disconnecting due to: {}'.format(e))
|
||||||
|
self.exception = e
|
||||||
|
return
|
||||||
if ca_signed:
|
if ca_signed:
|
||||||
with open(self.cert_path, 'w') as f:
|
with open(self.cert_path, 'w') as f:
|
||||||
# empty file means this is CA signed, not self-signed
|
# empty file means this is CA signed, not self-signed
|
||||||
|
@ -147,7 +156,13 @@ class Interface(PrintError):
|
||||||
else:
|
else:
|
||||||
sslc = ssl.create_default_context(ssl.Purpose.SERVER_AUTH, cafile=self.cert_path)
|
sslc = ssl.create_default_context(ssl.Purpose.SERVER_AUTH, cafile=self.cert_path)
|
||||||
sslc.check_hostname = 0
|
sslc.check_hostname = 0
|
||||||
await self.open_session(sslc, exit_early=False)
|
try:
|
||||||
|
await self.open_session(sslc, exit_early=False)
|
||||||
|
except (asyncio.CancelledError, ConnectionRefusedError, socket.gaierror, ssl.SSLError, TimeoutError) as e:
|
||||||
|
self.print_error('disconnecting due to: {}'.format(e))
|
||||||
|
self.exception = e
|
||||||
|
return
|
||||||
|
# should never get here (can only exit via exception)
|
||||||
assert False
|
assert False
|
||||||
|
|
||||||
def mark_ready(self):
|
def mark_ready(self):
|
||||||
|
@ -204,7 +219,10 @@ class Interface(PrintError):
|
||||||
header_queue = asyncio.Queue()
|
header_queue = asyncio.Queue()
|
||||||
self.session = NotificationSession(None, header_queue, self.host, self.port, ssl=sslc, proxy=self.proxy)
|
self.session = NotificationSession(None, header_queue, self.host, self.port, ssl=sslc, proxy=self.proxy)
|
||||||
async with self.session as session:
|
async with self.session as session:
|
||||||
ver = await session.send_request('server.version', [ELECTRUM_VERSION, PROTOCOL_VERSION])
|
try:
|
||||||
|
ver = await session.send_request('server.version', [ELECTRUM_VERSION, PROTOCOL_VERSION])
|
||||||
|
except aiorpcx.jsonrpc.RPCError as e:
|
||||||
|
raise GracefulDisconnect(e) # probably 'unsupported protocol version'
|
||||||
if exit_early:
|
if exit_early:
|
||||||
return
|
return
|
||||||
self.print_error(ver, self.host)
|
self.print_error(ver, self.host)
|
||||||
|
@ -228,6 +246,9 @@ class Interface(PrintError):
|
||||||
|
|
||||||
@aiosafe
|
@aiosafe
|
||||||
async def run_fetch_blocks(self, sub_reply, replies):
|
async def run_fetch_blocks(self, sub_reply, replies):
|
||||||
|
if self.tip < self.network.max_checkpoint():
|
||||||
|
raise GracefulDisconnect('server tip below max checkpoint')
|
||||||
|
|
||||||
async with self.network.bhi_lock:
|
async with self.network.bhi_lock:
|
||||||
height = self.blockchain.height()+1
|
height = self.blockchain.height()+1
|
||||||
await replies.put(blockchain.deserialize_header(bfh(sub_reply['hex']), sub_reply['height']))
|
await replies.put(blockchain.deserialize_header(bfh(sub_reply['hex']), sub_reply['height']))
|
||||||
|
|
|
@ -817,7 +817,7 @@ class Network(PrintError):
|
||||||
try:
|
try:
|
||||||
raise i.exception
|
raise i.exception
|
||||||
except BaseException as e:
|
except BaseException as e:
|
||||||
self.print_error(i.server, "errored because", str(e), str(type(e)))
|
self.print_error(i.server, "errored because:", str(e), str(type(e)))
|
||||||
remove.append(k)
|
remove.append(k)
|
||||||
changed = False
|
changed = False
|
||||||
for k in remove:
|
for k in remove:
|
||||||
|
|
|
@ -34,6 +34,7 @@ import hmac
|
||||||
import stat
|
import stat
|
||||||
import inspect
|
import inspect
|
||||||
from locale import localeconv
|
from locale import localeconv
|
||||||
|
import asyncio
|
||||||
|
|
||||||
from .i18n import _
|
from .i18n import _
|
||||||
|
|
||||||
|
@ -925,6 +926,10 @@ def make_dir(path, allow_symlink=True):
|
||||||
os.mkdir(path)
|
os.mkdir(path)
|
||||||
os.chmod(path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
|
os.chmod(path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
|
||||||
|
|
||||||
|
|
||||||
|
class AIOSafeSilentException(Exception): pass
|
||||||
|
|
||||||
|
|
||||||
def aiosafe(f):
|
def aiosafe(f):
|
||||||
# save exception in object.
|
# save exception in object.
|
||||||
# f must be a method of a PrintError instance.
|
# f must be a method of a PrintError instance.
|
||||||
|
@ -933,6 +938,10 @@ def aiosafe(f):
|
||||||
self = args[0]
|
self = args[0]
|
||||||
try:
|
try:
|
||||||
return await f(*args, **kwargs)
|
return await f(*args, **kwargs)
|
||||||
|
except AIOSafeSilentException as e:
|
||||||
|
self.exception = e
|
||||||
|
except asyncio.CancelledError as e:
|
||||||
|
self.exception = e
|
||||||
except BaseException as e:
|
except BaseException as e:
|
||||||
self.print_error("Exception in", f.__name__, ":", e.__class__.__name__, str(e))
|
self.print_error("Exception in", f.__name__, ":", e.__class__.__name__, str(e))
|
||||||
traceback.print_exc(file=sys.stderr)
|
traceback.print_exc(file=sys.stderr)
|
||||||
|
|
Loading…
Add table
Reference in a new issue