later icon indicating copy to clipboard operation
later copied to clipboard

Interruption of callbacks can result in `c++ exception (unknown reason)` message

Open jcheng5 opened this issue 6 years ago • 8 comments

Downstream issues: https://github.com/rstudio/shiny/issues/1967#issuecomment-385815733 https://github.com/rstudio/httpuv/issues/130


This happens only under certain circumstances, when a native/Rcpp callback function is registered with the native later::later API, and an interrupt occurs within the callback.

Rcpp::cppFunction(depends = "later",
  code = '
  void test() {
    later::later(doIt, 0, 0);
  }
  ',
  includes = '
  #include <later_api.h>

  void doIt(void* data) {
    throw new Rcpp::internal::InterruptedException();
  }
  ')

Run test() to reproduce; you should see later: c++ exception (unknown reason) occurred while executing callback.

In the example above, we are calling throw Rcpp::internal::InterruptedException() explicitly, but it can happen through user gesture as well:

Rcpp::cppFunction(depends = "later",
  code = '
  void test2() {
    later::later(doIt, 0, 0);
  }
  ',
  includes = '
  #include <later_api.h>

  void doIt(void* data) {
    Rcpp::Function func("Sys.sleep");
    func(5);
  }
  ')

Run test2(); later::run_now(5), and quickly hit Esc or Ctrl+C to interrupt. You should see Error in execCallbacks(timeoutSecs) : c++ exception (unknown reason).

The root cause seems to have to do with the compilation unit of the throw Rcpp::internal::InterruptedException versus that of the catch (Rcpp::internal::InterruptedException) (usually the latter is via the END_RCPP macro). A minimal repro for this problem is at https://github.com/jcheng5/exceptiontest.

jcheng5 avatar May 02 '18 21:05 jcheng5

Hmmm. exceptiontest doesn't repro on Ubuntu using gcc. But the first example in this issue does repro. But not the second example.

jcheng5 avatar May 02 '18 22:05 jcheng5

Is it possible that the difference you're seeing between Linux and Mac is because on Linux, all packages are compiled from source on the test machine, but on Mac, some packages (like Rcpp) were built on a different machine with possibly a different compiler and compiler settings?

wch avatar May 03 '18 14:05 wch

Possibly. Could also be clang vs g++ in general?

jcheng5 avatar May 03 '18 15:05 jcheng5

We am also having issues running our plumber service in docker. We keep having containers implode on us with this stack trace

*** caught segfault ***
address (nil), cause 'unknown'

1: .Call("_later_execCallbacks", PACKAGE = "later", timeoutSecs)
2: execCallbacks(timeoutSecs)
3: run_now(check_time)
4: service(0)
5: httpuv::runServer(host, port, self)
6: pr$run(host = "0.0.0.0", port = 8000, swagger = TRUE)

An irrecoverable exception occurred. R is aborting now ...

Is this the same issue? Sorry I am kind of at a loss on this one

JackStat avatar May 31 '18 15:05 JackStat

@JackStat Are you able to provide us with a reproducible example -- say, a Dockerfile that builds an image that will raise that same error?

Also, the segfault you're seeing is probably a separate problem. Can you file a new issue for this?

wch avatar May 31 '18 15:05 wch

It's so buried in our proprietary code I don't know if I can but I will try. We installed httpuv 1.3.5 and the issue went away. I will continue to dig and hopefully put this in the appropriate place

JackStat avatar May 31 '18 17:05 JackStat

Thanks, that would be very helpful. I think that it would make sense to file an issue in the httpuv repo.

wch avatar May 31 '18 17:05 wch

This may be completely unrelated in which case I am sorry, but I have seen some issues with re-throwing exceptions but only on clang-11 using libc++. It is not with your library but the error is due to an exception, it can yield c++ exception (unknown reason) and a segfault like in JackStat's example. The example is provided here.

boennecd avatar Feb 25 '21 06:02 boennecd