From 1f55037640f7cc19fbce66ef02a9d4792c438ce3 Mon Sep 17 00:00:00 2001
From: Janus <ysangkok@gmail.com>
Date: Thu, 15 Mar 2018 18:43:10 +0100
Subject: [PATCH] lightning: less junk on console, quicker shutdown

---
 electrum/gui/qt/main_window.py | 2 +-
 lib/lightning.py               | 9 ++++-----
 2 files changed, 5 insertions(+), 6 deletions(-)

diff --git a/electrum/gui/qt/main_window.py b/electrum/gui/qt/main_window.py
index 4ad53b328..43fa0e2c8 100644
--- a/electrum/gui/qt/main_window.py
+++ b/electrum/gui/qt/main_window.py
@@ -2078,7 +2078,7 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger):
             'wallet': self.wallet,
             'network': self.network,
             'plugins': self.gui_object.plugins,
-            'lightning': self.gui_object.lightning,
+            'l': self.gui_object.lightning,
             'window': self,
             'config': self.config,
             'electrum': electrum,
diff --git a/lib/lightning.py b/lib/lightning.py
index 463b69539..65b93b6cc 100644
--- a/lib/lightning.py
+++ b/lib/lightning.py
@@ -598,7 +598,7 @@ class LightningRPC:
         self.subscribers = []
     # overridden
     async def run(self, netAndWalLock):
-      while True:
+      while asyncio.get_event_loop().is_running():
         try:
             qitem = self.queue.get(block=False)
         except queue.Empty:
@@ -681,7 +681,7 @@ class LightningWorker:
 
         wasAlreadyUpToDate = False
 
-        while True:
+        while asyncio.get_event_loop().is_running():
             WALLET = self.wallet()
             NETWORK = self.network()
             CONFIG = self.config()
@@ -704,7 +704,7 @@ class LightningWorker:
                 writer.write(b"MAGIC")
                 writer.write(privateKeyHash[:6])
                 await asyncio.wait_for(writer.drain(), 5)
-                while True:
+                while asyncio.get_event_loop().is_running():
                     obj = await readJson(reader)
                     if not obj: continue
                     if "id" not in obj:
@@ -721,13 +721,12 @@ class LightningWorker:
 
 async def readJson(reader):
     data = b""
-    while True:
+    while asyncio.get_event_loop().is_running():
       newlines = sum(1 if x == b"\n"[0] else 0 for x in data)
       if newlines > 1: print("Too many newlines in Electrum/lightning.py!", data)
       try:
         return json.loads(data)
       except ValueError:
-        if data != b"": print("parse failed, data has", data)
         try:
             data += await asyncio.wait_for(reader.read(1), 1)
         except TimeoutError: