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

Recommended ways to debug RServe R session from remote client #124

Open
manuelwaltschek opened this issue Jan 24, 2019 · 1 comment
Open

Comments

@manuelwaltschek
Copy link

Hello there!

This is not really an issue, nor a bug or a feature request, but I wanted to ask for any ideas on how to debug an R session that has been created by RServe.

I am trying to implement a java ee application that does some statistics on mobility data and I tried to find a reliable open source solution. For now RServe with REngine seems to do the job, but when it comes to more complex object assignments and function calls I'd like to debug the R session the java client is using via Rserve. Is it somehow possible to remotely attach an R client / R IDE to a specific session to step through code and monitor the global environment?

I found RStudio server, packages like remoter or your RSclient but I could not find a hint that you can actually attach to a running R session other than creating a new one by creating a new connection.

Has anyone ever done that or is it simply not possible?

Thank you in advance,

Manuel Waltschek

@manuelwaltschek manuelwaltschek changed the title Recommenden ways to debug RServe R session from remote client Recommended ways to debug RServe R session from remote client Jan 24, 2019
@s-u
Copy link
Owner

s-u commented Jul 26, 2019

There is no easy way to attach to a process that is already running, other than gdb/llldb and using the C-level API from there to inspect what you need, but it's very painful/cumbersome.

Unfortunately Rserve itself is not re-entrant for safety reasons, so you cannot use run.Rserve() inside a session spawned by Rserve - which would be one way to attach another client to the same session. I'll take it as feature request since it would be technically possible to create "Rserve lite" which runs an in-session server just to debug that session.

That said, there is a poor-man's version, though. Since you have control of the client, you could run something like this:

The server part - you eval that in your session - you may need to have your Java code to prepare it:

run.server <- function() {
  c <- socketConnection(port=1234, server=TRUE, blocking=TRUE, open="a+b")
  on.exit(close(c))
  while(length(l <- readBin(c, 1L))) {
    res <- eval(unserialize(readBin(c,raw(), l)))
    r <- serialize(res, NULL)
    writeBin(length(r), c)
    writeBin(r, c)
  }
}

and you can serve one client by running run.server(). It will exit once the connection is closed.

Then on the client side you can evaluate any expressions:

ev <- function(x) {
  x <- serialize(substitute(x), NULL)
  writeBin(length(x), c)
  writeBin(x, c)
  l <- readBin(c, 1L)
  r <- readBin(c, raw(), l)
  unserialize(r)
}
> c=socketConnection(port=1234, open="a+b", blocking=TRUE)
> ev(Sys.getpid())
[1] 20263
> close(c)
## just to prove it was on the remote side:
> Sys.getpid()
[1] 20297

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