Skip to content

Commit

Permalink
guard against invalid mc.cores values (#2086)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinushey committed Feb 4, 2025
1 parent 731463e commit ff88572
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
15 changes: 9 additions & 6 deletions R/parallel.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ renv_parallel_cores <- function() {
if (renv_platform_windows())
return(1L)

value <- config$updates.parallel()
if (identical(value, TRUE)) {
parallel <- requireNamespace("parallel", quietly = TRUE)
getOption("mc.cores", default = if (parallel) 2L else 1L)
} else if (identical(value, FALSE)) {
parallel <- config$updates.parallel()
value <- if (identical(parallel, TRUE)) {
requireNamespace("parallel", quietly = TRUE)
getOption("mc.cores", default = 2L)
} else if (identical(parallel, FALSE)) {
1L
} else {
as.integer(value)
parallel
}

ok <- is.numeric(value) && length(value) == 1L && !is.na(value)
if (ok) value else 1L

}

renv_parallel_exec <- function(data, callback) {
Expand Down
21 changes: 21 additions & 0 deletions tests/testthat/test-update.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,24 @@ test_that("can upgrade gitlab", {
expect_equal(updated$skeleton$RemoteSha, latest$RemoteSha)

})

test_that("we guard against invalid mc.cores values", {

renv_scope_options(renv.config.updates.parallel = TRUE)

local({
renv_scope_options(mc.cores = 4L)
expect_equal(renv_parallel_cores(), 4L)
})

local({
renv_scope_options(mc.cores = NA)
expect_equal(renv_parallel_cores(), 1L)
})

local({
renv_scope_options(mc.cores = "oops")
expect_equal(renv_parallel_cores(), 1L)
})

})

0 comments on commit ff88572

Please sign in to comment.