irmen / Pyro5

Pyro 5 - Python remote objects
https://pyro5.readthedocs.io
MIT License
303 stars 36 forks source link

Why does the proxy loses the connection at KeyboardInterrupt exception? #51

Closed dcordb closed 3 years ago

dcordb commented 3 years ago

Thanks for this amazing library.

I have the following code on the server:

from Pyro5.server import serve, expose
import time

@expose
class Server:
    def foo(self):
        time.sleep(5)

    def ping(self):
        print('Pong')

serve(
    { Server: 'server'},
    port=1234,
    use_ns=False
)

And on the client:

from Pyro5.api import Proxy

uri = 'PYRO:server@0.0.0.0:1234'
srv = Proxy(uri)

srv.ping()
print(f'Status of connection = {srv._pyroConnection}')

try:
    srv.foo() # do ctrl-c here
except KeyboardInterrupt:
    print(f'Status of connection = {srv._pyroConnection}')

I run this, and then type ctrl-c on the call of srv.foo() (on the client). The output on the client is:

Status of connection = <Pyro5.socketutil.SocketConnection object at 0x7f294d78d160>
Status of connection = None

The question is, why does the proxy loses the connection when the KeyboardInterrupt exception happens?

irmen commented 3 years ago

Hi!

This is a safeguard to avoid corrupt transfers for the next method call, because the keyboardinterrupt could come halfway through reading bytes from the socket. If the connection wasn't reset, there's a chance the next call would read remaining bytes in the socket's IO buffer as being (part of) the response for this next method call - which leads to further trouble.

See the comment in the code block starting at https://github.com/irmen/Pyro5/blob/e76080fad180672efb24569606483cc90e3c3370/Pyro5/client.py#L246