Skip to content

r_bg exit status always 0 #291

@r2evans

Description

@r2evans

callr::r_bg always returns 0 even on error, despite R's default behavior (from ?q: The default error handler for non-interactive use effectively calls q("no", 1, FALSE) and returns error status 1.)

$ echo 'quux' | R --vanilla --quiet
> quux
Error: object 'quux' not found
Execution halted
$ echo $?
1

With callr though, we always get 0:

proc <- callr::r_bg(function() quux)
proc$read_all_output()
# [1] "WARNING: ignoring environment value of R_HOME\n"
proc$read_all_error()
# [1] "Error in (function ()  : object 'quux' not found\n"
proc$get_exit_status()
# [1] 0

I recognize I can use rscript_process to get the desired effect:

writeLines("quux", "quux.R")
proc <- callr::rscript_process$new(callr::rscript_process_options(script="quux.R"))
proc$read_all_output()
# [1] "WARNING: ignoring environment value of R_HOME\n"
proc$read_all_error()
# [1] "Error: object 'quux' not found\nExecution halted\n"
proc$get_exit_status()
# [1] 1

I suggest would be more consistent if the error= and interrupt= handlers in the wrapper used by callr::r_bg end with q(save="no", status=1).

modified   R/script.R
@@ -36,6 +36,7 @@ make_vanilla_script_expr <- function(expr_file, res, error,
       }
 
       base::saveRDS(base::list("error", e2, e), file = base::paste0(`__res__`, ".error"))
+      q(save = "no", status = 1)
     }, list(
          `__res__` = res,
          `__traceback__` = getOption("callr.traceback", FALSE)

With that, I see the desired behavior:

devtools::load_all()
proc <- callr::r_bg(function() quux)
proc$read_all_output()
# [1] "WARNING: ignoring environment value of R_HOME\n"
proc$read_all_error()
# [1] ""
proc$get_exit_status()
# [1] 1

Are there other aspects that would need to be addressed as well? (That is, what else could be changed for this to be a more complete solution?) Or is there rationale for having r_bg always return status 0?

I'm running R-4.3.3 and callr_3.7.6 within emacs/ess on ubuntu-24.04.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions