Avoid reentrant error in signal_handler.

This commit is contained in:
tecnovert 2025-01-30 15:20:37 +02:00
parent c7818f5fac
commit a0456cb689
No known key found for this signature in database
GPG key ID: 8ED6D8750C4E3F93
4 changed files with 33 additions and 24 deletions

View file

@ -489,7 +489,7 @@ class BasicSwap(BaseApp):
random.seed(secrets.randbits(128)) random.seed(secrets.randbits(128))
def finalise(self): def finalise(self):
self.log.info("Finalise") self.log.info("Finalising")
with self.mxDB: with self.mxDB:
self.delay_event.set() self.delay_event.set()
@ -1136,9 +1136,8 @@ class BasicSwap(BaseApp):
self.log.error( self.log.error(
"No wallets found for coin {}.".format(ci.coin_name()) "No wallets found for coin {}.".format(ci.coin_name())
) )
self.stopRunning( # systemd will try to restart the process if fail_code != 0
1 self.stopRunning(1)
) # systemd will try to restart the process if fail_code != 0
startup_tries = self.startup_tries startup_tries = self.startup_tries
chain_client_settings = self.getChainClientSettings(coin_type) chain_client_settings = self.getChainClientSettings(coin_type)

View file

@ -1990,7 +1990,7 @@ def load_config(config_path):
def signal_handler(sig, frame): def signal_handler(sig, frame):
logger.info(f"Signal {sig} detected") os.write(sys.stdout.fileno(), f"Signal {sig} detected.\n".encode("utf-8"))
def check_btc_fastsync_data(base_dir, sync_filename): def check_btc_fastsync_data(base_dir, sync_filename):

View file

@ -6,14 +6,14 @@
# Distributed under the MIT software license, see the accompanying # Distributed under the MIT software license, see the accompanying
# file LICENSE or http://www.opensource.org/licenses/mit-license.php. # file LICENSE or http://www.opensource.org/licenses/mit-license.php.
import os
import sys
import json import json
import logging
import os
import shutil import shutil
import signal import signal
import logging
import traceback
import subprocess import subprocess
import sys
import traceback
import basicswap.config as cfg import basicswap.config as cfg
from basicswap import __version__ from basicswap import __version__
@ -24,10 +24,11 @@ from basicswap.http_server import HttpThread
from basicswap.contrib.websocket_server import WebsocketServer from basicswap.contrib.websocket_server import WebsocketServer
logger = logging.getLogger() initial_logger = logging.getLogger()
logger.level = logging.DEBUG initial_logger.level = logging.DEBUG
if not len(logger.handlers): if not len(initial_logger.handlers):
logger.addHandler(logging.StreamHandler(sys.stdout)) initial_logger.addHandler(initial_logger.StreamHandler(sys.stdout))
logger = initial_logger
swap_client = None swap_client = None
@ -48,9 +49,10 @@ def is_known_coin(coin_name: str) -> bool:
def signal_handler(sig, frame): def signal_handler(sig, frame):
global swap_client os.write(
logger.info("Signal %d detected, ending program." % (sig)) sys.stdout.fileno(), f"Signal {sig} detected, ending program.\n".encode("utf-8")
if swap_client is not None: )
if swap_client is not None and not swap_client.chainstate_delay_event.is_set():
swap_client.stopRunning() swap_client.stopRunning()
@ -533,7 +535,7 @@ def runClient(fp, data_dir, chain, start_only_coins):
signal.CTRL_C_EVENT if os.name == "nt" else signal.SIGINT signal.CTRL_C_EVENT if os.name == "nt" else signal.SIGINT
) )
except Exception as e: except Exception as e:
swap_client.log.info("Interrupting %d, error %s", d.handle.pid, str(e)) swap_client.log.info(f"Interrupting {d.handle.pid}, error {e}")
for d in daemons: for d in daemons:
try: try:
d.handle.wait(timeout=120) d.handle.wait(timeout=120)
@ -541,8 +543,8 @@ def runClient(fp, data_dir, chain, start_only_coins):
if fp: if fp:
fp.close() fp.close()
closed_pids.append(d.handle.pid) closed_pids.append(d.handle.pid)
except Exception as ex: except Exception as e:
swap_client.log.error("Error: {}".format(ex)) swap_client.log.error(f"Error: {e}")
if os.path.exists(pids_path): if os.path.exists(pids_path):
with open(pids_path) as fd: with open(pids_path) as fd:

View file

@ -149,7 +149,9 @@ def page_bid(self, url_split, post_string):
) )
def page_bids(self, url_split, post_string, sent=False, available=False, received=False): def page_bids(
self, url_split, post_string, sent=False, available=False, received=False
):
server = self.server server = self.server
swap_client = server.swap_client swap_client = server.swap_client
swap_client.checkSystemStatus() swap_client.checkSystemStatus()
@ -220,13 +222,19 @@ def page_bids(self, url_split, post_string, sent=False, available=False, receive
"summary": summary, "summary": summary,
"filter_key": filter_key, "filter_key": filter_key,
"bids": [ "bids": [
(format_timestamp(b[0]), b[2].hex(), b[3].hex(), (
strBidState(b[5]), strTxState(b[7]), format_timestamp(b[0]),
strTxState(b[8]), b[11]) b[2].hex(),
b[3].hex(),
strBidState(b[5]),
strTxState(b[7]),
strTxState(b[8]),
b[11],
)
for b in bids for b in bids
], ],
"bids_count": len(bids), "bids_count": len(bids),
} },
) )
sent_bids = swap_client.listBids(sent=True, filters=filters) sent_bids = swap_client.listBids(sent=True, filters=filters)