You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've got a simple R script that downloads files in parallel. I've recently switched from the base lapply to future_lapply and found that while it works great with plan(multisession), it doesn't work with plan(multicore) (nor with plan(multiprocess)).
I have this small block of code:
library(future.apply)
options(doFuture.debug=TRUE)
#plan(sequential)#plan(multisession)#plan(multiprocess)
plan(multicore)
url_base<-"https://cloud.r-project.org/src/contrib/"# first two should work, last element should not be foundfiles<- c("A3_1.0.0.tar.gz", "ABC.RAP_0.9.0.tar.gz", "non_existant.tar.gz")
future_lapply(files, function(s) {
tryCatch({
download.file(paste0(url_base, s), s)
s
},
error=function(e) {
print(paste(e))
NULL
})
})
Output is
Error: Unexpected result (of class ‘NULL’ != ‘FutureResult’) retrieved for MulticoreFuture future (label = ‘<none>’, expression = ‘{; ...future.globals.maxSize.org <- getOption("future.globals.maxSize"); if (!identical(...future.globals.maxSize.org, ...future.globals.maxSize)) {; oopts <- options(future.globals.maxSize = ...future.globals.maxSize); on.exit(options(oopts), add = TRUE); }; ...; ...future.FUN(...future.X_jj, ...); }); }’):
Swamped, but some very quick feedback: Maybe download.file() for some choices of method does not work in forked processes and/or on macOS. That Unexpected result (of class ‘NULL’ != ‘FutureResult’) error message suggests that the forked worker has actually terminated (e.g. core dumped). If you install:
and retry you should get a little bit more information around that error. Please retry and see what you get. (I won't fix your problem but it will help narrowing it down).
Error: Failed to retrieve the result of MulticoreFuture (<none>) from the forked worker (on localhost; PID 52198). Post-mortem diagnostic: No process exists with this PID, i.e. the forked localhost worker is no longer alive.
Full error output attached.
It appears that the error is indeed with download.file() - I realized that it actually can use different methods and that the default one - libcurl - seems to be broken with plan(multicore) on macOS.
PS. The reason why I tweaked your example is that if you use tryCatch(..., error = function(ex) ... NULL) to return NULL on failure you will not be able to tell the difference from a crashed worker when using parallel::mclapply() (which returns NULL for crashed workers).
The simpler code example you provided using future.apply does fail - same error as before:
Error: Failed to retrieve the result of MulticoreFuture (<none>) from the forked worker (on localhost; PID 59293). Post-mortem diagnostic: No process exists with this PID, i.e. the forked localhost worker is no longer alive.
The res object does not get created.
The second example also fails; however in this second case res is:
> res
[[1]]
[1] "Error in download.file(paste0(url_base, s), s) : \n cannot open URL 'https://cloud.r-project.org/src/contrib/A3_1.0.0.tar.gz'\n"
attr(,"class")
[1] "try-error"
attr(,"condition")
<simpleError in download.file(paste0(url_base, s), s): cannot open URL 'https://cloud.r-project.org/src/contrib/A3_1.0.0.tar.gz'>
[[2]]
[1] "Error in download.file(paste0(url_base, s), s) : \n cannot open URL 'https://cloud.r-project.org/src/contrib/ABC.RAP_0.9.0.tar.gz'\n"
attr(,"class")
[1] "try-error"
attr(,"condition")
<simpleError in download.file(paste0(url_base, s), s): cannot open URL 'https://cloud.r-project.org/src/contrib/ABC.RAP_0.9.0.tar.gz'>
[[3]]
[1] "Error in download.file(paste0(url_base, s), s) : \n cannot open URL 'https://cloud.r-project.org/src/contrib/non_existant.tar.gz'\n"
attr(,"class")
[1] "try-error"
attr(,"condition")
<simpleError in download.file(paste0(url_base, s), s): cannot open URL 'https://cloud.r-project.org/src/contrib/non_existant.tar.gz'>
8 Warnings are returned - pretty much all identical except for what I assume is the PID - first 2 shown below:
Warning messages:
Warning messages:
1: In selectChildren(ac[!fin], -1) :
cannot wait for child 60839 as it does not exist
2: In selectChildren(ac[!fin], -1) :
cannot wait for child 60838 as it does not exist
If I tweak the mclapply example to only use 1 core (mc.cores = 1) it does work correctly; 2 or higher results in the error.
So, does this mean my machine is hosed, or is this a weird edge case bug (feature?) with download.file()?
For the sake of troubleshooting this one, I want to get to the point where it also crash/fail when using only the parallel package, because I believe (99.999%) that the problem has to do with forking and download.file(..., method = "libcurl") - and that it has nothing to do with the future framework. The plan(multicore) is basically utilizing the same code as parallel::mclapply() does internally.
Before anything else, what is your sessionInfo()?
It's weird that your run using parallel::mclapply() failed to download anything. So, does it work do download only the first file or the first two files? That is, try with:
FYI, setting mc.cores = 1 is effectively the same as calling lapply() instead of mclapply(), so then there is no forking or parallelization involved. In other words, that doesn't help the troubleshooting/narrowing down of the problem.
FYI2, I don't have access to macOS. I'm on Linux, where forking is also supported, but I cannot reproduce your problem. All code snippets work as expected.
> sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-apple-darwin16.7.0 (64-bit)
Running under: macOS Sierra 10.12.6
Matrix products: default
BLAS/LAPACK: /usr/local/Cellar/openblas/0.3.3/lib/libopenblasp-r0.3.3.dylib
locale:
[1] en_US/en_US/en_US/C/en_US/en_US
attached base packages:
[1] parallel stats graphics grDevices utils datasets methods
[8] base
loaded via a namespace (and not attached):
[1] compiler_3.5.1
My primary machine is a Mac, but I do have access to Linux. On Linux, it works as expected for me as well - no need to change download.file() method.
One file works great - it appears to not use parallelization as the input is just 1 item, similar to setting mc.cores = 1
Two or more files fails regardless of wether the input is a valid file at the destination host. Running it via commandline R, I get more details on the errors than when running in RStudio, though the end result is the same:
> library(parallel)
> url_base <- "https://cloud.r-project.org/src/contrib/"
> files <- c("A3_1.0.0.tar.gz", "ABC.RAP_0.9.0.tar.gz", "non_existant.tar.gz")
> res <- mclapply(files[1:2], function(s) { try({ download.file(paste0(url_base, s), s); s }) })
trying URL 'https://cloud.r-project.org/src/contrib/A3_1.0.0.tar.gz'
trying URL 'https://cloud.r-project.org/src/contrib/ABC.RAP_0.9.0.tar.gz'
*** caught segfault ***
address 0x1048a6a3a, cause 'memory not mapped'
*** caught segfault ***
address 0x1048a6a3a, cause 'memory not mapped'
Traceback:
1: download.file(paste0(url_base, s), s)
2: doTryCatch(return(expr), name, parentenv, handler)
3: tryCatchOne(expr, names, parentenv, handlers[[1L]])
4: tryCatchList(expr, classes, parentenv, handlers)
5: tryCatch(expr, error = function(e) { call <- conditionCall(e) if (!is.null(call)) { if (identical(call[[1L]], quote(doTryCatch))) call <- sys.call(-4L) dcall <- deparse(call)[1L] prefix <- paste("Error in", dcall, ": ") LONG <- 75L sm <- strsplit(conditionMessage(e), "\n")[[1L]] w <- 14L + nchar(dcall, type = "w") + nchar(sm[1L], type = "w") if (is.na(w)) w <- 14L + nchar(dcall, type = "b") + nchar(sm[1L], type = "b") if (w > LONG) prefix <- paste0(prefix, "\n ") } else prefix <- "Error : " msg <- paste0(prefix, conditionMessage(e), "\n") .Internal(seterrmessage(msg[1L])) if (!silent && isTRUE(getOption("show.error.messages"))) { cat(msg, file = outFile) .Internal(printDeferredWarnings()) } invisible(structure(msg, class = "try-error", condition = e))})
6: try({ download.file(paste0(url_base, s), s) s})
7:
FUN(X[[i]], ...)Traceback:
1: 8: download.file(paste0(url_base, s), s)lapply(X = S, FUN = FUN, ...)
2: 9: doTryCatch(return(expr), name, parentenv, handler)doTryCatch(return(expr), name, parentenv, handler)
3: 10: tryCatchOne(expr, names, parentenv, handlers[[1L]])tryCatchOne(expr, names, parentenv, handlers[[1L]])
4: 11: tryCatchList(expr, classes, parentenv, handlers)
12: tryCatch(expr, error = function(e) { call <- conditionCall(e)tryCatchList(expr, classes, parentenv, handlers) if (!is.null(call)) {
if (identical(call[[1L]], quote(doTryCatch))) call <- sys.call(-4L) 5: dcall <- deparse(call)[1L]tryCatch(expr, error = function(e) { prefix <- paste("Error in", dcall, ": ") call <- conditionCall(e) LONG <- 75L if (!is.null(call)) { sm <- strsplit(conditionMessage(e), "\n")[[1L]] if (identical(call[[1L]], quote(doTryCatch))) call <- sys.call(-4L) dcall <- deparse(call)[1L] prefix <- paste("Error in", dcall, ": ") LONG <- 75L w <- 14L + nchar(dcall, type = "w") + nchar(sm[1L], type = "w") sm <- strsplit(conditionMessage(e), "\n")[[1L]] w <- 14L + nchar(dcall, type = "w") + nchar(sm[1L], type = "w") if (is.na(w)) if (is.na(w)) w <- 14L + nchar(dcall, type = "b") + nchar(sm[1L], w <- 14L + nchar(dcall, type = "b") + nchar(sm[1L], type = "b") type = "b") if (w > LONG) if (w > LONG) prefix <- paste0(prefix, "\n ") prefix <- paste0(prefix, "\n ") } } else prefix <- "Error : " else prefix <- "Error : " msg <- paste0(prefix, conditionMessage(e), "\n") msg <- paste0(prefix, conditionMessage(e), "\n") .Internal(seterrmessage(msg[1L])) .Internal(seterrmessage(msg[1L])) if (!silent && isTRUE(getOption("show.error.messages"))) { if (!silent && isTRUE(getOption("show.error.messages"))) { cat(msg, file = outFile) cat(msg, file = outFile) .Internal(printDeferredWarnings()) } .Internal(printDeferredWarnings()) invisible(structure(msg, class = "try-error", condition = e)) }}) invisible(structure(msg, class = "try-error", condition = e))
})
6: try({ download.file(paste0(url_base, s), s) s})
13: 7: try(lapply(X = S, FUN = FUN, ...), silent = TRUE)FUN(X[[i]], ...)
14: 8: sendMaster(try(lapply(X = S, FUN = FUN, ...), silent = TRUE))lapply(X = S, FUN = FUN, ...)
15: 9: FUN(X[[i]], ...)doTryCatch(return(expr), name, parentenv, handler)
16: 10: lapply(seq_len(cores), inner.do)tryCatchOne(expr, names, parentenv, handlers[[1L]])
17: 11: mclapply(files[1:2], function(s) {tryCatchList(expr, classes, parentenv, handlers) try({
download.file(paste0(url_base, s), s) s12: })tryCatch(expr, error = function(e) {}) call <- conditionCall(e)
if (!is.null(call)) { if (identical(call[[1L]], quote(doTryCatch)))
call <- sys.call(-4L)Possible actions:
1: abort (with core dump, if enabled)
dcall <- deparse(call)[1L]2: normal R exit
prefix <- paste("Error in", dcall, ": ")3: exit R without saving workspace
LONG <- 75L4: exit R saving workspace
sm <- strsplit(conditionMessage(e), "\n")[[1L]] w <- 14L + nchar(dcall, type = "w") + nchar(sm[1L], type = "w") if (is.na(w)) w <- 14L + nchar(dcall, type = "b") + nchar(sm[1L], type = "b") if (w > LONG) prefix <- paste0(prefix, "\n ") } else prefix <- "Error : " msg <- paste0(prefix, conditionMessage(e), "\n") .Internal(seterrmessage(msg[1L])) if (!silent && isTRUE(getOption("show.error.messages"))) { cat(msg, file = outFile) .Internal(printDeferredWarnings()) } invisible(structure(msg, class = "try-error", condition = e))})
13: try(lapply(X = S, FUN = FUN, ...), silent = TRUE)
14: sendMaster(try(lapply(X = S, FUN = FUN, ...), silent = TRUE))
15: FUN(X[[i]], ...)
16: lapply(seq_len(cores), inner.do)
17: mclapply(files[1:2], function(s) { try({ download.file(paste0(url_base, s), s)Selection: s })})
Comment: We don't need the try() statement, we don't need the non-existing URL, and we don't need the return value to be s.
Since this is related to the core R distribution, could you please post this (after verifying that this smaller example also segfaults) together with your sessionInfo() and your updated traceback() to the R-devel mailing list? I think it's also useful for them to know how you installed R on your macOS, e.g. which of the macOS binary distributions did you install?
Sorry to butt in here, but @magic-lantern's sessionInfo() return -- particularly the fact that you are using OpenBLAS -- touches on something that I've been wondering myself: Does future.apply work as expected when you are using optimized BLAS libraries like OpenBLAS or MKL that automatically invoke multithreading?
My understanding is you should set the BLAS threads to 1 (using, say RhpcBLASctl::blas_set_num_threads(1)) if you are going to use any multicore process from within R. Otherwise, you risk competing for computational resources with yourself... or even crashing your system. Is that correct or something that future.apply users should be aware of?
In my case, the problem was that the default method for download.file() is libcurl on macOS 10.14 and libcurl did not support parallelization. However, if I provide a non-default download method to the download.file() call such as:
res <- parallel::mclapply(files, function(s) download.file(paste0(url_base,
s), s, method="wget"))
res <- parallel::mclapply(files, function(s) download.file(paste0(url_base,
s), s, method="curl"))
It works correctly - no segfault.
If the operation you are attempting to parallelize calls some external library or process, you may get a similar error. Additionally, I'm not on macOS Catalina, so your issue may be different than mine.
Thanks for the clarification and R-devel pointers. It might be worth protecting against this automagically in the future framework by disabling multi-threading when multicore is used - I've created futureverse/future#355 to keep track of this.
Activity
HenrikBengtsson commentedon Sep 18, 2018
Swamped, but some very quick feedback: Maybe
download.file()for some choices ofmethoddoes not work in forked processes and/or on macOS. ThatUnexpected result (of class ‘NULL’ != ‘FutureResult’)error message suggests that the forked worker has actually terminated (e.g. core dumped). If you install:and retry you should get a little bit more information around that error. Please retry and see what you get. (I won't fix your problem but it will help narrowing it down).
magic-lantern commentedon Sep 19, 2018
Thanks for the suggestion.
That version does give a more meaningful error
Full error output attached.
It appears that the error is indeed with
download.file()- I realized that it actually can use different methods and that the default one - libcurl - seems to be broken withplan(multicore)on macOS.output.txt
If I'm misreading the traceback, please let me know. Otherwise, I can open a ticket on
download.file()which it appears you also maintain.HenrikBengtsson commentedon Sep 19, 2018
Thanks for reporting back and confirm that the new post-mortem diagnosis works. So, I assume that the following cleaned up code also crashes for you:
If so, then I'd assume that the following non-future code will also crash, or more specifically return
NULLrather than an object of classtry-error:Is that correct?
PS. The reason why I tweaked your example is that if you use
tryCatch(..., error = function(ex) ... NULL)to return NULL on failure you will not be able to tell the difference from a crashed worker when usingparallel::mclapply()(which returnsNULLfor crashed workers).magic-lantern commentedon Sep 19, 2018
The simpler code example you provided using future.apply does fail - same error as before:
The res object does not get created.
The second example also fails; however in this second case res is:
8 Warnings are returned - pretty much all identical except for what I assume is the PID - first 2 shown below:
If I tweak the mclapply example to only use 1 core (
mc.cores = 1) it does work correctly; 2 or higher results in the error.So, does this mean my machine is hosed, or is this a weird edge case bug (feature?) with
download.file()?HenrikBengtsson commentedon Sep 19, 2018
For the sake of troubleshooting this one, I want to get to the point where it also crash/fail when using only the parallel package, because I believe (99.999%) that the problem has to do with forking and
download.file(..., method = "libcurl")- and that it has nothing to do with the future framework. Theplan(multicore)is basically utilizing the same code asparallel::mclapply()does internally.Before anything else, what is your
sessionInfo()?It's weird that your run using
parallel::mclapply()failed to download anything. So, does it work do download only the first file or the first two files? That is, try with:and then
If that works, what about?
FYI, setting
mc.cores = 1is effectively the same as callinglapply()instead ofmclapply(), so then there is no forking or parallelization involved. In other words, that doesn't help the troubleshooting/narrowing down of the problem.FYI2, I don't have access to macOS. I'm on Linux, where forking is also supported, but I cannot reproduce your problem. All code snippets work as expected.
magic-lantern commentedon Sep 19, 2018
Here's my
sessionInfo()My primary machine is a Mac, but I do have access to Linux. On Linux, it works as expected for me as well - no need to change
download.file()method.One file works great - it appears to not use parallelization as the input is just 1 item, similar to setting
mc.cores = 1Two or more files fails regardless of wether the input is a valid file at the destination host. Running it via commandline R, I get more details on the errors than when running in RStudio, though the end result is the same:
HenrikBengtsson commentedon Sep 19, 2018
Thanks. So we've ruled out the future package. An even smaller example to be shared with others appears to be (please validate):
Comment: We don't need the
try()statement, we don't need the non-existing URL, and we don't need the return value to bes.Since this is related to the core R distribution, could you please post this (after verifying that this smaller example also segfaults) together with your
sessionInfo()and your updatedtraceback()to the R-devel mailing list? I think it's also useful for them to know how you installed R on your macOS, e.g. which of the macOS binary distributions did you install?magic-lantern commentedon Sep 19, 2018
Thanks for the simplification of the example code. I still get the same result.
I'll post to R-devel mailing list to see what guidance can be provided.
Since this certainly isn't a problem with future.apply, I'll close this issue.
HenrikBengtsson commentedon Sep 19, 2018
thxs.
grantmcdermott commentedon Dec 18, 2018
Sorry to butt in here, but @magic-lantern's
sessionInfo()return -- particularly the fact that you are using OpenBLAS -- touches on something that I've been wondering myself: Does future.apply work as expected when you are using optimized BLAS libraries like OpenBLAS or MKL that automatically invoke multithreading?My understanding is you should set the BLAS threads to 1 (using, say
RhpcBLASctl::blas_set_num_threads(1)) if you are going to use any multicore process from within R. Otherwise, you risk competing for computational resources with yourself... or even crashing your system. Is that correct or something that future.apply users should be aware of?HenrikEckermann commentedon Dec 30, 2019
@magic-lantern
Did you figure a solution for this problem? I encounter the same on MacOS Catalina right now...
magic-lantern commentedon Dec 31, 2019
@HenrikBengtsson - Since my issue wasn't actually with future.apply, I closed this issue and posted to the R-Devel mailing list at: https://stat.ethz.ch/pipermail/r-devel/2018-September/076845.html
In my case, the problem was that the default method for
download.file()is libcurl on macOS 10.14 and libcurl did not support parallelization. However, if I provide a non-default download method to thedownload.file()call such as:It works correctly - no segfault.
If the operation you are attempting to parallelize calls some external library or process, you may get a similar error. Additionally, I'm not on macOS Catalina, so your issue may be different than mine.
HenrikBengtsson commentedon Dec 31, 2019
Thanks for the clarification and R-devel pointers. It might be worth protecting against this automagically in the future framework by disabling multi-threading when multicore is used - I've created futureverse/future#355 to keep track of this.