Skip to content

Objects used within R6 methods not properly exported to multisession workers #804

@pedroliman

Description

@pedroliman

Describe the bug

When using R6, future doesn't seem the objects to export within R6 methods.

Reproduce example

library(R6)
library(future)
library(future.apply)
library(dplyr)

test_class <- R6::R6Class(
  public = list(
    df = data.frame(a = 1),
    simulate = function() {
      self$df %>% mutate(result = a * rnorm(nrow(self$df)))
    },
    run_many_times = function(times=10) {
      results <- future_lapply(1:times, function(i) {
        self$simulate()
      }, future.seed = T)
      return(results)
    }
))

instance <- test_class$new()

plan(sequential)
instance$run_many_times(2)

plan(multicore)
instance$run_many_times(5)

plan(multisession)
instance$run_many_times(5)

The sequential and multicore runs work fine, but the multisession fails with:

Error in self$df %>% mutate(result = a * rnorm(nrow(self$df))) : 
  could not find function "%>%"

Expected behavior

Running the instance$run_many_times returns a list of 5 elements, whether using the default backend, multicore, or multisession.

Session information

Please share your session information after the error has occurred so that we also see which packages and versions are involved;

> sessionInfo()

R version 4.4.0 (2024-04-24)
Platform: aarch64-apple-darwin20
Running under: macOS Sonoma 14.7.6

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.0

Random number generation:
 RNG:     L'Ecuyer-CMRG 
 Normal:  Inversion 
 Sample:  Rejection 
 
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: America/New_York
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] dplyr_1.1.4         future.apply_1.11.2 future_1.58.0       R6_2.5.1           

loaded via a namespace (and not attached):
 [1] digest_0.6.37     codetools_0.2-20  tidyselect_1.2.1  magrittr_2.0.3    glue_1.7.0       
 [6] tibble_3.3.0      parallel_4.4.0    pkgconfig_2.0.3   generics_0.1.4    lifecycle_1.0.4  
[11] cli_3.6.4         parallelly_1.45.1 vctrs_0.6.5       compiler_4.4.0    globals_0.18.0   
[16] tools_4.4.0       listenv_0.9.1     pillar_1.10.2     rlang_1.1.5       jsonlite_1.9.1   

> future::futureSessionInfo()
*** Package versions
future 1.58.0, parallelly 1.45.1, parallel 4.4.0, globals 0.18.0, listenv 0.9.1

*** Allocations
availableCores():
           system /proc/self/status 
               10                10 
availableWorkers():
$system
 [1] "localhost" "localhost" "localhost" "localhost" "localhost" "localhost" "localhost" "localhost"
 [9] "localhost" "localhost"


*** Settings
- future.plan=<not set>
- future.fork.multithreading.enable=<not set>
- future.globals.maxSize=<not set>
- future.globals.onReference=<not set>
- future.resolve.recursive=<not set>
- future.rng.onMisuse=<not set>
- future.wait.timeout=<not set>
- future.wait.interval=<not set>
- future.wait.alpha=<not set>
- future.startup.script=FALSE

*** Backends
Number of workers: 1
List of future strategies:
1. sequential:
   - args: function (..., gc = FALSE, earlySignal = FALSE, envir = parent.frame(), workers = "<NULL>")
   - tweaked: FALSE
   - call: plan(sequential)

*** Basic tests
Main R session details:
   pid     r sysname release
1 2480 4.4.0  Darwin  23.6.0
                                                                                                       version
1 Darwin Kernel Version 23.6.0: Thu Apr 24 20:29:18 PDT 2025; root:xnu-10063.141.1.705.2~1/RELEASE_ARM64_T6000
  nodename machine   login    user effective_user
1  host001   arm64 user002 user001        user001
Worker R session details:
  worker  pid     r sysname release
1      1 2480 4.4.0  Darwin  23.6.0
                                                                                                       version
1 Darwin Kernel Version 23.6.0: Thu Apr 24 20:29:18 PDT 2025; root:xnu-10063.141.1.705.2~1/RELEASE_ARM64_T6000
  nodename machine   login    user effective_user
1  host001   arm64 user002 user001        user001
Number of unique worker PIDs: 1 (as expected)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions