Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ Suggests:
pkgload,
rmarkdown,
Rmpfr,
testthat (>= 3.0.0)
testthat (>= 3.1.7)
VignetteBuilder: knitr
Config/testthat/edition: 3
28 changes: 20 additions & 8 deletions R/bootstrap.R
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,7 @@ start_mirai_bootstrap <- function(inputs, object) {
mapped <- tryCatch(
mirai::mirai_map(
batches,
function(batch, original_fit, dev_path) {
if (!is.null(dev_path) && !pkgload::is_dev_package("singlehit")) {
pkgload::load_all(dev_path, quiet = TRUE)
}
dplyr::bind_rows(lapply(batch, function(task) {
singlehit:::bootstrap_refit(original_fit, task$sample_data, task$replicate)
}))
},
bootstrap_worker(),
.args = list(original_fit = object, dev_path = dev_path),
.compute = compute
),
Expand All @@ -245,6 +238,25 @@ start_mirai_bootstrap <- function(inputs, object) {
)
}

# The function mirai sends to each daemon with every batch. Its environment is
# globalenv() so that mirai does not serialize the frame that created it, which
# holds the full task list. The body uses only namespaced calls, so it needs no
# free variables from that frame. Source references are removed because under
# pkgload::load_all() they would carry the whole source file with each batch.
bootstrap_worker <- function() {
worker <- function(batch, original_fit, dev_path) {
if (!is.null(dev_path) && !pkgload::is_dev_package("singlehit")) {
pkgload::load_all(dev_path, quiet = TRUE)
}
dplyr::bind_rows(lapply(batch, function(task) {
singlehit:::bootstrap_refit(original_fit, task$sample_data, task$replicate)
}))
}
worker <- utils::removeSource(worker)
environment(worker) <- globalenv()
worker
}

bind_bootstrap_results <- function(results) {
failed <- vapply(results, function(result) inherits(result, c("miraiError", "errorValue")), logical(1))
if (any(failed)) {
Expand Down
39 changes: 39 additions & 0 deletions tests/testthat/test-bootstrap-dispatch.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
test_that("the mirai worker does not capture the frame that creates it", {
worker <- bootstrap_worker()

# A closure built inside start_mirai_bootstrap() would carry that frame, which
# holds every task, and mirai would serialize it with each batch.
expect_identical(environment(worker), globalenv())
expect_lt(length(serialize(worker, NULL)), 10000L)
})

test_that("the mirai worker refits a batch exactly like the sequential path", {
fit <- fit_dose_response(ward_fixture(), "beta_poisson")
tasks <- generate_bootstrap_tasks(fit, fit$data$response, 3L, 42L)

batched <- bootstrap_worker()(tasks, fit, NULL)
sequential <- run_sequential_bootstrap(tasks, fit)

expect_equal(tibble::as_tibble(batched), tibble::as_tibble(sequential))
})

test_that("start_mirai_bootstrap sends mirai the detached worker", {
skip_if_not_installed("mirai")
fit <- fit_dose_response(ward_fixture(), "beta_poisson")
inputs <- prepare_bootstrap(fit, 3L, "observed", 42L, "mirai", NULL, NULL)
captured <- NULL
local_mocked_bindings(
daemons_set = function(...) TRUE,
mirai_map = function(.x, .f, ...) {
captured <<- .f
list()
},
.package = "mirai"
)

job <- start_mirai_bootstrap(inputs, fit)

expect_s3_class(job, "qdr_bootstrap_job")
expect_identical(environment(captured), globalenv())
expect_lt(length(serialize(captured, NULL)), 10000L)
})
Loading