Skip to content

future.apply breaks for functions in custom environments #130

@klmr

Description

@klmr

Describe the bug

‘future.apply’ does not work with functions defined in ’box’ modules, see klmr/box#386. However, the actual issue is unrelated to ‘box’, and can be reproduced with the following minimal example code, where the function passed to future.apply::future_lapply() (or similar) is defined in a custom environment whose parent is not (transitively) the global environment.

Reproduce example

local(
  future.apply::future_sapply(1 : 5, \(x) x),
  envir = new.env(parent = baseenv())
)

Or, similarly:

f = \(x) x
environment(f) = new.env(parent = baseenv())

future.apply::future_sapply(1 : 5, f)

Both of the above result in the following error (on R 4.5 — on previous versions the behaviour differs, and leads to an infinite loop):

Warning: Caught simpleError. Canceling all iterations ...
Error in `parent.env<-`(`*tmp*`, value = <environment>) : 
  cycles in parent chains are not allowed

Expected behavior

The code should work as if being run in the global environment, i.e. it should result in 1 : 5.

Session information

> sessionInfo()
Details
R version 4.5.2 (2025-10-31)
Platform: aarch64-apple-darwin20
Running under: macOS Monterey 12.7.6

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

locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8

time zone: Europe/Zurich
tzcode source: internal

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

other attached packages:
[1] future_1.69.0  nvimcom_0.9.67

loaded via a namespace (and not attached):
[1] compiler_4.5.2      parallelly_1.46.1   parallel_4.5.2      tools_4.5.2        
[5] listenv_0.10.0      future.apply_1.20.1 codetools_0.2-20    digest_0.6.39      
[9] globals_0.19.0     

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions