Skip to content

Commit be38592

Browse files
committed
Handle errors in parallel teardown files
I also changed the exit status of the subprocesses to 0 (normal exit), from 1. I didn't find any reason why it was 1, hopefully there wasn't any. It is a normal exit, so 0 makes more senset to me (now). Closes #1165.
1 parent 14b0541 commit be38592

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

R/parallel.R

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ queue_teardown <- function(queue) {
333333

334334
clean_fn <- function() {
335335
withr::deferred_run(.GlobalEnv)
336-
quit(save = "no", status = 1L, runLast = TRUE)
336+
quit(save = "no", status = 0L, runLast = TRUE)
337337
}
338338

339339
topoll <- list()
@@ -344,7 +344,7 @@ queue_teardown <- function(queue) {
344344
tryCatch(
345345
{
346346
tasks$worker[[i]]$call(clean_fn)
347-
topoll <- c(topoll, tasks$worker[[i]]$get_poll_connection())
347+
topoll <- c(topoll, tasks$worker[[i]])
348348
},
349349
error = function(e) tasks$worker[i] <- list(NULL)
350350
)
@@ -357,10 +357,16 @@ queue_teardown <- function(queue) {
357357
} else {
358358
grace <- 3L
359359
}
360+
first_error <- NULL
360361
limit <- Sys.time() + grace
361362
while (length(topoll) > 0 && (timeout <- limit - Sys.time()) > 0) {
362363
timeout <- as.double(timeout, units = "secs") * 1000
363-
pr <- processx::poll(topoll, as.integer(timeout))
364+
conns <- lapply(topoll, function(x) x$get_poll_connection())
365+
pr <- unlist(processx::poll(conns, as.integer(timeout)))
366+
for (i in which(pr == "ready")) {
367+
msg <- topoll[[i]]$read()
368+
first_error <- first_error %||% msg$error
369+
}
364370
topoll <- topoll[pr != "ready"]
365371
}
366372

@@ -377,6 +383,13 @@ queue_teardown <- function(queue) {
377383
}
378384
}
379385
}
386+
387+
if (!is.null(first_error)) {
388+
cli::cli_abort(
389+
"At least one parallel worker failed to run teardown",
390+
parent = first_error
391+
)
392+
}
380393
}
381394

382395
# Reporter that just forwards events in the subprocess back to the main process
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
test_that("teardown error", {
2-
skip("teardown errors are ignored")
32
withr::local_envvar(TESTTHAT_PARALLEL = "TRUE")
43
err <- tryCatch(
54
capture.output(suppressMessages(testthat::test_local(
@@ -8,6 +7,14 @@ test_that("teardown error", {
87
))),
98
error = function(e) e
109
)
11-
expect_s3_class(err, "testthat_process_error")
12-
expect_match(err$message, "Error in teardown", fixed = TRUE)
10+
expect_s3_class(err$parent, "callr_error")
11+
expect_match(
12+
err$message,
13+
"At least one parallel worker failed to run teardown"
14+
)
15+
expect_match(
16+
err$parent$parent$parent$message,
17+
"Error in teardown",
18+
fixed = TRUE
19+
)
1320
})

0 commit comments

Comments
 (0)