Skip to content

Commit f1ea180

Browse files
Record R option 'future.globals.onReference' when future is created
1 parent f30a3c2 commit f1ea180

File tree

7 files changed

+27
-18
lines changed

7 files changed

+27
-18
lines changed

R/backend_api-Future-class.R

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ Future <- function(expr = NULL, envir = parent.frame(), substitute = TRUE, stdou
124124

125125
args <- list(...)
126126
args_names <- names(args)
127+
if ("onReference" %in% args_names) {
128+
onReference <- args[["onReference"]]
129+
} else {
130+
onReference <- getOption("future.globals.onReference", "ignore")
131+
}
127132

128133
## WORKAROUND: Skip scanning of globals if already done /HB 2021-01-18
129134
if (!is.null(globals)) {
@@ -132,7 +137,7 @@ Future <- function(expr = NULL, envir = parent.frame(), substitute = TRUE, stdou
132137
## Global objects?
133138
## 'persistent' is only allowed for ClusterFuture:s, which will be
134139
## asserted when run() is called /HB 2023-01-17
135-
gp <- getGlobalsAndPackages(expr, envir = envir, tweak = tweakExpression, globals = globals, persistent = isTRUE(args[["persistent"]]))
140+
gp <- getGlobalsAndPackages(expr, envir = envir, tweak = tweakExpression, globals = globals, persistent = isTRUE(args[["persistent"]]), onReference = onReference)
136141
globals <- gp[["globals"]]
137142
expr <- gp[["expr"]]
138143

@@ -230,6 +235,7 @@ Future <- function(expr = NULL, envir = parent.frame(), substitute = TRUE, stdou
230235
core[["label"]] <- label
231236
core[["earlySignal"]] <- earlySignal
232237
core[["gc"]] <- gc
238+
core[["onReference"]] <- onReference
233239
core[["owner"]] <- session_uuid()
234240
counter <- .package[["futureCounter"]] <- .package[["futureCounter"]] + 1L
235241
core[["uuid"]] <- future_uuid(owner = core[["owner"]], counter = counter)
@@ -792,7 +798,7 @@ getFutureContext <- function(future, mc.cores = NULL, local = TRUE, ..., debug =
792798
## Pass down other future.* options
793799
future.globals.maxSize = getOption("future.globals.maxSize"),
794800
future.globals.method = getOption("future.globals.method"),
795-
future.globals.onReference = getOption("future.globals.onReference"),
801+
future.globals.onReference = future[["onReference"]],
796802
future.globals.resolve = getOption("future.globals.resolve"),
797803
future.resolve.recursive = getOption("future.resolve.recursive"),
798804
future.rng.onMisuse = getOption("future.rng.onMisuse"),

R/core_api-future.R

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,10 @@ future <- function(expr, envir = parent.frame(), substitute = TRUE, lazy = FALSE
191191
if (substitute) expr <- substitute(expr)
192192
t_start <- Sys.time()
193193

194+
onReference <- getOption("future.globals.onReference", "ignore")
195+
194196
if (!is.null(globals)) {
195-
gp <- getGlobalsAndPackages(expr, envir = envir, tweak = tweakExpression, globals = globals)
197+
gp <- getGlobalsAndPackages(expr, envir = envir, tweak = tweakExpression, globals = globals, onReference = onReference)
196198
expr <- gp[["expr"]]
197199
globals <- gp[["globals"]]
198200
## Record packages?
@@ -202,7 +204,7 @@ future <- function(expr, envir = parent.frame(), substitute = TRUE, lazy = FALSE
202204
gp <- NULL
203205
attr(globals, "already-done") <- TRUE
204206
}
205-
207+
206208
future <- Future(expr, substitute = FALSE,
207209
envir = envir,
208210
lazy = TRUE,
@@ -214,6 +216,7 @@ future <- function(expr, envir = parent.frame(), substitute = TRUE, lazy = FALSE
214216
earlySignal = earlySignal,
215217
label = label,
216218
gc = gc,
219+
onReference = onReference,
217220
...)
218221

219222
## WORKAROUND: Was argument 'local' specified?

R/core_api-value.R

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ value.Future <- function(future, stdout = TRUE, signal = TRUE, ...) {
4545

4646
value <- result[["value"]]
4747
visible <- result[["visible"]]
48-
if (is.null(visible)) visible <- TRUE
4948

5049
## Always signal immediateCondition:s and as soon as possible.
5150
## They will always be signaled if they exist.
@@ -156,7 +155,7 @@ value.Future <- function(future, stdout = TRUE, signal = TRUE, ...) {
156155

157156

158157
## Check for non-exportable objects in the value?
159-
onReference <- getOption("future.globals.onReference", "ignore")
158+
onReference <- future[["onReference"]]
160159
if (onReference %in% c("error", "warning")) {
161160
new <- tryCatch({
162161
assert_no_references(value, action = onReference, source = "value")
@@ -208,7 +207,7 @@ value.Future <- function(future, stdout = TRUE, signal = TRUE, ...) {
208207
}
209208
}
210209

211-
if (visible) value else invisible(value)
210+
if (isTRUE(visible)) value else invisible(value)
212211
}
213212

214213

R/protected_api-globals.R

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#' @export
3131
#'
3232
#' @keywords internal
33-
getGlobalsAndPackages <- function(expr, envir = parent.frame(), tweak = tweakExpression, globals = TRUE, locals = getOption("future.globals.globalsOf.locals", TRUE), resolve = getOption("future.globals.resolve"), persistent = FALSE, maxSize = getOption("future.globals.maxSize", 500 * 1024 ^ 2), ...) {
33+
getGlobalsAndPackages <- function(expr, envir = parent.frame(), tweak = tweakExpression, globals = TRUE, locals = getOption("future.globals.globalsOf.locals", TRUE), resolve = getOption("future.globals.resolve"), persistent = FALSE, maxSize = getOption("future.globals.maxSize", 500 * 1024 ^ 2), onReference = getOption("future.globals.onReference", "ignore"), ...) {
3434
if (is.null(resolve)) {
3535
resolve <- FALSE
3636
} else {
@@ -385,13 +385,12 @@ getGlobalsAndPackages <- function(expr, envir = parent.frame(), tweak = tweakExp
385385

386386
## Protect against references?
387387
if (length(globals) > 0L) {
388-
action <- getOption("future.globals.onReference", "ignore")
389-
if (action != "ignore") {
388+
if (onReference != "ignore") {
390389
if (debug) {
391-
mdebugf("Checking for globals with references (future.globals.onReference = \"%s\") ...", action, appendLF = FALSE)
390+
mdebugf("Checking for globals with references (future.globals.onReference = \"%s\") ...", onReference, appendLF = FALSE)
392391
}
393392
t <- system.time({
394-
assert_no_references(globals, action = action)
393+
assert_no_references(globals, action = onReference)
395394
}, gcFirst = FALSE)
396395
if (debug) mdebugf("[%.3f s]", t[3])
397396
}

R/protected_api-signalConditions.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
#'
2626
#' @keywords internal
2727
signalConditions <- function(future, include = "condition", exclude = NULL, resignal = TRUE, ...) {
28+
## Nothing to do?
29+
if (length(include) == 0L) return(invisible(future))
30+
2831
## Future is not yet launched
2932
if (!future[["state"]] %in% c("finished", "failed")) {
3033
stop(FutureError(
@@ -34,9 +37,6 @@ signalConditions <- function(future, include = "condition", exclude = NULL, resi
3437
future = future))
3538
}
3639

37-
## Nothing to do?
38-
if (length(include) == 0L) return(invisible(future))
39-
4040
result <- result(future)
4141
stop_if_not(inherits(result, "FutureResult"))
4242

R/utils_api-futureCall.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,18 @@ futureCall <- function(FUN, args = list(), envir = parent.frame(), lazy = FALSE,
3232
# envir <- new.env(parent = envir)
3333

3434
expr <- quote(do.call(what = FUN, args = args))
35-
35+
36+
3637
## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3738
## 1. Global variables
3839
## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3940
if (is.logical(globals)) {
4041
## Gather all globals?
4142
if (globals) {
4243
if (debug) mdebug("Finding globals ...")
43-
44+
onReference <- getOption("future.globals.onReference", "ignore")
4445
# expr <- do.call(call, args = c(list("FUN"), list(...)))
45-
gp <- getGlobalsAndPackages(expr, envir = globalEnv, tweak = tweakExpression, globals = TRUE)
46+
gp <- getGlobalsAndPackages(expr, envir = globalEnv, tweak = tweakExpression, globals = TRUE, onReference = onReference)
4647
globals <- gp[["globals"]]
4748
packages <- unique(c(packages, gp[["packages"]]))
4849
gp <- NULL

man/getGlobalsAndPackages.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)