Skip to content

Problem with multicore on macOS #29

Closed

Description

@magic-lantern

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 found
files <- 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, ...); }); }’): 

Expected output is:

[[1]]
[1] "A3_1.0.0.tar.gz"
[[2]]
[1] "ABC.RAP_0.9.0.tar.gz"
[[3]]
NULL

If I use plan(sequential) or plan(multisession) it works just fine.

It seems that for some reason, the download.file() function call doesn't work, but I can't figure how how to debug this issue further.

Is this a known macOS issue? Or a known download.file() issue?

Activity

  1. HenrikBengtsson commented on Sep 18, 2018

    @HenrikBengtsson
    Collaborator

    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:

    remotes::install_github('HenrikBengtsson/future@develop')

    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).

  2. magic-lantern commented on Sep 19, 2018

    @magic-lantern
    Author

    Thanks for the suggestion.

    That version does give a more meaningful error

    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.

    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.

  3. HenrikBengtsson commented on Sep 19, 2018

    @HenrikBengtsson
    Collaborator

    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:

    library(future.apply)
    plan(multicore)
    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 <- future_lapply(files, function(s) {
      try({ download.file(paste0(url_base, s), s); s })
    })

    If so, then I'd assume that the following non-future code will also crash, or more specifically return NULL rather than an object of class try-error:

    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, function(s) {
      try({ download.file(paste0(url_base, s), s); s })
    })

    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 using parallel::mclapply() (which returns NULL for crashed workers).

  4. magic-lantern commented on Sep 19, 2018

    @magic-lantern
    Author

    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()?

  5. HenrikBengtsson commented on Sep 19, 2018

    @HenrikBengtsson
    Collaborator

    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:

    res <- mclapply(files[1], function(s) { try({ download.file(paste0(url_base, s), s); s }) })

    and then

    res <- mclapply(files[1:2], function(s) { try({ download.file(paste0(url_base, s), s); s }) })

    If that works, what about?

    res <- mclapply(files[2:3], function(s) { try({ download.file(paste0(url_base, s), s); s }) })

    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.

  6. magic-lantern commented on Sep 19, 2018

    @magic-lantern
    Author

    Here's my sessionInfo()

    > 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    })})
    
  7. HenrikBengtsson commented on Sep 19, 2018

    @HenrikBengtsson
    Collaborator

    Thanks. So we've ruled out the future package. An even smaller example to be shared with others appears to be (please validate):

    url_base <- "https://cloud.r-project.org/src/contrib/"
    files <- c("A3_1.0.0.tar.gz", "ABC.RAP_0.9.0.tar.gz")
    res <- parallel::mclapply(files, function(s) download.file(paste0(url_base, s), 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?

  8. magic-lantern commented on Sep 19, 2018

    @magic-lantern
    Author

    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.

  9. HenrikBengtsson commented on Sep 19, 2018

    @HenrikBengtsson
    Collaborator

    thxs.

  10. grantmcdermott commented on Dec 18, 2018

    @grantmcdermott

    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?

  11. HenrikEckermann commented on Dec 30, 2019

    @HenrikEckermann

    @magic-lantern

    Did you figure a solution for this problem? I encounter the same on MacOS Catalina right now...

  12. magic-lantern commented on Dec 31, 2019

    @magic-lantern
    Author

    @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 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.

  13. HenrikBengtsson commented on Dec 31, 2019

    @HenrikBengtsson
    Collaborator

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions