Skip to content

Commit

Permalink
fix issue #1658
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-buerkner committed May 27, 2024
1 parent 652a7c0 commit b61d495
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Package: brms
Encoding: UTF-8
Type: Package
Title: Bayesian Regression Models using 'Stan'
Version: 2.21.4
Version: 2.21.5
Date: 2024-05-27
Authors@R:
c(person("Paul-Christian", "Bürkner", email = "[email protected]",
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

* Fix a bug that led to partially duplicated Stan code in multilevel terms
thanks to Henrik Singmann. (#1651)
* Fix problems with parallel executions of post-processing functions
sometimes leaving unused R instances behind. Thanks to Andrew Johnson,
Aki Vehtari, and Noa Kallioinen. (#1658)

### Other Changes

Expand Down
17 changes: 10 additions & 7 deletions R/misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -425,13 +425,16 @@ plapply <- function(X, FUN, cores = 1, ...) {
if (cores == 1) {
out <- lapply(X, FUN, ...)
} else {
if (!os_is_windows()) {
out <- parallel::mclapply(X = X, FUN = FUN, mc.cores = cores, ...)
} else {
cl <- parallel::makePSOCKcluster(cores)
on.exit(parallel::stopCluster(cl))
out <- parallel::parLapply(cl = cl, X = X, fun = FUN, ...)
}
cl_type <- ifelse(os_is_windows(), "PSOCK", "FORK")
cl <- parallel::makeCluster(cores, type = cl_type)
# Register a cleanup for the cluster in case the function fails
# Need to wrap in a tryCatch to avoid error if cluster is already stopped
on.exit(tryCatch(
{ parallel::stopCluster(cl) },
error = function(e) invisible(NULL)
))
out <- parallel::parLapply(cl = cl, X = X, fun = FUN, ...)
parallel::stopCluster(cl)
}
out
}
Expand Down

0 comments on commit b61d495

Please sign in to comment.