Skip to content

Commit

Permalink
replaced all loseConnection calls with abortConnection, hopefully add…
Browse files Browse the repository at this point in the history
…ressing some memory leakage
  • Loading branch information
forrestv committed Apr 25, 2013
1 parent a32b6ef commit 0cb07df
Showing 2 changed files with 18 additions and 20 deletions.
26 changes: 8 additions & 18 deletions p2pool/p2p.py
Original file line number Diff line number Diff line change
@@ -67,7 +67,7 @@ def connectionMade(self):
max_id=2**256,
func=lambda id, hashes, parents, stops: self.send_sharereq(id=id, hashes=hashes, parents=parents, stops=stops),
timeout=15,
on_timeout=self.transport.loseConnection,
on_timeout=self.disconnect,
)

self.remote_tx_hashes = set() # view of peer's known_txs # not actually initially empty, but sending txs instead of tx hashes won't hurt
@@ -80,12 +80,7 @@ def connectionMade(self):
def _connect_timeout(self):
self.timeout_delayed = None
print 'Handshake timed out, disconnecting from %s:%i' % self.addr
if hasattr(self.transport, 'abortConnection'):
# Available since Twisted 11.1
self.transport.abortConnection()
else:
# This doesn't always close timed out connections!
self.transport.loseConnection()
self.disconnect()

def packetReceived(self, command, payload2):
try:
@@ -99,19 +94,14 @@ def packetReceived(self, command, payload2):
def badPeerHappened(self):
if p2pool.DEBUG:
print "Bad peer banned:", self.addr
self.transport.loseConnection()
self.disconnect()
if self.transport.getPeer().host != '127.0.0.1': # never ban localhost
self.node.bans[self.transport.getPeer().host] = time.time() + 60*60

def _timeout(self):
self.timeout_delayed = None
print 'Connection timed out, disconnecting from %s:%i' % self.addr
if hasattr(self.transport, 'abortConnection'):
# Available since Twisted 11.1
self.transport.abortConnection()
else:
# This doesn't always close timed out connections!
self.transport.loseConnection()
self.disconnect()

message_version = pack.ComposedType([
('version', pack.IntType(32)),
@@ -138,7 +128,7 @@ def handle_version(self, version, services, addr_to, addr_from, nonce, sub_versi
if nonce in self.node.peers:
if p2pool.DEBUG:
print 'Detected duplicate connection, disconnecting from %s:%i' % self.addr
self.transport.loseConnection()
self.disconnect()
return

self.nonce = nonce
@@ -350,7 +340,7 @@ def handle_remember_tx(self, tx_hashes, txs):
for tx_hash in tx_hashes:
if tx_hash in self.remembered_txs:
print >>sys.stderr, 'Peer referenced transaction twice, disconnecting'
self.transport.loseConnection()
self.disconnect()
return

if tx_hash in self.node.known_txs_var.value:
@@ -363,7 +353,7 @@ def handle_remember_tx(self, tx_hashes, txs):
break
else:
print >>sys.stderr, 'Peer referenced unknown transaction %064x, disconnecting' % (tx_hash,)
self.transport.loseConnection()
self.disconnect()
return

self.remembered_txs[tx_hash] = tx
@@ -374,7 +364,7 @@ def handle_remember_tx(self, tx_hashes, txs):
tx_hash = bitcoin_data.hash256(bitcoin_data.tx_type.pack(tx))
if tx_hash in self.remembered_txs:
print >>sys.stderr, 'Peer referenced transaction twice, disconnecting'
self.transport.loseConnection()
self.disconnect()
return

if tx_hash in self.node.known_txs_var.value and not warned:
12 changes: 10 additions & 2 deletions p2pool/util/p2protocol.py
Original file line number Diff line number Diff line change
@@ -55,7 +55,7 @@ def dataReceiver(self):
except:
print 'RECV', command, payload[:100].encode('hex') + ('...' if len(payload) > 100 else '')
log.err(None, 'Error handling message: (see RECV line)')
self.transport.loseConnection()
self.disconnect()

def packetReceived(self, command, payload2):
handler = getattr(self, 'handle_' + command, None)
@@ -67,8 +67,16 @@ def packetReceived(self, command, payload2):
if getattr(self, 'connected', True) and not getattr(self, 'disconnecting', False):
handler(**payload2)

def disconnect(self):
if hasattr(self.transport, 'abortConnection'):
# Available since Twisted 11.1
self.transport.abortConnection()
else:
# This doesn't always close timed out connections! warned about in main
self.transport.loseConnection()

def badPeerHappened(self):
self.transport.loseConnection()
self.disconnect()

def sendPacket(self, command, payload2):
if len(command) >= 12:

0 comments on commit 0cb07df

Please sign in to comment.