@@ -214,8 +214,18 @@ tune_cluster_loop <- function(resamples,
214
214
if (identical(parallel_over , " resamples" )) {
215
215
seeds <- generate_seeds(rng , n_resamples )
216
216
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(
219
229
split = splits ,
220
230
seed = seeds ,
221
231
.packages = packages ,
@@ -237,11 +247,12 @@ tune_cluster_loop <- function(resamples,
237
247
)
238
248
}
239
249
)
250
+ })
240
251
} else if (identical(parallel_over , " everything" )) {
241
252
seeds <- generate_seeds(rng , n_resamples * n_grid_info )
242
253
243
- suppressPackageStartupMessages(
244
- results <- foreach :: foreach(
254
+ results <- local( suppressPackageStartupMessages(
255
+ foreach :: foreach(
245
256
iteration = iterations ,
246
257
split = splits ,
247
258
.packages = packages ,
@@ -271,7 +282,7 @@ tune_cluster_loop <- function(resamples,
271
282
seed = seed
272
283
)
273
284
}
274
- )
285
+ ))
275
286
} else {
276
287
rlang :: abort(" Internal error: Invalid `parallel_over`." )
277
288
}
0 commit comments