|
33 | 33 | import gevent.coros |
34 | 34 |
|
35 | 35 | import gevent_zmq as zmq |
36 | | -from .exceptions import TimeoutExpired, RemoteError |
| 36 | +from .exceptions import TimeoutExpired, RemoteError, LostRemote |
37 | 37 | from .channel import ChannelMultiplexer, BufferedChannel |
38 | 38 | from .socket import SocketBase |
39 | 39 | from .heartbeat import HeartBeatOnChannel |
@@ -105,34 +105,39 @@ def __call__(self, method, *args): |
105 | 105 | raise NameError(method) |
106 | 106 | return self._methods[method](*args) |
107 | 107 |
|
| 108 | + def _print_traceback(self, protocol_v1): |
| 109 | + exc_type, exc_value, exc_traceback = sys.exc_info() |
| 110 | + try: |
| 111 | + traceback.print_exception(exc_type, exc_value, exc_traceback, |
| 112 | + file=sys.stderr) |
| 113 | + |
| 114 | + if protocol_v1: |
| 115 | + return (repr(exc_value),) |
| 116 | + |
| 117 | + human_traceback = traceback.format_exc() |
| 118 | + name = exc_type.__name__ |
| 119 | + human_msg = str(exc_value) |
| 120 | + return (name, human_msg, human_traceback) |
| 121 | + finally: |
| 122 | + del exc_traceback |
| 123 | + |
108 | 124 | def _async_task(self, initial_event): |
109 | | - protocol_v2 = initial_event.header.get('v', 1) >= 2 |
| 125 | + protocol_v1 = initial_event.header.get('v', 1) < 2 |
110 | 126 | channel = self._multiplexer.channel(initial_event) |
111 | 127 | hbchan = HeartBeatOnChannel(channel, freq=self._heartbeat_freq, |
112 | | - passive=not protocol_v2) |
| 128 | + passive=protocol_v1) |
113 | 129 | bufchan = BufferedChannel(hbchan) |
114 | 130 | event = bufchan.recv() |
115 | 131 | try: |
116 | 132 | functor = self._methods.get(event.name, None) |
117 | 133 | if functor is None: |
118 | 134 | raise NameError(event.name) |
119 | 135 | functor.pattern.process_call(self._context, bufchan, event, functor) |
| 136 | + except LostRemote: |
| 137 | + self._print_traceback(protocol_v1) |
120 | 138 | except Exception: |
121 | | - try: |
122 | | - exc_type, exc_value, exc_traceback = sys.exc_info() |
123 | | - traceback.print_exception(exc_type, exc_value, exc_traceback, |
124 | | - file=sys.stderr) |
125 | | - human_traceback = traceback.format_exc() |
126 | | - |
127 | | - name = exc_type.__name__ |
128 | | - human_msg = str(exc_value) |
129 | | - |
130 | | - if protocol_v2: |
131 | | - bufchan.emit('ERR', (name, human_msg, human_traceback)) |
132 | | - else: |
133 | | - bufchan.emit('ERR', (repr(exc_value),)) |
134 | | - finally: |
135 | | - del exc_traceback |
| 139 | + exception_info = self._print_traceback(protocol_v1) |
| 140 | + bufchan.emit('ERR', exception_info) |
136 | 141 | finally: |
137 | 142 | bufchan.close() |
138 | 143 | bufchan.channel.close() |
|
0 commit comments