From 511b89ebbea1e5e85879dcf2d0d890a473756ad2 Mon Sep 17 00:00:00 2001
From: Peter Surda <surda@economicsofbitcoin.com>
Date: Sun, 26 Feb 2017 20:44:56 +0100
Subject: [PATCH] PoW Worker should wait for sql thread to init

- I think this was causing freezing queue on launch if not empty
- I can't reproduce the bug anymore
---
 src/class_singleWorker.py | 5 +++++
 src/class_sqlThread.py    | 2 ++
 src/state.py              | 2 ++
 3 files changed, 9 insertions(+)

diff --git a/src/class_singleWorker.py b/src/class_singleWorker.py
index 0190e1c5..87034f7c 100644
--- a/src/class_singleWorker.py
+++ b/src/class_singleWorker.py
@@ -51,6 +51,11 @@ class singleWorker(threading.Thread, StoppableThread):
         super(singleWorker, self).stopThread()
 
     def run(self):
+
+        while not state.sqlReady and state.shutdown == 0:
+            self.stop.wait(2)
+        if state.shutdown > 0:
+            return
         
         # Initialize the neededPubkeys dictionary.
         queryreturn = sqlQuery(
diff --git a/src/class_sqlThread.py b/src/class_sqlThread.py
index b46a570f..9bd7e8e7 100644
--- a/src/class_sqlThread.py
+++ b/src/class_sqlThread.py
@@ -494,6 +494,8 @@ class sqlThread(threading.Thread):
                 parameters = (int(time.time()),)
                 self.cur.execute(item, parameters)
 
+        state.sqlReady = True
+
         while True:
             item = helper_sql.sqlSubmitQueue.get()
             if item == 'commit':
diff --git a/src/state.py b/src/state.py
index 3d5209c4..c7b79e07 100644
--- a/src/state.py
+++ b/src/state.py
@@ -19,6 +19,8 @@ shutdown = 0 #Set to 1 by the doCleanShutdown function. Used to tell the proof o
 
 curses = False
 
+sqlReady = False # set to true by sqlTread when ready for processing
+
 # If the trustedpeer option is specified in keys.dat then this will
 # contain a Peer which will be connected to instead of using the
 # addresses advertised by other peers. The client will only connect to