Suppose I have a Service:
import rpyc
class MyService(rpyc.Service):
my_dict = {}
def exposed_put(self, key, val):
MyService.my_dict[key] = val
def exposed_get(self, key):
return MyService.my_dict[key]
def exposed_delete(self, key):
del MyService.my_dict[key]
Now I start that Service running in a ThreadedServer:
from rpyc.utils.server import ThreadedServer
server = ThreadedServer(MyService, port=8000)
server.start()
Now in a different process on the same machine, I open a new Connection to the server:
import rpyc
c = rpyc.connect('localhost', 8000)
... but before accessing the root of the connection, the server process stops for some reason, such as Ctrl-Z in the controlling terminal of the server process. Now when I try to access the root via:
c.root
... Python hangs. Ctrl-C on the client side shows this:
In [31]: c.root
^C---------------------------------------------------------------------------
KeyboardInterrupt Traceback (most recent call last)
<ipython-input-31-856a441cc51a> in <module>()
----> 1 c.root
/home/mack/anaconda/lib/python2.7/site-packages/rpyc/core/protocol.pyc in root(self)
465 """Fetches the root object (service) of the other party"""
466 if self._remote_root is None:
--> 467 self._remote_root = self.sync_request(consts.HANDLE_GETROOT)
468 return self._remote_root
469
/home/mack/anaconda/lib/python2.7/site-packages/rpyc/core/protocol.pyc in sync_request(self, handler, *args)
436 seq = self._send_request(handler, args)
437 while seq not in self._sync_replies:
--> 438 self.serve(0.1)
439 isexc, obj = self._sync_replies.pop(seq)
440 if isexc:
/home/mack/anaconda/lib/python2.7/site-packages/rpyc/core/protocol.pyc in serve(self, timeout)
385 otherwise.
386 """
--> 387 data = self._recv(timeout, wait_for_lock = True)
388 if not data:
389 return False
/home/mack/anaconda/lib/python2.7/site-packages/rpyc/core/protocol.pyc in _recv(self, timeout, wait_for_lock)
342 return None
343 try:
--> 344 if self._channel.poll(timeout):
345 data = self._channel.recv()
346 else:
/home/mack/anaconda/lib/python2.7/site-packages/rpyc/core/channel.pyc in poll(self, timeout)
41 def poll(self, timeout):
42 """polls the underlying steam for data, waiting up to *timeout* seconds"""
---> 43 return self.stream.poll(timeout)
44 def recv(self):
45 """Receives the next packet (or *frame*) from the underlying stream.
/home/mack/anaconda/lib/python2.7/site-packages/rpyc/core/stream.pyc in poll(self, timeout)
39 while True:
40 try:
---> 41 rl, _, _ = select([self], [], [], timeout)
42 except select_error as ex:
43 if ex[0] == errno.EINTR:
KeyboardInterrupt:
So it appears a call to Stream.poll ends up in an infinite loop if the server process is stopped but still connected (underlying socket is still open). Am I correct in thinking this is an unexpected case in the Stream implementation? I am using version 3.3.0. How might I detect this case and avoid the client hanging?
If there is a risk of the server being haulted you can just check the value of c.closed
you could also provide, on your client side a callback function to notify you that it has closed and pass it to your initialiser, with the name on_exit
possibly, then register it with atexit.
To deal with the remote possibility that the server is paused, or less remote, is busy you would have to implement a heartbeat, i.e. a periodic callback to inform the client that the server is available.