Skip to content

Changes to .libPaths() not inherited by plan(multisession) #219

Closed

Description

@jcheng5

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.

(cc @aronatkins)

Activity

  1. HenrikBengtsson commented on Apr 25, 2018

    @HenrikBengtsson
    Collaborator

    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?

  2. jcheng5 commented on Apr 25, 2018

    @jcheng5
    Author

    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)

  3. jcheng5 commented on Apr 25, 2018

    @jcheng5
    Author

    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
    
  4. HenrikBengtsson commented on Apr 25, 2018

    @HenrikBengtsson
    Collaborator

    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.

  5. aronatkins commented on Apr 26, 2018

    @aronatkins

    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.

  6. HenrikBengtsson commented on Apr 26, 2018

    @HenrikBengtsson
    Collaborator

    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).

  7. HenrikBengtsson commented on Sep 11, 2018

    @HenrikBengtsson
    Collaborator

    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?

  8. added
    Backend API
    on Feb 15, 2019
  9. markusdumke commented on Mar 20, 2019

    @markusdumke

    Any news on this topic?

    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

    Sys.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.

  10. HenrikBengtsson commented on Mar 20, 2019

    @HenrikBengtsson
    Collaborator

    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:

    libs <- .libPaths()[1]
    libs_cmd <- sprintf(".libPaths(c(%s))", paste(sprintf('"%s"', libs), collapse = ", "))
    cat(libs_cmd) ## .libPaths(c("/home/alice/R/x86_64-pc-linux-gnu-library/3.5"))
    
    cl <- future::makeClusterPSOCK(future::availableCores(), rscript_args = c("-e", shQuote(libs_cmd)))
    
    plan(cluster, workers = cl)

    Possible simplifications

    The above works. As a first step to simplify this, I could add so the above could be done as:

    cl <- future::makeClusterPSOCK(future::availableCores(), libs = libs)
    plan(cluster, workers = cl)

    and then possibly even:

    plan(cluster, workers = future::availableCores(), libs = libs)

    and equivalently

    plan(multisession, libs = libs)

    Things to consider

  11. HenrikBengtsson commented on Mar 26, 2019

    @HenrikBengtsson
    Collaborator

    UPDATE 2019-03-25: The develop branch now has:

    • 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:

    cl <- future::makeClusterPSOCK(2L, rscript_libs = libs)
    plan(cluster, workers = cl)

    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.

  12. tdhock commented on Sep 1, 2020

    @tdhock

    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.

    future::plan("multisession")
    lp <- function(i).libPaths()
    future.apply::future_lapply(1:2, lp)
    tdir <- tempdir()
    future::value(future::future(.libPaths(tdir)))
    future.apply::future_lapply(1:2, lp)

    Output:

    $ 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"
    
    > 
    
  13. HenrikBengtsson commented on Sep 2, 2020

    @HenrikBengtsson
    Collaborator

    makeClusterPSOCK(rscript_libs=.libPaths()) + plan(cluster, workers=cl) works for me.

    That approach is still the best solution, i.e.

    cl <- future::makeClusterPSOCK(2L, rscript_libs = libs)
    plan(cluster, workers = cl)

    future::value(future::future(.libPaths(tdir)))

    You should not use that approach, for a few reasons:

    1. 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().

    2. 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, ...

  14. GrimTrigger88 commented on Sep 21, 2020

    @GrimTrigger88

    This workaround does not work on Databricks clusters:

    libs <- .libPaths()[1]
    libs_cmd <- sprintf(".libPaths(c(%s))", paste(sprintf('"%s"', libs), collapse = ", "))
    cat(libs_cmd) ## .libPaths(c("/home/alice/R/x86_64-pc-linux-gnu-library/3.5"))
    .libPaths(c("/home/christopher.wal-2970021b/R/x86_64-pc-linux-gnu-library/3.6"))>
    cl <- future::makeClusterPSOCK(future::availableCores(), rscript_args = c("-e", shQuote(libs_cmd)))
    plan(cluster, workers = cl)
    Error: Initialization of plan() failed, because the test future used for validation failed. The reason was: Package 'future' is not installed on worker (r_version: 3.6.3 (2020-02-29); platform: x86_64-pc-linux-gnu (64-bit); os: Linux 4.4.0-1112-aws #124-Ubuntu SMP Fri Jul 24 11:10:25 UTC 2020; hostname: 0820-212415-fonts44-10-214-165-115)`

  15. HenrikBengtsson commented on Sep 21, 2020

    @HenrikBengtsson
    Collaborator

    @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.

  16. GrimTrigger88 commented on Oct 4, 2020

    @GrimTrigger88

    @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.

  17. HenrikBengtsson commented on Oct 20, 2020

    @HenrikBengtsson
    Collaborator

    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 worker
    prepend_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"
  18. added this to the Next release milestone on Oct 20, 2020
  19. HenrikBengtsson commented on Oct 20, 2020

    @HenrikBengtsson
    Collaborator

    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 now
    
    r_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.

    > parallel::clusterEvalQ(cl, { .Library.site })
    [[1]]
    [1] "/home/hb/my_pwd/" "/tmp/hb/RtmpmOA9qt"
    
  20. HenrikBengtsson commented on Oct 21, 2020

    @HenrikBengtsson
    Collaborator

    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:

    plan(multisession, workers = 2L, rscript_libs = .libPaths())

    I've created #435 for the discussion whether or not this should be the new default. Closing this very long thread.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Backend APIfeature requestpkg/parallelly

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions