Repository navigation
Description
Thanks for this awesome package! I'm trying to get promise error handling to work with future_promise(), but it reports each error twice. With onReject only one error is printed to console and the error catching seems to work okay, but it's confusing for the user (and there may be other issues I'm not seeing). Here's a minimal example showing the behaviour on my machine:
library(magrittr)
library(future)
library(promises)
future::plan(future::multisession)
test <- promises::future_promise( stop("An intentional error.") )
Which gives this output showing two identical unhandled errors:
Unhandled promise error: An intentional error.
Unhandled promise error: An intentional error.
Here's an example with error handling following the code this vignette, along with the results on my machine. Note that it still throws one error to the console:
test_handled <- future_promise( stop("An intentional error.") ) %>%
then(onRejected = function(err) {
warning("An error occurred: ", err)
warning("Using default value of 0 instead")
0
}
)
Which gives this output still showing one unhandled error:
Unhandled promise error: An intentional error.
And if I print test_handled to the console, I get what I think is the expected result of the warning output and the fulfilled promise:
test_handled
Output:
<Promise [fulfilled: numeric]>
Warning messages:
1: In onRejected(reason) :
An error occurred: Error in withCallingHandlers({: An intentional error.
2: In onRejected(reason) : Using default value of 0 instead
Am I making an obvious user mistake, or missing some setup? Naively, I'd think the expected behaviour is to not report a handled error, or at least not to report it as an unhandled error.
Session Info:
> sessionInfo()
R version 4.1.2 (2021-11-01)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.3 LTS
Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0
locale:
[1] LC_CTYPE=C.UTF-8 LC_NUMERIC=C LC_TIME=C.UTF-8 LC_COLLATE=C.UTF-8 LC_MONETARY=C.UTF-8 LC_MESSAGES=C.UTF-8
[7] LC_PAPER=C.UTF-8 LC_NAME=C LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] promises_1.2.0.1 future_1.28.0 magrittr_2.0.3
loaded via a namespace (and not attached):
[1] Rcpp_1.0.9 codetools_0.2-18 listenv_0.8.0 digest_0.6.29 later_1.3.0 parallelly_1.32.1 R6_2.5.1
[8] rlang_1.0.5 cli_3.4.0 logdriver_0.1.0 rstudioapi_0.14 tools_4.1.2 parallel_4.1.2 fastmap_1.1.0
[15] compiler_4.1.2 globals_0.16.1
Activity
jcheng5 commentedon Sep 16, 2022
Hmmm, weird. And it gets weirder:
I wouldn't expect any of these to report an unhandled promise error, but p3, p4, and p5 do, while p6 does twice.
Update 2023-02-07: p3 reports because the
thenmethod does not convert formulas to functions, but instead, silently ignores anything that's not a function. (I copied that from JavaScript but probably better to warn or error if not a function or NULL.) So it's onlyfuture_promisethat has the problem.jcheng5 commentedon Sep 16, 2022
That is sort of expected, consider using
message()instead.warning()doesn't work very well from within promise handlers; by default, R wants to batch up all thewarning()calls and report them all after the current console command is done executing. The problem is, promise handlers run during the random in-between times, when the console isn't executing a command, sowarnings()get batched up but not reported until the next console command is done executing. (See thewarnoption under?options)chris31415926535 commentedon Sep 17, 2022
That is weird! And duly noted about using
message()instead.Importantly,
future_promise()does seem to handle the error withonRejected, even if it prints a message to the contrary. This very silly Shiny example has three buttons: one for an unhandled synchronous error, one for an unhandledfuture_promise()error, and one for a handledfuture_promise()error. The first two buttons halt the app, but the third doesn't: the app keeps working, prints the expected message to the console, and then also gives the output about an unhandled promise error.chris31415926535 commentedon Sep 20, 2022
Update: I've taken a look at the promises code and done some basic print debugging. When I trigger a
future_promise()error the internal functionprivate$doRejectFinalReasonis called three times, although it seems (both from the name and the code) like it should only be called once. In addition, the first two times it's called the internal valueprivate$rejectionHandledstarts with valueFALSE, even if I manually set it toTRUEat the end of the function. This is mysterious because there's no command setting it back toFALSE, which makes me wonder if it's related to part of the function wrapped inthen::then().I'm very new to this codebase, but I wonder if this could be a combination of two things:
private$doRejectcan be called in a few different places, and if it is then it may callprivate$doRejectFinalReasonmore than once; and,then::then()inside ofprivate$doRejectFinalReason, which means that ifprivate$doRejectFinalReasonis being called more than once, the previous iteration hasn't updatedprivate$rejectionHandledbefore the new iteration is called.Just a guess though. Hope this is useful for someone more knowledgeable :)
r2evans commentedon Feb 5, 2023
@chris31415926535 any progress or resolution with this?
r2evans commentedon Feb 5, 2023
@jcheng5 this bug is more than cosmetic, I believe it is causing "worker-leakage" requiring manual intervention.
See https://stackoverflow.com/questions/75354879/why-future-promise-error-is-unhandled-with-onrejected.
Linux, R-4.2.2, future-1.30.0, promises-1.2.0.1, later-1.3.0.
jcheng5 commentedon Feb 5, 2023
@schloerke Maybe one for us to look at together next week?
Fix #86: future_promise spuriously reporting unhandled errors
Fix #86: future_promise spuriously reporting unhandled errors (#90)
r2evans commentedon Apr 25, 2023
This is related to a bug in
futurethat I've been discussing, futureverse/future#676. I have been testing hisdevelopbranch and it still occasionally runs out of workers, but I'm not certain that is due to a bug or just a long long queue in promises that I somehow over-filled. I'm working on that component ...