Skip to content
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

Question: how does the runtime prevent netref'ed values from being garbage collected? #511

Closed
sidneycadot opened this issue Oct 19, 2022 · 1 comment

Comments

@sidneycadot
Copy link

Hello,

Suppose I have the following server:

import rpyc

class MyService(rpyc.Service):
    def exposed_func(self):
        return {"a": 10, "b": 20}

service = MyService()
server = rpyc.utils.server.ThreadedServer(service, port=20000)
server.start()

When a client queries func(), it receives a netref to the dictionary that is supposed to exist at the server side.

However, at the server side, the dict value has already gone out of scope after exposed_func() returns, and is thus eligible for being garbage collected.

How does the server-side ensure that the value returned by exposed_func() still exists for the netref to be valid? Who takes ownership (and manages the lifetime of) the dict returned by the exposed_func()?

@sidneycadot
Copy link
Author

sidneycadot commented Oct 19, 2022

Okay I studied the source code for a bit, how this works is clever...

The Connection (aka Protocol) keeps references to objects that are returned over the link in its _local_objects attribute. These are subject to explicit reference counting, and when the remote side del's its netref, this is propagated over the line, leading to a decrease in the refcount. Once this hits zero, the object reference is removed from the _local_objects collection.

A mildly terrifying control flow for the lifetime management, but it seems to work :-)

Perhaps it is useful to mention this "live netref"-counting behavior in the docs somewhere; it demystifies what's going on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant