tsqlite in lnrouter: avoid exceptions on shutdown - electrum - Electrum Bitcoin wallet HTML git clone https://git.parazyd.org/electrum DIR Log DIR Files DIR Refs DIR Submodules --- DIR commit d2d67f1fe14830cc9d2bf34edd869797a639fbee DIR parent dd7c4b3bab7232366bfd6be9e222be7ae3b357ba HTML Author: Janus <ysangkok@gmail.com> Date: Tue, 12 Feb 2019 19:34:14 +0100 sqlite in lnrouter: avoid exceptions on shutdown Diffstat: M electrum/lnrouter.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) --- DIR diff --git a/electrum/lnrouter.py b/electrum/lnrouter.py t@@ -36,7 +36,7 @@ import base64 import asyncio from sqlalchemy import create_engine, Column, ForeignKey, Integer, String, DateTime, Boolean -from sqlalchemy.engine import Engine +from sqlalchemy.pool import StaticPool from sqlalchemy.orm import sessionmaker from sqlalchemy.orm.query import Query from sqlalchemy.ext.declarative import declarative_base t@@ -71,7 +71,6 @@ def validate_features(features : int): Base = declarative_base() session_factory = sessionmaker() DBSession = scoped_session(session_factory) -engine = None FLAG_DISABLE = 1 << 1 FLAG_DIRECTION = 1 << 0 t@@ -262,27 +261,32 @@ class ChannelDB: NUM_MAX_RECENT_PEERS = 20 def __init__(self, network: 'Network'): - global engine self.network = network self.num_nodes = 0 self.num_channels = 0 self.path = os.path.join(get_headers_dir(network.config), 'channel_db.sqlite3') - engine = create_engine('sqlite:///' + self.path)#, echo=True) - DBSession.remove() - DBSession.configure(bind=engine, autoflush=False) - - Base.metadata.drop_all(engine) - Base.metadata.create_all(engine) - - self.lock = threading.RLock() # (intentionally not persisted) self._channel_updates_for_private_channels = {} # type: Dict[Tuple[bytes, bytes], dict] self.ca_verifier = LNChannelVerifier(network, self) + self.network.run_from_another_thread(self.sqlinit()) + + async def sqlinit(self): + """ + this has to run on the async thread since that is where + the lnpeer loop is running from, which will do call in here + """ + engine = create_engine('sqlite:///' + self.path, pool_reset_on_return=None, poolclass=StaticPool)#, echo=True) + DBSession.remove() + DBSession.configure(bind=engine, autoflush=False) + + Base.metadata.drop_all(engine) + Base.metadata.create_all(engine) + def update_counts(self): self.num_channels = DBSession.query(ChannelInfoInDB).count() self.num_nodes = DBSession.query(NodeInfoInDB).count()