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

ThreadedServer Signal Handling: Shutting down a server by a remote call accept by service? #555

Open
glyh opened this issue Apr 19, 2024 · 4 comments
Assignees
Labels
Feature Request for developer-valued functionality Question Issue is a result of unclear documentation or lack of documentation Triage Investigation by a maintainer has started

Comments

@glyh
Copy link

glyh commented Apr 19, 2024

I am aware this is probably not the right place to ask but I can't find any other places to talk about rpyc.

So basically I want some rpc to shutdown the service.

class MyService(rpyc.Service):
    def exposed_shutdown(self):
        # what to do?

def run():
  ThreadedServer(MyService).start()

I have tried:

def exposed_shutdown(self):
    exit(0)
def exposed_shutdown(self):
    # https://rpyc.readthedocs.io/en/latest/_modules/rpyc/utils/server.html#Server.start
    raise EOFError
class SigShutdown(Exception):
    pass

class MyService(rpyc.Service):
    def exposed_shutdown(self):
        raise SigShutdown

def run():
    try:
        ThreadedServer(MyService).start()
    except SigShutdown:
        pass

None of the above works.

I am aware there's close(), but I have no way to pass that function into the Service as to get this function I need to construct a Server first which depends on the Service.

@glyh glyh changed the title How do I shutdown a server? How do I shutdown a server by a remote call accept by service? Apr 19, 2024
@glyh
Copy link
Author

glyh commented Apr 19, 2024

Looks like I need to patch _accept_method, and that is fairly complicated to do, I guess I'll probably stick to kill to just kill the process.

@glyh glyh changed the title How do I shutdown a server by a remote call accept by service? Shutting down a server by a remote call accept by service? Apr 19, 2024
@comrumino
Copy link
Collaborator

comrumino commented Apr 22, 2024

The reason the above method does not work is that the signal is being absorbed by the thread running the connection to your client (my best guess without testing/breakpoints/debugging myself). Each client will result in their own thread spawn when using ThreadedServer. From a system administration standpoint, it would make more sense to terminate the process using whatever starts your server — if you are using something like systemd it would potentially restart the service if you try doing it this way.

If you want a pure RPyC solution, I would say you would probably want to inherit the threaded server and extend it to do one of the following:

  1. intercept the shutdown request
  2. add support for propagating signals from your thread
  3. Try calling os.exit or sys.exit, but this is dirty and wouldn't exit cleanly.

As for where to ask questions, here is okay. I respond on best effort.

@comrumino comrumino self-assigned this Apr 22, 2024
@comrumino comrumino added the Question Issue is a result of unclear documentation or lack of documentation label Apr 22, 2024
@glyh
Copy link
Author

glyh commented Apr 23, 2024

I'll take a look once I have time. In my case the server is a forked subprocess of the client, and the Cilent is a textual TUI app.

@comrumino
Copy link
Collaborator

comrumino commented Apr 26, 2024

@glyh , iirc, when I did this for an electron desktop in the past, rather than forking the process, try keep the the server as a child process and sending the signal from the client/TUI-app. Of course, there needs to be signal handling for the RPyC server process — if I have time, I will check the docs for an example or try to write one this weekend.

From what I remember, RPyC does not document how to do this very well. There is certainly room for enhancement of RPyC regarding signal handling and related documentation (i.e., the ThreadedServer has many "gotchas" for signal handling).

@comrumino comrumino added Feature Request for developer-valued functionality Triage Investigation by a maintainer has started labels Apr 26, 2024
@comrumino comrumino changed the title Shutting down a server by a remote call accept by service? ThreadedServer Signal Handling: Shutting down a server by a remote call accept by service? Apr 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Request for developer-valued functionality Question Issue is a result of unclear documentation or lack of documentation Triage Investigation by a maintainer has started
Projects
None yet
Development

No branches or pull requests

2 participants