Commit 69e2f2ce authored by Yoshinori Okuji's avatar Yoshinori Okuji

Protect initialization more strictly. Also ignore adding or expecting a packet...

Protect initialization more strictly. Also ignore adding or expecting a packet when a connection is closed.

git-svn-id: https://svn.erp5.org/repos/neo/branches/prototype3@218 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent 25d74f5f
...@@ -234,6 +234,9 @@ class Connection(BaseConnection): ...@@ -234,6 +234,9 @@ class Connection(BaseConnection):
def addPacket(self, packet): def addPacket(self, packet):
"""Add a packet into the write buffer.""" """Add a packet into the write buffer."""
if self.s is None:
return
try: try:
self.write_buf.append(packet.encode()) self.write_buf.append(packet.encode())
except ProtocolError, m: except ProtocolError, m:
...@@ -264,6 +267,9 @@ class Connection(BaseConnection): ...@@ -264,6 +267,9 @@ class Connection(BaseConnection):
The additional timeout defines the amount of time after the timeout The additional timeout defines the amount of time after the timeout
to invoke a timeoutExpired callback. If it is zero, no ping is sent, and to invoke a timeoutExpired callback. If it is zero, no ping is sent, and
the callback is executed immediately.""" the callback is executed immediately."""
if self.s is None:
return
event = IdleEvent(self, msg_id, timeout, additional_timeout) event = IdleEvent(self, msg_id, timeout, additional_timeout)
self.event_dict[msg_id] = event self.event_dict[msg_id] = event
self.em.addIdleEvent(event) self.em.addIdleEvent(event)
...@@ -271,7 +277,7 @@ class Connection(BaseConnection): ...@@ -271,7 +277,7 @@ class Connection(BaseConnection):
class ClientConnection(Connection): class ClientConnection(Connection):
"""A connection from this node to a remote node.""" """A connection from this node to a remote node."""
def __init__(self, event_manager, handler, addr = None, **kw): def __init__(self, event_manager, handler, addr = None, **kw):
self.connecting = False self.connecting = True
Connection.__init__(self, event_manager, handler, addr = addr) Connection.__init__(self, event_manager, handler, addr = addr)
handler.connectionStarted(self) handler.connectionStarted(self)
try: try:
...@@ -283,11 +289,11 @@ class ClientConnection(Connection): ...@@ -283,11 +289,11 @@ class ClientConnection(Connection):
s.connect(addr) s.connect(addr)
except socket.error, m: except socket.error, m:
if m[0] == errno.EINPROGRESS: if m[0] == errno.EINPROGRESS:
self.connecting = True
event_manager.addWriter(self) event_manager.addWriter(self)
else: else:
raise raise
else: else:
self.connecting = False
self.handler.connectionCompleted() self.handler.connectionCompleted()
event_manager.addReader(self) event_manager.addReader(self)
except: except:
...@@ -319,7 +325,11 @@ class MTClientConnection(ClientConnection): ...@@ -319,7 +325,11 @@ class MTClientConnection(ClientConnection):
lock = RLock() lock = RLock()
self.acquire = lock.acquire self.acquire = lock.acquire
self.release = lock.release self.release = lock.release
super(MTClientConnection, self).__init__(*args, **kwargs) try:
self.lock()
super(MTClientConnection, self).__init__(*args, **kwargs)
finally:
self.unlock()
def lock(self, blocking = 1): def lock(self, blocking = 1):
return self.acquire(blocking = blocking) return self.acquire(blocking = blocking)
...@@ -333,7 +343,11 @@ class MTServerConnection(ServerConnection): ...@@ -333,7 +343,11 @@ class MTServerConnection(ServerConnection):
lock = RLock() lock = RLock()
self.acquire = lock.acquire self.acquire = lock.acquire
self.release = lock.release self.release = lock.release
super(MTServerConnection, self).__init__(*args, **kwargs) try:
self.lock()
super(MTServerConnection, self).__init__(*args, **kwargs)
finally:
self.unlock()
def lock(self, blocking = 1): def lock(self, blocking = 1):
return self.acquire(blocking = blocking) return self.acquire(blocking = blocking)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment