Skip to content

Commit

Permalink
Allow syncronizer to be GC-ed
Browse files Browse the repository at this point in the history
Proper fix for #1525.
Using python's GC module, I've verified that the daemon, when running,
now releases all verifiers, synchronizers and wallets - all the resources
we care about releasing.
  • Loading branch information
Neil Booth committed Nov 11, 2015
1 parent d612684 commit 8cc3b58
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
7 changes: 7 additions & 0 deletions lib/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,13 @@ def process_pending_sends(self):
message_id = self.queue_request(method, params)
self.unanswered_requests[message_id] = method, params, callback

def unsubscribe(self, callback):
'''Unsubscribe a callback to free object references to enable GC.'''
# Note: we can't unsubscribe from the server, so if we receive
# subsequent notifications process_response() will emit a harmless
# "received unexpected notification" warning
self.subscriptions.pop(callback, None)

def connection_down(self, server):
'''A connection to server either went down, or was never made.
We distinguish by whether it is in self.interfaces.'''
Expand Down
3 changes: 3 additions & 0 deletions lib/synchronizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ def is_up_to_date(self):
return (not self.requested_tx and not self.requested_histories
and not self.requested_addrs)

def release(self):
self.network.unsubscribe(self.addr_subscription_response)

def add(self, address):
'''This can be called from the proxy or GUI threads.'''
with self.lock:
Expand Down
5 changes: 4 additions & 1 deletion lib/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from plugins import run_hook
import bitcoin
from synchronizer import Synchronizer
from verifier import SPV
from mnemonic import Mnemonic

import paymentrequest
Expand Down Expand Up @@ -1128,7 +1129,6 @@ def prepare_for_verifier(self):
self.transactions.pop(tx_hash)

def start_threads(self, network):
from verifier import SPV
self.network = network
if self.network is not None:
self.prepare_for_verifier()
Expand All @@ -1142,8 +1142,11 @@ def start_threads(self, network):
def stop_threads(self):
if self.network:
self.network.remove_jobs([self.synchronizer, self.verifier])
self.synchronizer.release()
self.synchronizer = None
self.verifier = None
# Now no references to the syncronizer or verifier
# remain so they will be GC-ed
self.storage.put('stored_height', self.get_local_height(), True)

def wait_until_synchronized(self, callback=None):
Expand Down

0 comments on commit 8cc3b58

Please sign in to comment.