Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new --exit-on-disconnect flag to start lbrynet, will exit process when connection drops #3646

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lbry/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,9 @@ class TranscodeConfig(BaseConfig):
class CLIConfig(TranscodeConfig):

api = String('Host name and port for lbrynet daemon API.', 'localhost:5279', metavar='HOST:PORT')
exit_on_disconnect = Toggle(
'Shutdown daemon when connection to wallet server closes.', False
)

@property
def api_connection_url(self) -> str:
Expand Down
4 changes: 3 additions & 1 deletion lbry/wallet/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ async def from_lbrynet_config(cls, config: Config):
'jurisdiction': config.jurisdiction,
'concurrent_hub_requests': config.concurrent_hub_requests,
'data_path': config.wallet_dir,
'tx_cache_size': config.transaction_cache_size
'tx_cache_size': config.transaction_cache_size,
'exit_on_disconnect': config.exit_on_disconnect,
}
if 'LBRY_FEE_PER_NAME_CHAR' in os.environ:
ledger_config['fee_per_name_char'] = int(os.environ.get('LBRY_FEE_PER_NAME_CHAR'))
Expand Down Expand Up @@ -244,6 +245,7 @@ async def reset(self):
'hub_timeout': self.config.hub_timeout,
'concurrent_hub_requests': self.config.concurrent_hub_requests,
'data_path': self.config.wallet_dir,
'exit_on_disconnect': self.config.exit_on_disconnect,
}
if Config.lbryum_servers.is_set(self.config):
self.ledger.config['explicit_servers'] = self.config.lbryum_servers
Expand Down
22 changes: 20 additions & 2 deletions lbry/wallet/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import json
import socket
import random
import sys
from time import perf_counter
from collections import defaultdict
from typing import Dict, Optional, Tuple
Expand Down Expand Up @@ -197,6 +198,10 @@ def known_hubs(self):
def jurisdiction(self):
return self.config.get("jurisdiction")

@property
def exit_on_disconnect(self):
return self.config["exit_on_disconnect"]

def disconnect(self):
if self._keepalive_task and not self._keepalive_task.done():
self._keepalive_task.cancel()
Expand Down Expand Up @@ -373,7 +378,13 @@ def is_connected(self):
def rpc(self, list_or_method, args, restricted=True, session: Optional[ClientSession] = None):
if session or self.is_connected:
session = session or self.client
return session.send_request(list_or_method, args)
try:
return session.send_request(list_or_method, args)
except asyncio.TimeoutError:
if self.exit_on_disconnect:
log.error("exiting on server disconnect")
sys.exit(1)
raise
else:
self._urgent_need_reconnect.set()
raise ConnectionError("Attempting to send rpc request when connection is not available.")
Expand All @@ -387,9 +398,16 @@ async def retriable_call(self, function, *args, **kwargs):
try:
return await function(*args, **kwargs)
except asyncio.TimeoutError:
log.warning("Wallet server call timed out, retrying.")
if self.exit_on_disconnect:
log.error("Wallet server call timed out, exiting on server disconnect.")
sys.exit(1)
else:
log.warning("Wallet server call timed out, retrying.")
except ConnectionError:
log.warning("connection error")
if self.exit_on_disconnect:
log.error("exiting on server disconnect")
sys.exit(1)
Comment on lines +409 to +410
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should consider adding SystemExit exception handling here:

except (GracefulExit, KeyboardInterrupt, asyncio.CancelledError):

I guess the risk is that it would hang while shutting down components, but calling daemon.stop() is done in other scenarios like SIGINT, SIGTERM.

raise asyncio.CancelledError() # if we got here, we are shutting down

def _update_remote_height(self, header_args):
Expand Down