eval_fork: Safe Evaluation

Description Usage Arguments Details Examples

View source: R/fork.R

Description

Evaluates an expression in a temporary fork and returns the value without any side effects on the main R session. For eval_safe() the expression is wrapped in additional R code to handle errors and graphics.

Usage

1
2
3
4
5
6
eval_safe(expr, tmp = tempfile("fork"), std_out = stdout(),
  std_err = stderr(), timeout = 0, priority = NULL, uid = NULL,
  gid = NULL, rlimits = NULL, profile = NULL, device = pdf)

eval_fork(expr, tmp = tempfile("fork"), std_out = stdout(),
  std_err = stderr(), timeout = 0)

Arguments

expr

expression to evaluate

tmp

the value of tempdir() inside the forked process

std_out

if and where to direct child process STDOUT. Must be one of TRUE, FALSE, filename, connection object or callback function. See section on Output Streams below for details.

std_err

if and where to direct child process STDERR. Must be one of TRUE, FALSE, filename, connection object or callback function. See section on Output Streams below for details.

timeout

maximum time in seconds to allow for call to return

priority

(integer) priority of the child process. High value is low priority. Non root user may only raise this value (decrease priority)

uid

evaluate as given user (uid or name). See unix::setuid(), only for root.

gid

evaluate as given group (gid or name). See unix::setgid() only for root.

rlimits

named vector/list with rlimit values, for example: c(cpu = 60, fsize = 1e6).

profile

AppArmor profile, see RAppArmor::aa_change_profile(). Requires the RAppArmor package (Debian/Ubuntu only)

device

graphics device to use in the fork, see dev.new()

Details

Some programs such as Java are not fork-safe and cannot be called from within a forked process if they have already been loaded in the main process. On MacOS any software calling CoreFoundation functionality might crash within the fork. This includes libcurl which has been built on OSX against native SecureTransport rather than OpenSSL for https connections. The same limitations hold for e.g. parallel::mcparallel().

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#Only works on Unix
if(.Platform$OS.type == "unix"){

# works like regular eval:
eval_safe(rnorm(5))

# Exceptions get propagated
test <- function() { doesnotexit() }
tryCatch(eval_safe(test()), error = function(e){
  cat("oh no!", e$message, "\n")
})

# Honor interrupt and timeout, even inside C evaluations
try(eval_safe(svd(matrix(rnorm(1e8), 1e4)), timeout = 2))

# Capture output
outcon <- rawConnection(raw(0), "r+")
eval_safe(print(sessionInfo()), std_out = outcon)
cat(rawToChar(rawConnectionValue(outcon)))
close(outcon)
}

jeroen/sys documentation built on Dec. 17, 2018, 10:09 a.m.