Skip to content

allow rstudioapi to communicate with RStudio from child procs - #155

Merged
kevinushey merged 3 commits into
masterfrom
feature/rstudio-ipc-communication
Sep 25, 2019
Merged

kevinushey merged 3 commits into
masterfrom
feature/rstudio-ipc-communication

Conversation

@kevinushey

@kevinushey kevinushey commented Sep 10, 2019 •

Copy link
Copy Markdown
Contributor

Re: rstudio/rstudio#4490.

This PR implements a (somewhat primitive) mechanism for performing IPC with the parent RStudio session. A pair of files are used to transfer 'requests' / 'responses'; the requests made by rstudioapi are just serialized R calls that can be evaluated in the main R session, and the responses can later be read after they are written by the parent session.

This PR communicates through plain files for a couple reasons:

  1. Since the relationship here is 1-1, and there's no notion of a "queue" of actions, just using a regular file keeps things simple.

  2. R's own solutions for this don't appear appropriate: socketConnection() are network sockets, and so require a host + port and the host of issues that accompany those. fifo() seem like an option, but unfortunately (as far as I can tell) are unreliable on Windows.

  3. We could likely consider farming this out to a separate R package that can handle connections like these more gracefully, but given our small requirements list it seems okay to proceed without dependencies and just use plain old files.

  4. Exposing the rsession's guts / C routines to child processes is likely to be challenging, so I wanted to avoid that route.

The main downside of this approach is we need to poll the filesystem rather than listen or wait for something like an OS signal, but that seems like it's worth paying given the downsides of other options. (If we found a low-dependency CRAN package that gave us the lower-level connection bits we might instead want to depend on that)

Comment thread R/code.R Outdated
#' \dontrun{rstudioapi::verifyAvailable()}
isAvailable <- function(version_needed = NULL) {

if (isChildProcess())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this might be too much magic. There may be existing code out there that relies on isAvailable to figure out if it's running in RStudio's main session. As written IPCs also require the synchronization of two independent polling loops (one in the package, one in RStudio), so are going to be expensive to call from the child process, which the caller might need to take into account. How would you feel about a separate function to check if the API is available in the parent?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point. My original intention was that a script a user wrote to run in the main R rsession should "just work" (at least as far as rstudioapi calls go) when run as a child process as well, but the distinction is probably necessary in some contexts.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. I think we should have a separate function which asks "am I in a child R process being run by RStudio?", which can just be a more official/exported version of isChildProcess. The tidyverse team has asked for how to determine this deterministically a number of times!

The site we're discussing here can just get a new parameter that indicates whether it should ask the parent if necessary (default to existing behavior, which is to only look at the main R session).

I think ordinary API calls being magical is okay as it makes code more portable.

Comment thread R/remote.R
saveRDS(data, file = tmp)
file.rename(tmp, request)

# loop until response is ready (poll)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A lot of API commands are fire and forget (they don't return any useful value). In those cases, could we indicate by some means that the command is to be sent to the parent process and execution is to proceed without waiting for a response? I don't think we need to wait for all of these polling loops to line up if the caller doesn't need the response (errors be damned).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would complicate things a bit.

If the child process can submit multiple commands without waiting for a response, then we'll need a proper queue that the parent + child process can synchronize over, rather than just the "single file" approach. We could probably build something on top of an existing CRAN package but it could be substantially more work + would require more dependencies for rstudioapi (but maybe that's fine).

We'd also need some way to encode, for each rstudioapi function, whether it has an "interesting" return value.

It seems unlikely that users would want to make a large number of rstudioapi calls in succession from one script, so I'm not sure if optimizing for this particular facet is worth it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems unlikely that users would want to make a large number of rstudioapi calls in succession from one script

I do think this is going to be common. One use of this feature will be to have child R sessions reporting progress to the parent session; for example, knitr could detect when it's being run as a child RStudio process and use rstudioapi to draw progress bars in RStudio as the knit proceeds, instead of its current ASCII bars. knitr can emit dozens of updates in the space of a second; waiting for all of the IPC files to sync and clear will cause the progress bar update time to exceed the knit time!

That said, we could take this as-is now and implement async later if we need it.

Comment thread R/remote.R
data <- list(secret = secret, call = call)
tmp <- tempfile(tmpdir = dirname(request))
saveRDS(data, file = tmp)
file.rename(tmp, request)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What should we do here if the request file already exists?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't ever happen (at least in the current architecture) since RStudio won't allow the child R process to proceed from the polling loop below until that file has been removed:

https://github.com/rstudio/rstudio/pull/5391/files#diff-6fb86e81578c5a08f06852af0b868400R254-R257

@kevinushey

Copy link
Copy Markdown
Contributor Author

Taking this as-is for now but we may need to consider more performant IPC (especially if we want to allow for things like progress bars or other tools that will require more frequent communication)

@dracodoc

dracodoc commented Dec 4, 2019

Copy link
Copy Markdown

Will ipc be a good option for this purpose? Simple case can use file source, more performant case can use redis source.

@kevinushey

Copy link
Copy Markdown
Contributor Author

I don't think ipc helps here, since communication needs to occur between a parent process in C++-land and a child R process. ipc seems like it's geared towards R <-> R communication.

@MariaSemple
MariaSemple deleted the feature/rstudio-ipc-communication branch January 21, 2022 19:19
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

Successfully merging this pull request may close these issues.

3 participants