You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Changes to .libPaths() not inherited by plan(multisession) #219
As we are doing our final testing on the promises/Shiny async work, we ran into some problems testing plan(multisession) with RStudio Connect. We get the error message in #216, even though we're running workers locally.
The problem is that Connect modifies .libPaths() in the R process, and that change is not inherited by the workers that are spawned.
We're going to modify Connect so it also also modifies the R_LIBS_USER (or whatever) environment variable, but we thought you might also consider having future propagate .libPaths customizations.
I see that you have a workaround for covr specifically, here; and indeed, library(covr) also fixes the problem for Connect.
I think I understand the gist of the problem your describing. For my efficiency, and lower the risk for miscommunication, could you please create a minimal reproducible example that emulates the problem based on code using just library(future) and plan(multisession) that I can work with?
Let me know if you have a problem with docker and I can rewrite this.
Put the following in a Dockerfile file in an empty directory:
FROM rocker/r-base
RUN mkdir /project && cd /project && mkdir lib && R -e ".libPaths('./lib'); install.packages('future')"
WORKDIR /project
CMD R -e ".libPaths('./lib'); library(future); plan(multisession); f <- future({ 'hello' }); print(value(f))"
then
docker build . -t future-libpath
docker run --rm future-libpath
Thanks. Just looking at that Dockerfile clarifies it. This should be all I need. This will also useful for setting up a package test for the problem in Issue #216.
We are working around this problem in RStudio Connect by configuring R_LIBS whenever prepending a custom R library path to .libPaths().
// given libdir with the custom R library:
.libPaths(c(libdir, .libPaths()))
Sys.setenv(R_LIBS = paste(libdir, Sys.getenv("R_LIBS"), sep=.Platform$path.sep))
The combination of these two techniques correctly configures the current R session and makes it more likely that child R processes use the same R libraries.
We will probably keep this code in place even after this issue is resolved to aid other process launching code.
Thanks for re-iterating that your R_LIBS strategy works and that you'll plan to keep it. That helps coming up with a "sustainable" strategy in the future ecosystem rather than a quick patch for certain backends. This FR generalizes to other env vars and also options (Issue #134).
A quick thought about this one. Rather than specifying / controlling the library paths per future, it should be enough to control this when setting up the future plan, correct? Something like:
plan(multisession, libs= .libPaths())
One might even argue that libs = .libPaths() should be the new default for all localhost backends.
Comments?
Reacted by Joe Cheng, Markus Dumke, Laurent Faucheux, yonicd, Matthew Landis and atomman
What is the suggested workaround for other package authors, who like to use future inside a package (e.g. via future.apply, foreach or furrr). Would you recommend to put the suggested workaround
Agree. It's not good to have this hardcoded in the code, especially since that would not work on all backends. Unfortunately, nothing new available - only strategic thoughts such as trying to distill a unified API for controlling future backends.
Manual workaround
Here's a poor man's solution that gives the user the control over this when it comes to PSOCK clusters:
makeClusterPSOCK() gained support for argument 'rscript_libs' to control the R package library search path on the workers. For example, to prepend the folder '~/R-libs' on the workers, use 'rscript_libs = c("~/R-libs", "*")', where "*" will be resolved to the current '.libPaths()' on the workers.
This means that we now can override the package library path on the workers as:
I'm getting this issue too. makeClusterPSOCK(rscript_libs=.libPaths()) + plan(cluster, workers=cl) works for me.
Is there any plan to get support using plan(multisession) or should we just switch to using plan(cluster) ?
The other work-around, calling .libPaths(my.lib) in the future/worker code, works for me. Using that approach revealed that the workers are stateful -- is that intended? If so can you please add documentation?
For example I was surprised by the following result... setting
1 running .libPaths() in two workers via future_lapply gives the default library.
2. running .libPaths(my.lib) in one worker via value/future adds that library (for just the first worker).
3. re-running .libPaths() in two workers via future_lapply (same code as before) gives a different result, showing that the new library is only present on the first worker.
$ R --vanilla < future-lib.R
R version 4.0.2 (2020-06-22) -- "Taking Off Again"
Copyright (C) 2020 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> future::plan("multisession")
> lp <- function(i).libPaths()
> future.apply::future_lapply(1:2, lp)
[[1]]
[1] "C:/Program Files/R/R-4.0.2/library"
[[2]]
[1] "C:/Program Files/R/R-4.0.2/library"
> tdir <- tempdir()
> future::value(future::future(.libPaths(tdir)))
> future.apply::future_lapply(1:2, lp)
[[1]]
[1] "C:/Users/th798/AppData/Local/Temp/RtmpAfH1ES"
[2] "C:/Program Files/R/R-4.0.2/library"
[[2]]
[1] "C:/Program Files/R/R-4.0.2/library"
>
You should not use that approach, for a few reasons:
You cannot control which workers a future ends up running on, i.e. you cannot use futures per se to configure your workers. That is something that needs to be done when the workers are setup. The best approach rightnow is therefore to do it via makeClusterPSOCK().
More importantly, that code assumes that folder tdir exists on the worker where that future is evaluated. If you switch your plan() so that the workers are on a different system, then that code might not work. That is, this code fails the "write-once-run-anywhere" sniff test.
Using that approach revealed that the workers are stateful -- is that intended? If so can you please add documentation?
Yes, you can change some of the state of cluster (and therefore multisession) workers, and any other backend with non-transient workers. Though, it should not be assumed and not relied up. It would be possible to reset the .libPaths() state after each future evaluation. The downside is that it adds yet another amount of overhead. The same goes for environment variables, R options, attached packages, local files, ...
Another thing that confuses me is that your .libPaths()[1] suggests you're running R 3.5.* on that machine, yet, makeClusterPSOCK()+plan() reports that it's actually running R 3.6.3.
Have you installed the future package on the '0820-212415-fonts44-10-214-165-115' machine where you try to spin up a worker. If not, you must do that. The future framework/maksClusterPSOCK() does not install or "transfer" R packages on the fly.
PS. Please try to format your code with Markdown to make it easier to parse and less error prone.
@HenrikBengtsson, I was just copying your manual workaround code - the "Alice" example directory is commented out (though a good example of why Markdown is useful). The problem I had was caused by either the fact that the future package - among others - existed in both the system library and the user library. However, some packages that needed to be accessed by other sessions were only available in one place or the other.
I believe this was the root of the issue in detecting packages properly on our cluster. We worked around this by clearing out duplicated packages and only using the installation of future on the cluster (system library). We haven't had problems since.
In the next release, currently in the develop branch, one can pass any makeClusterPSOCK() argument via plan(). For this particular example, it means we can now do:
library(future)
## Prepend two local folders to the library path on the workerprepend_libs<- c(getwd(), tempdir())
plan(multisession, workers=2L, rscript_libs= c(prepend_libs, "*"))
f<- future(.libPaths())
value(f)
## [1] "/home/hb/repositories/future"## [2] "/tmp/hb/Rtmp2nd5k6" ## [3] "/home/hb/R/x86_64-pc-linux-gnu-library/4.0-custom"## [4] "/home/hb/software/R-devel/tags/R-4-0-3/lib/R/library"
For completeness, since future 1.18.0, and hence in the new parallelly 1.20.0 package, we can do the following on Unix-like systems:
library(parallelly) ## or library(future) since it re-exports makeClusterPSOCK() for nowr_libs_site<- c(getwd(), tempdir())
R_LIBS_SITE<- paste0("R_LIBS_SITE=", shQuote(paste(user_libs, collapse=.Platform$path.sep)))
cl<- makeClusterPSOCK(1L, rscript= c(R_LIBS_SITE, file.path(R.home("bin"), "Rscript")))
This will launch the R worker using Linux-style R_LIBS_SITE='/some/path:/another/path' Rscript .... This does not work in MS Windows. This way, R will set the package site library on startup, e.g.
So, there's now a few manual workarounds for having multisession workers inheriting .libPaths(). In the future world, the one that works on all platforms is:
Activity
HenrikBengtsson commentedon Apr 25, 2018
I think I understand the gist of the problem your describing. For my efficiency, and lower the risk for miscommunication, could you please create a minimal reproducible example that emulates the problem based on code using just
library(future)andplan(multisession)that I can work with?jcheng5 commentedon Apr 25, 2018
Sure. Would it work if I provide the example in Dockerfile form? (Since it works best if you don't have future installed in the system-wide R library)
jcheng5 commentedon Apr 25, 2018
Let me know if you have a problem with docker and I can rewrite this.
Put the following in a Dockerfile file in an empty directory:
then
HenrikBengtsson commentedon Apr 25, 2018
Thanks. Just looking at that Dockerfile clarifies it. This should be all I need. This will also useful for setting up a package test for the problem in Issue #216.
TESTS: Add test for Issues #214, #215, #216, #219
aronatkins commentedon Apr 26, 2018
We are working around this problem in RStudio Connect by configuring
R_LIBSwhenever prepending a custom R library path to.libPaths().The combination of these two techniques correctly configures the current R session and makes it more likely that child R processes use the same R libraries.
We will probably keep this code in place even after this issue is resolved to aid other process launching code.
HenrikBengtsson commentedon Apr 26, 2018
Thanks for re-iterating that your
R_LIBSstrategy works and that you'll plan to keep it. That helps coming up with a "sustainable" strategy in the future ecosystem rather than a quick patch for certain backends. This FR generalizes to other env vars and also options (Issue #134).HenrikBengtsson commentedon Sep 11, 2018
A quick thought about this one. Rather than specifying / controlling the library paths per future, it should be enough to control this when setting up the future plan, correct? Something like:
One might even argue that
libs = .libPaths()should be the new default for all localhost backends.Comments?
markusdumke commentedon Mar 20, 2019
Any news on this topic?
What is the suggested workaround for other package authors, who like to use
futureinside a package (e.g. viafuture.apply,foreachorfurrr). Would you recommend to put the suggested workaroundSys.setenv(R_LIBS = paste0(.libPaths()[1], Sys.getenv("R_LIBS"), sep = .Platform$path.sep))inside a function? This does not feel like a good solution to me.
HenrikBengtsson commentedon Mar 20, 2019
Agree. It's not good to have this hardcoded in the code, especially since that would not work on all backends. Unfortunately, nothing new available - only strategic thoughts such as trying to distill a unified API for controlling future backends.
Manual workaround
Here's a poor man's solution that gives the user the control over this when it comes to PSOCK clusters:
Possible simplifications
The above works. As a first step to simplify this, I could add so the above could be done as:
and then possibly even:
and equivalently
Things to consider
libsfor all backends?libs = .libPaths()the default for all local workers?.libPaths(), orR_LIBS(_USER)?makeClusterPSOCK() gained support for argument 'rscript_libs' [#219]
HenrikBengtsson commentedon Mar 26, 2019
UPDATE 2019-03-25: The develop branch now has:
This means that we now can override the package library path on the workers as:
To prepend, instead of overriding, use
rscript_libs = c(libs, "*").EDIT: Using
Sys.getenv("R_LIBS_USER")in the example was not useful, because that env vars are inherited by the R worker processes.tdhock commentedon Sep 1, 2020
I'm getting this issue too. makeClusterPSOCK(rscript_libs=.libPaths()) + plan(cluster, workers=cl) works for me.
Is there any plan to get support using plan(multisession) or should we just switch to using plan(cluster) ?
The other work-around, calling .libPaths(my.lib) in the future/worker code, works for me. Using that approach revealed that the workers are stateful -- is that intended? If so can you please add documentation?
For example I was surprised by the following result... setting
1 running .libPaths() in two workers via future_lapply gives the default library.
2. running .libPaths(my.lib) in one worker via value/future adds that library (for just the first worker).
3. re-running .libPaths() in two workers via future_lapply (same code as before) gives a different result, showing that the new library is only present on the first worker.
Output:
HenrikBengtsson commentedon Sep 2, 2020
That approach is still the best solution, i.e.
You should not use that approach, for a few reasons:
You cannot control which workers a future ends up running on, i.e. you cannot use futures per se to configure your workers. That is something that needs to be done when the workers are setup. The best approach rightnow is therefore to do it via
makeClusterPSOCK().More importantly, that code assumes that folder
tdirexists on the worker where that future is evaluated. If you switch yourplan()so that the workers are on a different system, then that code might not work. That is, this code fails the "write-once-run-anywhere" sniff test.Yes, you can change some of the state of
cluster(and thereforemultisession) workers, and any other backend with non-transient workers. Though, it should not be assumed and not relied up. It would be possible to reset the.libPaths()state after each future evaluation. The downside is that it adds yet another amount of overhead. The same goes for environment variables, R options, attached packages, local files, ...GrimTrigger88 commentedon Sep 21, 2020
This workaround does not work on Databricks clusters:
HenrikBengtsson commentedon Sep 21, 2020
@GrimTrigger88, I don't understand your example. For instance, why do you call:
.libPaths(c("/home/christopher.wal-2970021b/R/x86_64-pc-linux-gnu-library/3.6"))?
Another thing that confuses me is that your
.libPaths()[1]suggests you're running R 3.5.* on that machine, yet,makeClusterPSOCK()+plan()reports that it's actually running R 3.6.3.Have you installed the future package on the '0820-212415-fonts44-10-214-165-115' machine where you try to spin up a worker. If not, you must do that. The future framework/
maksClusterPSOCK()does not install or "transfer" R packages on the fly.PS. Please try to format your code with Markdown to make it easier to parse and less error prone.
GrimTrigger88 commentedon Oct 4, 2020
@HenrikBengtsson, I was just copying your manual workaround code - the "Alice" example directory is commented out (though a good example of why Markdown is useful). The problem I had was caused by either the fact that the future package - among others - existed in both the system library and the user library. However, some packages that needed to be accessed by other sessions were only available in one place or the other.
I believe this was the root of the issue in detecting packages properly on our cluster. We worked around this by clearing out duplicated packages and only using the installation of future on the cluster (system library). We haven't had problems since.
Now it's possible to pass any makeClusterPSOCK() argument via plan()
HenrikBengtsson commentedon Oct 20, 2020
In the next release, currently in the develop branch, one can pass any
makeClusterPSOCK()argument viaplan(). For this particular example, it means we can now do:HenrikBengtsson commentedon Oct 20, 2020
For completeness, since future 1.18.0, and hence in the new parallelly 1.20.0 package, we can do the following on Unix-like systems:
This will launch the R worker using Linux-style
R_LIBS_SITE='/some/path:/another/path' Rscript .... This does not work in MS Windows. This way, R will set the package site library on startup, e.g.HenrikBengtsson commentedon Oct 21, 2020
So, there's now a few manual workarounds for having multisession workers inheriting
.libPaths(). In the future world, the one that works on all platforms is:I've created #435 for the discussion whether or not this should be the new default. Closing this very long thread.