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

Time limited Rserve session #199

Open
sbstatgen opened this issue Jan 23, 2024 · 3 comments
Open

Time limited Rserve session #199

sbstatgen opened this issue Jan 23, 2024 · 3 comments

Comments

@sbstatgen
Copy link

I would like to terminate a client's Rserve session if a certain amount of time expires or based on some other criteria. Alternatively, require the client to log in/authenticate again. Is there any way to do this in Rserve ?

@s-u
Copy link
Owner

s-u commented Jan 23, 2024

Once the connection is established, you have full control over what the session will do, so you can terminate the session whenever you feel like it which will close the connection on the client side.

There are many way you can do this - if you really want to terminate the session regardless, you can use a watchdog child process that will wait until the time is up and then kill the session:

watchdog = function (max.lifetime = 100) {  ## set the max allowed session time in seconds
  pid = Sys.getpid()
  p = parallel:::mcfork()
  if (inherits(p,"masterProcess")) { ## the watchdog
    parallel:::closeAll()
    p$pid=pid
    Sys.sleep(max.lifetime)
    parallel:::sendMaster(raw(),TRUE) ## check if it is still alive
    parallel:::mckill(p, 9)  ## terminate it
  }
}

The sendMaster() will abort the watchdog if the master is already gone so the kill is only executed if it's still there.

For example can put it in watchdog.R and run R CMD Rserve --RS-set source=watchdog.R then you will see:

library(RSclient)
> c=RS.connect()
> RS.eval.qap(c, quote(watchdog(5))) ## set very short timeout
> RS.eval.qap(c, quote(Sys.sleep(100))) ## try to run a long computation - it will end early:
rsc_abort: connection closed by peer
Error in RS.eval.qap(c, quote(Sys.sleep(100))) : 
  read error - could not obtain response header

You could improve the above by checking on the session more often (so the watchdog can quit early) and you could add warnings to the user etc. You could also be less aggressive and only interrupt etc.

@sbstatgen
Copy link
Author

Have checked it....works smoothly. For linux server I guess (not windows), but that is what I needed. Thanks a lot for this !

@sbstatgen
Copy link
Author

I had a follow-up question regarding your suggestion about "watchdog can quit early". If the user exits early using say RS.close(c), the master is exiting. It should ideally kill any running child processes such as watchdog by sending some signal. It seems this is not the case for RS.close(c) ? Is there any way to achieve this from the master, or checking intermittently from the child is the only possibility?

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

2 participants