use locks when accessing dictionary inventory

This commit is contained in:
Jonathan Warren 2013-09-07 18:23:20 -04:00
parent 831edf0d24
commit f0bf3aad48
3 changed files with 29 additions and 19 deletions

View File

@ -1,5 +1,5 @@
# objectHashHolder is a timer-driven thread. One objectHashHolder thread is used # objectHashHolder is a timer-driven thread. One objectHashHolder thread is used
# by each sendDataThread. It uses it whenever a sendDataThread needs to # by each sendDataThread. The sendDataThread uses it whenever it needs to
# advertise an object to peers. Instead of sending it out immediately, it must # advertise an object to peers. Instead of sending it out immediately, it must
# wait a random number of seconds for each connection so that different peers # wait a random number of seconds for each connection so that different peers
# get different objects at different times. Thus an attacker who is # get different objects at different times. Thus an attacker who is

View File

@ -298,6 +298,7 @@ class receiveDataThread(threading.Thread):
bigInvList[hash] = 0 bigInvList[hash] = 0
# We also have messages in our inventory in memory (which is a python # We also have messages in our inventory in memory (which is a python
# dictionary). Let's fetch those too. # dictionary). Let's fetch those too.
with shared.inventoryLock:
for hash, storedValue in shared.inventory.items(): for hash, storedValue in shared.inventory.items():
if hash not in self.someObjectsOfWhichThisRemoteNodeIsAlreadyAware: if hash not in self.someObjectsOfWhichThisRemoteNodeIsAlreadyAware:
objectType, streamNumber, payload, receivedTime = storedValue objectType, streamNumber, payload, receivedTime = storedValue
@ -1496,11 +1497,14 @@ class receiveDataThread(threading.Thread):
print 'received getdata request for item:', hash.encode('hex') print 'received getdata request for item:', hash.encode('hex')
shared.numberOfInventoryLookupsPerformed += 1 shared.numberOfInventoryLookupsPerformed += 1
shared.inventoryLock.acquire()
if hash in shared.inventory: if hash in shared.inventory:
objectType, streamNumber, payload, receivedTime = shared.inventory[ objectType, streamNumber, payload, receivedTime = shared.inventory[
hash] hash]
shared.inventoryLock.release()
self.sendData(objectType, payload) self.sendData(objectType, payload)
else: else:
shared.inventoryLock.release()
queryreturn = sqlQuery( queryreturn = sqlQuery(
'''select objecttype, payload from inventory where hash=?''', '''select objecttype, payload from inventory where hash=?''',
hash) hash)

View File

@ -32,6 +32,7 @@ class singleCleaner(threading.Thread):
shared.UISignalQueue.put(( shared.UISignalQueue.put((
'updateStatusBar', 'Doing housekeeping (Flushing inventory in memory to disk...)')) 'updateStatusBar', 'Doing housekeeping (Flushing inventory in memory to disk...)'))
with shared.inventoryLock: # If you use both the inventoryLock and the sqlLock, always use the inventoryLock OUTSIDE of the sqlLock.
with SqlBulkExecute() as sql: with SqlBulkExecute() as sql:
for hash, storedValue in shared.inventory.items(): for hash, storedValue in shared.inventory.items():
objectType, streamNumber, payload, receivedTime = storedValue objectType, streamNumber, payload, receivedTime = storedValue
@ -118,4 +119,9 @@ class singleCleaner(threading.Thread):
queryData = sqlQuery('''SELECT hash FROM inventory WHERE streamnumber=?''', streamNumber) queryData = sqlQuery('''SELECT hash FROM inventory WHERE streamnumber=?''', streamNumber)
for row in queryData: for row in queryData:
shared.inventorySets[streamNumber].add(row[0]) shared.inventorySets[streamNumber].add(row[0])
with shared.inventoryLock:
for hash, storedValue in shared.inventory.items():
objectType, streamNumber, payload, receivedTime = storedValue
if streamNumber in shared.inventorySets:
shared.inventorySets[streamNumber].add(hash)
time.sleep(300) time.sleep(300)