Skip to content

Commit

Permalink
Add optional assertion of the internal Future 'state' field [#667]
Browse files Browse the repository at this point in the history
  • Loading branch information
HenrikBengtsson committed Feb 20, 2023
1 parent ede6dda commit ae6f137
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 6 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: future
Version: 1.31.0-9005
Version: 1.31.0-9006
Title: Unified Parallel and Distributed Processing in R for Everyone
Imports:
digest,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

S3method("$<-",Future)
S3method("[",FutureGlobals)
S3method("[",sessionDetails)
S3method(as.FutureGlobals,FutureGlobals)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Deprecated and Defunct

* Add optional assertion of the internal Future `state` field.

* Deprecated `plan(multiprocess, ...)` is now defunct when running in
interactive mode. The next step is to make it defunct also when
running in batch mode.
Expand Down
42 changes: 37 additions & 5 deletions R/Future-class.R
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,12 @@ Future <- function(expr = NULL, envir = parent.frame(), substitute = TRUE, stdou
## /HB 2023-02-09
if (isTRUE(args$local) &&
Sys.getenv("R_FUTURE_CHECK_IGNORE_CIVIS", "true") == "true") {
for (call in sys.calls()) {
if ("CivisFuture" %in% as.character(call[[1]])) {
for (call in sys.calls()) {
if ("CivisFuture" %in% as.character(call[[1]])) {
msg <- sprintf("%s. In this case it was because civis::CivisFuture() was used. Please contact the maintainers of the 'civis' package about this problem.", msg)
if (!interactive()) dfcn <- .Deprecated
break
}
if (!interactive()) dfcn <- .Deprecated
break
}
}
}

Expand Down Expand Up @@ -820,6 +820,7 @@ getExpression.Future <- local({
} ## getExpression()
})


globals <- function(future, ...) UseMethod("globals")

globals.Future <- function(future, ...) {
Expand All @@ -831,3 +832,34 @@ packages <- function(future, ...) UseMethod("packages")
packages.Future <- function(future, ...) {
future[["packages"]]
}


#' @export
`$<-.Future` <- function(x, name, value) {
if (name == "state") {
if (!is.element(value, c("created", "running", "finished", "failed", "interrupted"))) {
action <- getOption("future.state.onInvalid", "warning")

## FIXME: civis::CivisFuture uses 'succeeded' /HB 2019-06-18
if (Sys.getenv("R_FUTURE_CHECK_IGNORE_CIVIS", "true") == "true") {
for (call in sys.calls()) {
if ("CivisFuture" %in% as.character(call[[1]])) {
action <- "ignore"
break
}
}
}

if (action != "ignore") {
msg <- sprintf("Trying to assign an invalid value to the internal '%s' field of a %s object: %s", name, class(x)[1], value)
if (action == "error") {
stop(FutureError(msg, call = sys.call(), future = x))
} else {
warning(FutureWarning(msg, call = sys.call(), future = x))
}
}
}
}

NextMethod()
}
3 changes: 3 additions & 0 deletions R/options.R
Original file line number Diff line number Diff line change
Expand Up @@ -315,4 +315,7 @@ update_package_options <- function(debug = FALSE) {
## SETTINGS USED FOR DEPRECATING FEATURES
## future 1.22.0:
update_package_option("future.globals.keepWhere", mode = "logical", debug = debug)

## future 1.32.0:
update_package_option("future.state.onInvalid", mode = "character", debug = debug)
}

0 comments on commit ae6f137

Please sign in to comment.