Skip to content

add closing protection for Session #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions msgpackrpc/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ class TransportError(RPCError):
CODE = ".TransportError"
pass

class SessionError(RPCError):
CODE = ".SessionError"
pass

class CallError(RPCError):
CODE = ".NoMethodError"
pass
Expand Down
8 changes: 7 additions & 1 deletion msgpackrpc/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from msgpackrpc.future import Future
from msgpackrpc.transport import tcp
from msgpackrpc.compat import iteritems
from msgpackrpc.error import TimeoutError
from msgpackrpc.error import TimeoutError, SessionError


class Session(object):
Expand Down Expand Up @@ -32,6 +32,7 @@ def __init__(self, address, timeout, loop=None, builder=tcp, reconnect_limit=5,
self._transport = builder.ClientTransport(self, self._address, reconnect_limit, encodings=(pack_encoding, unpack_encoding))
self._generator = _NoSyncIDGenerator()
self._request_table = {}
self._closing = False

@property
def address(self):
Expand All @@ -45,6 +46,9 @@ def call_async(self, method, *args):

def send_request(self, method, args):
# need lock?
if self._closing:
raise SessionError('Session closed')

msgid = next(self._generator)
future = Future(self._loop, self._timeout)
self._request_table[msgid] = future
Expand All @@ -58,6 +62,7 @@ def callback():
self._loop.start()

def close(self):
self._closing = True
if self._transport:
self._transport.close()
self._transport = None
Expand All @@ -68,6 +73,7 @@ def on_connect_failed(self, reason):
The callback called when the connection failed.
Called by the transport layer.
"""
self._closing = True
# set error for all requests
for msgid, future in iteritems(self._request_table):
future.set_error(reason)
Expand Down