Browse Source

fix a race condition in synchronizer

wallet.synchronizer gets assigned a newly constructed Synchronizer instance.
Synchronizer in tx_response refers to the value of wallet.synchronizer.
If the wallet has a missing txn, there could be a race condition that synchronizer asks for a txn and we get the callback from the network WHILE the constructor is still running, in which case wallet.synchronizer would still be None and we would consider the callback "orphan", and the wallet would get "stuck" synchronizing.
3.2.x
SomberNight 7 years ago
parent
commit
de4fe9db69
No known key found for this signature in database GPG Key ID: B33B5F232C6271E9
  1. 9
      lib/synchronizer.py

9
lib/synchronizer.py

@ -50,6 +50,8 @@ class Synchronizer(ThreadJob):
self.requested_histories = {} self.requested_histories = {}
self.requested_addrs = set() self.requested_addrs = set()
self.lock = Lock() self.lock = Lock()
self.initialized = False
self.initialize() self.initialize()
def parse_response(self, response): def parse_response(self, response):
@ -84,7 +86,7 @@ class Synchronizer(ThreadJob):
return bh2u(hashlib.sha256(status.encode('ascii')).digest()) return bh2u(hashlib.sha256(status.encode('ascii')).digest())
def on_address_status(self, response): def on_address_status(self, response):
if self.wallet.synchronizer is None: if self.wallet.synchronizer is None and self.initialized:
return # we have been killed, this was just an orphan callback return # we have been killed, this was just an orphan callback
params, result = self.parse_response(response) params, result = self.parse_response(response)
if not params: if not params:
@ -100,7 +102,7 @@ class Synchronizer(ThreadJob):
self.requested_addrs.remove(addr) self.requested_addrs.remove(addr)
def on_address_history(self, response): def on_address_history(self, response):
if self.wallet.synchronizer is None: if self.wallet.synchronizer is None and self.initialized:
return # we have been killed, this was just an orphan callback return # we have been killed, this was just an orphan callback
params, result = self.parse_response(response) params, result = self.parse_response(response)
if not params: if not params:
@ -131,7 +133,7 @@ class Synchronizer(ThreadJob):
self.requested_histories.pop(addr) self.requested_histories.pop(addr)
def tx_response(self, response): def tx_response(self, response):
if self.wallet.synchronizer is None: if self.wallet.synchronizer is None and self.initialized:
return # we have been killed, this was just an orphan callback return # we have been killed, this was just an orphan callback
params, result = self.parse_response(response) params, result = self.parse_response(response)
if not params: if not params:
@ -183,6 +185,7 @@ class Synchronizer(ThreadJob):
if self.requested_tx: if self.requested_tx:
self.print_error("missing tx", self.requested_tx) self.print_error("missing tx", self.requested_tx)
self.subscribe_to_addresses(set(self.wallet.get_addresses())) self.subscribe_to_addresses(set(self.wallet.get_addresses()))
self.initialized = True
def run(self): def run(self):
'''Called from the network proxy thread main loop.''' '''Called from the network proxy thread main loop.'''

Loading…
Cancel
Save