use sock.shutdown() before sock.close()

This commit is contained in:
Jonathan Warren 2013-04-29 12:46:33 -04:00
parent e6438a9df3
commit 5c4669b39e
1 changed files with 17 additions and 7 deletions

View File

@ -283,14 +283,15 @@ class receiveDataThread(QThread):
try: try:
self.sock.shutdown(socket.SHUT_RDWR)
self.sock.close() self.sock.close()
except Exception, err: except Exception, err:
print 'Within receiveDataThread run(), self.sock.close() failed.', err print 'Within receiveDataThread run(), self.sock.shutdown or .close() failed.', err
try: try:
del selfInitiatedConnections[self.streamNumber][self] del selfInitiatedConnections[self.streamNumber][self]
printLock.acquire() printLock.acquire()
print 'removed self (a receiveDataThread) from ConnectionList' print 'removed self (a receiveDataThread) from selfInitiatedConnections'
printLock.release() printLock.release()
except: except:
pass pass
@ -449,6 +450,7 @@ class receiveDataThread(QThread):
printLock.acquire() printLock.acquire()
print 'We are connected to too many people. Closing connection.' print 'We are connected to too many people. Closing connection.'
printLock.release() printLock.release()
self.sock.shutdown(socket.SHUT_RDWR)
self.sock.close() self.sock.close()
return return
self.sendBigInv() self.sendBigInv()
@ -1682,11 +1684,11 @@ class receiveDataThread(QThread):
if PORT != recaddrPort: if PORT != recaddrPort:
print 'Strange occurance: The port specified in an addr message', str(recaddrPort),'does not match the port',str(PORT),'that this program (or some other peer) used to connect to it',str(hostFromAddrMessage),'. Perhaps they changed their port or are using a strange NAT configuration.' print 'Strange occurance: The port specified in an addr message', str(recaddrPort),'does not match the port',str(PORT),'that this program (or some other peer) used to connect to it',str(hostFromAddrMessage),'. Perhaps they changed their port or are using a strange NAT configuration.'
if needToWriteKnownNodesToDisk: #Runs if any nodes were new to us. Also, share those nodes with our peers. if needToWriteKnownNodesToDisk: #Runs if any nodes were new to us. Also, share those nodes with our peers.
output = open(appdata + 'knownnodes.dat', 'wb')
knownNodesLock.acquire() knownNodesLock.acquire()
output = open(appdata + 'knownnodes.dat', 'wb')
pickle.dump(knownNodes, output) pickle.dump(knownNodes, output)
knownNodesLock.release()
output.close() output.close()
knownNodesLock.release()
self.broadcastaddr(listOfAddressDetailsToBroadcastToPeers) #no longer broadcast self.broadcastaddr(listOfAddressDetailsToBroadcastToPeers) #no longer broadcast
printLock.acquire() printLock.acquire()
print 'knownNodes currently has', len(knownNodes[self.streamNumber]), 'nodes for this stream.' print 'knownNodes currently has', len(knownNodes[self.streamNumber]), 'nodes for this stream.'
@ -1773,11 +1775,11 @@ class receiveDataThread(QThread):
if PORT != recaddrPort: if PORT != recaddrPort:
print 'Strange occurance: The port specified in an addr message', str(recaddrPort),'does not match the port',str(PORT),'that this program (or some other peer) used to connect to it',str(hostFromAddrMessage),'. Perhaps they changed their port or are using a strange NAT configuration.' print 'Strange occurance: The port specified in an addr message', str(recaddrPort),'does not match the port',str(PORT),'that this program (or some other peer) used to connect to it',str(hostFromAddrMessage),'. Perhaps they changed their port or are using a strange NAT configuration.'
if needToWriteKnownNodesToDisk: #Runs if any nodes were new to us. Also, share those nodes with our peers. if needToWriteKnownNodesToDisk: #Runs if any nodes were new to us. Also, share those nodes with our peers.
output = open(appdata + 'knownnodes.dat', 'wb')
knownNodesLock.acquire() knownNodesLock.acquire()
output = open(appdata + 'knownnodes.dat', 'wb')
pickle.dump(knownNodes, output) pickle.dump(knownNodes, output)
knownNodesLock.release()
output.close() output.close()
knownNodesLock.release()
self.broadcastaddr(listOfAddressDetailsToBroadcastToPeers) self.broadcastaddr(listOfAddressDetailsToBroadcastToPeers)
printLock.acquire() printLock.acquire()
print 'knownNodes currently has', len(knownNodes[self.streamNumber]), 'nodes for this stream.' print 'knownNodes currently has', len(knownNodes[self.streamNumber]), 'nodes for this stream.'
@ -1913,6 +1915,7 @@ class receiveDataThread(QThread):
print 'Remote node useragent:', useragent, ' stream number:', self.streamNumber print 'Remote node useragent:', useragent, ' stream number:', self.streamNumber
printLock.release() printLock.release()
if self.streamNumber != 1: if self.streamNumber != 1:
self.sock.shutdown(socket.SHUT_RDWR)
self.sock.close() self.sock.close()
printLock.acquire() printLock.acquire()
print 'Closed connection to', self.HOST, 'because they are interested in stream', self.streamNumber,'.' print 'Closed connection to', self.HOST, 'because they are interested in stream', self.streamNumber,'.'
@ -1922,6 +1925,7 @@ class receiveDataThread(QThread):
if not self.initiatedConnection: if not self.initiatedConnection:
broadcastToSendDataQueues((0,'setStreamNumber',(self.HOST,self.streamNumber))) broadcastToSendDataQueues((0,'setStreamNumber',(self.HOST,self.streamNumber)))
if data[72:80] == eightBytesOfRandomDataUsedToDetectConnectionsToSelf: if data[72:80] == eightBytesOfRandomDataUsedToDetectConnectionsToSelf:
self.sock.shutdown(socket.SHUT_RDWR)
self.sock.close() self.sock.close()
printLock.acquire() printLock.acquire()
print 'Closing connection to myself: ', self.HOST print 'Closing connection to myself: ', self.HOST
@ -1933,8 +1937,8 @@ class receiveDataThread(QThread):
knownNodes[self.streamNumber][self.HOST] = (self.remoteNodeIncomingPort, int(time.time())) knownNodes[self.streamNumber][self.HOST] = (self.remoteNodeIncomingPort, int(time.time()))
output = open(appdata + 'knownnodes.dat', 'wb') output = open(appdata + 'knownnodes.dat', 'wb')
pickle.dump(knownNodes, output) pickle.dump(knownNodes, output)
knownNodesLock.release()
output.close() output.close()
knownNodesLock.release()
self.sendverack() self.sendverack()
if self.initiatedConnection == False: if self.initiatedConnection == False:
@ -2011,6 +2015,7 @@ class sendDataThread(QThread):
if data == self.HOST or data == 'all': if data == self.HOST or data == 'all':
printLock.acquire() printLock.acquire()
print 'sendDataThread thread (associated with', self.HOST,') ID:',id(self), 'shutting down now.' print 'sendDataThread thread (associated with', self.HOST,') ID:',id(self), 'shutting down now.'
self.sock.shutdown(socket.SHUT_RDWR)
self.sock.close() self.sock.close()
sendDataQueues.remove(self.mailbox) sendDataQueues.remove(self.mailbox)
print 'len of sendDataQueues', len(sendDataQueues) print 'len of sendDataQueues', len(sendDataQueues)
@ -2045,6 +2050,7 @@ class sendDataThread(QThread):
self.lastTimeISentData = int(time.time()) self.lastTimeISentData = int(time.time())
except: except:
print 'self.sock.sendall failed' print 'self.sock.sendall failed'
self.sock.shutdown(socket.SHUT_RDWR)
self.sock.close() self.sock.close()
sendDataQueues.remove(self.mailbox) sendDataQueues.remove(self.mailbox)
print 'sendDataThread thread', self, 'ending now' print 'sendDataThread thread', self, 'ending now'
@ -2064,6 +2070,7 @@ class sendDataThread(QThread):
self.lastTimeISentData = int(time.time()) self.lastTimeISentData = int(time.time())
except: except:
print 'self.sock.sendall failed' print 'self.sock.sendall failed'
self.sock.shutdown(socket.SHUT_RDWR)
self.sock.close() self.sock.close()
sendDataQueues.remove(self.mailbox) sendDataQueues.remove(self.mailbox)
print 'sendDataThread thread', self, 'ending now' print 'sendDataThread thread', self, 'ending now'
@ -2079,6 +2086,7 @@ class sendDataThread(QThread):
self.lastTimeISentData = int(time.time()) self.lastTimeISentData = int(time.time())
except: except:
print 'self.sock.send pong failed' print 'self.sock.send pong failed'
self.sock.shutdown(socket.SHUT_RDWR)
self.sock.close() self.sock.close()
sendDataQueues.remove(self.mailbox) sendDataQueues.remove(self.mailbox)
print 'sendDataThread thread', self, 'ending now' print 'sendDataThread thread', self, 'ending now'
@ -5662,6 +5670,8 @@ if __name__ == "__main__":
config.set('bitmessagesettings','sockspassword','') config.set('bitmessagesettings','sockspassword','')
config.set('bitmessagesettings','keysencrypted','false') config.set('bitmessagesettings','keysencrypted','false')
config.set('bitmessagesettings','messagesencrypted','false') config.set('bitmessagesettings','messagesencrypted','false')
config.set('bitmessagesettings','defaultnoncetrialsperbyte',str(networkDefaultProofOfWorkNonceTrialsPerByte))
config.set('bitmessagesettings','defaultpayloadlengthextrabytes',str(networkDefaultPayloadLengthExtraBytes))
if storeConfigFilesInSameDirectoryAsProgramByDefault: if storeConfigFilesInSameDirectoryAsProgramByDefault:
#Just use the same directory as the program and forget about the appdata folder #Just use the same directory as the program and forget about the appdata folder