Skip to content

Commit fde9720

Browse files
Merge pull request #177 from tidymodels/weird-fix
do weird thing to fix strange bug
2 parents af1f1a1 + e75c68c commit fde9720

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

Diff for: R/tune_cluster.R

+16-5
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,18 @@ tune_cluster_loop <- function(resamples,
214214
if (identical(parallel_over, "resamples")) {
215215
seeds <- generate_seeds(rng, n_resamples)
216216

217-
suppressPackageStartupMessages(
218-
results <- foreach::foreach(
217+
# We are wrapping in `local()` here because `fn_tune_grid_loop_iter_safely()` adds
218+
# on.exit/deferred handlers to our execution frame by passing `tune_env$progress_env`
219+
# to cli's progress bar constructor, which then adds an exit handler on that
220+
# environment. Because `%op%` evaluates its expression in `eval()` in the calling
221+
# environment (i.e. here), the handlers are added in the special frame environment
222+
# created by `eval()`. This causes the handler to run much too early. By evaluating in
223+
# a local environment, we prevent `defer()`/`on.exit()` from finding the short-lived
224+
# context of `%op%`. Instead it looks all the way up here to register the handler.
225+
226+
results <- local({
227+
suppressPackageStartupMessages(
228+
foreach::foreach(
219229
split = splits,
220230
seed = seeds,
221231
.packages = packages,
@@ -237,11 +247,12 @@ tune_cluster_loop <- function(resamples,
237247
)
238248
}
239249
)
250+
})
240251
} else if (identical(parallel_over, "everything")) {
241252
seeds <- generate_seeds(rng, n_resamples * n_grid_info)
242253

243-
suppressPackageStartupMessages(
244-
results <- foreach::foreach(
254+
results <- local(suppressPackageStartupMessages(
255+
foreach::foreach(
245256
iteration = iterations,
246257
split = splits,
247258
.packages = packages,
@@ -271,7 +282,7 @@ tune_cluster_loop <- function(resamples,
271282
seed = seed
272283
)
273284
}
274-
)
285+
))
275286
} else {
276287
rlang::abort("Internal error: Invalid `parallel_over`.")
277288
}

0 commit comments

Comments
 (0)