Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def on_error(err):
"""
this may seem redundant after looking at _connect_transport, but
it will handle a case where something goes wrong in
_connect_transport itself -- as the only connect our
caller has is the 'done' future
"""
transport.connect_failures += 1
# something bad has happened, and maybe didn't get caught
# upstream yet
if not txaio.is_called(done):
txaio.reject(done, err)
txaio.add_callbacks(d, None, on_error)
def lost(fail):
rtn = orig(fail)
if not txaio.is_called(done):
# asyncio will call connection_lost(None) in case of
# a transport failure, in which case we create an
# appropriate exception
if fail is None:
fail = TransportLost("failed to complete connection")
txaio.reject(done, fail)
return rtn
proto.connection_lost = lost
requests.clear()
if outstanding:
self.log.info(
'Cancelling {count} outstanding requests',
count=len(outstanding),
)
for request in outstanding:
self.log.debug(
'cleaning up outstanding {request_type} request {request_id}, '
'firing errback on user handler {request_on_reply}',
request_on_reply=request.on_reply,
request_id=request.request_id,
request_type=request.__class__.__name__,
)
txaio.reject(request.on_reply, exc)
# wait for any async-ness in the error handlers for on_reply
txaio.add_callbacks(d, lambda _: request.on_reply, lambda _: request.on_reply)
return d
on_reply = self._subscribe_reqs.pop(msg.request).on_reply
# ERROR reply to UNSUBSCRIBE
elif msg.request_type == message.Unsubscribe.MESSAGE_TYPE and msg.request in self._unsubscribe_reqs:
on_reply = self._unsubscribe_reqs.pop(msg.request).on_reply
# ERROR reply to REGISTER
elif msg.request_type == message.Register.MESSAGE_TYPE and msg.request in self._register_reqs:
on_reply = self._register_reqs.pop(msg.request).on_reply
# ERROR reply to UNREGISTER
elif msg.request_type == message.Unregister.MESSAGE_TYPE and msg.request in self._unregister_reqs:
on_reply = self._unregister_reqs.pop(msg.request).on_reply
if on_reply:
txaio.reject(on_reply, self._exception_from_message(msg))
else:
raise ProtocolError("WampAppSession.onMessage(): ERROR received for non-pending request_type {0} and request ID {1}".format(msg.request_type, msg.request))
else:
raise ProtocolError("Unexpected message {0}".format(msg.__class__))
on_reply = self._subscribe_reqs.pop(msg.request).on_reply
# ERROR reply to UNSUBSCRIBE
elif msg.request_type == message.Unsubscribe.MESSAGE_TYPE and msg.request in self._unsubscribe_reqs:
on_reply = self._unsubscribe_reqs.pop(msg.request).on_reply
# ERROR reply to REGISTER
elif msg.request_type == message.Register.MESSAGE_TYPE and msg.request in self._register_reqs:
on_reply = self._register_reqs.pop(msg.request).on_reply
# ERROR reply to UNREGISTER
elif msg.request_type == message.Unregister.MESSAGE_TYPE and msg.request in self._unregister_reqs:
on_reply = self._unregister_reqs.pop(msg.request).on_reply
if on_reply:
txaio.reject(on_reply, self._exception_from_message(msg))
else:
raise ProtocolError("WampAppSession.onMessage(): ERROR received for non-pending request_type {0} and request ID {1}".format(msg.request_type, msg.request))
else:
raise ProtocolError("Unexpected message {0}".format(msg.__class__))
def on_disconnect(session, was_clean):
self.log.debug(
"session on_disconnect: was_clean={was_clean}",
was_clean=was_clean,
)
if not txaio.is_called(done):
if was_clean:
# eg the session has left the realm, and the transport was properly
# shut down. successfully finish the connection
txaio.resolve(done, None)
else:
txaio.reject(done, RuntimeError('transport closed uncleanly'))
session.on('disconnect', on_disconnect)
def error(fail):
self._delay_f = None
if self._stopping:
# might be better to add framework-specific checks in
# subclasses to see if this is CancelledError (for
# Twisted) and whatever asyncio does .. but tracking
# if we're in the shutdown path is fine too
txaio.resolve(self._done_f, None)
else:
self.log.info("Internal error {msg}", msg=txaio.failure_message(fail))
self.log.debug("{tb}", tb=txaio.failure_format_traceback(fail))
txaio.reject(self._done_f, fail)
def on_connect_success(result):
# async connect call returns a 2-tuple
transport, proto = result
# in the case where we .abort() the transport / connection
# during setup, we still get on_connect_success but our
# transport is already closed (this will happen if
# e.g. there's an "open handshake timeout") -- I don't
# know if there's a "better" way to detect this? #python
# doesn't know of one, anyway
if transport.is_closing():
if not txaio.is_called(done):
reason = getattr(proto, "_onclose_reason", "Connection already closed")
txaio.reject(done, TransportLost(reason))
return
# if e.g. an SSL handshake fails, we will have
# successfully connected (i.e. get here) but need to
# 'listen' for the "connection_lost" from the underlying
# protocol in case of handshake failure .. so we wrap
# it. Also, we don't increment transport.success_count
# here on purpose (because we might not succeed).
# XXX double-check that asyncio behavior on TLS handshake
# failures is in fact as described above
orig = proto.connection_lost
@wraps(orig)
def lost(fail):
rtn = orig(fail)
self.onUserError(txaio.create_failure(), "While firing on_progress")
except:
pass
else:
# process final call result
# drop original request
del self._call_reqs[msg.request]
# user callback that gets fired
on_reply = call_request.on_reply
# above might already have rejected, so we guard ..
if enc_err:
txaio.reject(on_reply, enc_err)
else:
if msg.kwargs:
if msg.args:
res = types.CallResult(*msg.args, **msg.kwargs)
else:
res = types.CallResult(**msg.kwargs)
txaio.resolve(on_reply, res)
else:
if msg.args:
if len(msg.args) > 1:
res = types.CallResult(*msg.args)
txaio.resolve(on_reply, res)
else:
txaio.resolve(on_reply, msg.args[0])
else:
txaio.resolve(on_reply, None)