Skip to content

Commit

Permalink
fix shutdown after rebuild
Browse files Browse the repository at this point in the history
  • Loading branch information
Ouziel committed Feb 25, 2025
1 parent 35a625c commit 498368a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
17 changes: 13 additions & 4 deletions counterparty-core/counterpartycore/lib/cli/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,8 @@ def run(self):
def stop(self):
logger.info("Stopping Asset Conservation Checker thread...")
self.stop_event.set()
self.db.interrupt()
if self.db is not None:
self.db.interrupt()
self.join()
if self.db is not None:
self.db.close()
Expand All @@ -679,6 +680,7 @@ def __init__(self, args, log_stream=None, stop_when_ready=False):
self.log_stream = log_stream
self.profiler = None
self.stop_when_ready = stop_when_ready
self.stopped = False

# Log all config parameters, sorted by key
# Filter out default values #TODO: these should be set in a different way
Expand Down Expand Up @@ -766,13 +768,14 @@ def run_server(self):
logger.info("Starting profiler before catchup...")
self.profiler = cProfile.Profile()
self.profiler.enable()
blocks.catch_up(self.db, self.api_stop_event)
blocks.catch_up(self.db)
logger.info("Stopping profiler after catchup...")
self.profiler.disable()
else:
blocks.catch_up(self.db, self.api_stop_event)
blocks.catch_up(self.db)

if self.stop_when_ready:
self.stop() # stop here
return

# Blockchain Watcher
Expand All @@ -788,6 +791,8 @@ def run(self):
_thread.interrupt_main()

def stop(self):
if self.stopped:
return
logger.info("Shutting down...")
if self.db:
CurrentState().set_ledger_state(self.db, "Stopping")
Expand Down Expand Up @@ -822,6 +827,7 @@ def stop(self):
logger.error("Error dumping profiler stats: %s", e)
self.profiler = None

self.stopped = True
logger.info("Shutdown complete.")


Expand All @@ -830,7 +836,10 @@ def start_all(args, log_stream=None, stop_when_ready=False):
try:
server.start()
while True:
server.join(1)
if not server.stopped:
server.join(1)
else:
break
except KeyboardInterrupt:
logger.warning("Interruption received. Shutting down...")
finally:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

def test_rebuild():
sh_counterparty_server, _db_file, _api_url = prepare("testnet4")
sh_counterparty_server("rebuild")
sh_counterparty_server("rebuild", "-vv")

0 comments on commit 498368a

Please sign in to comment.