Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R process crashes if interrupt happens during later's call to sys.nframe #57

Closed
wch opened this issue Jun 2, 2018 · 3 comments · Fixed by #58
Closed

R process crashes if interrupt happens during later's call to sys.nframe #57

wch opened this issue Jun 2, 2018 · 3 comments · Fixed by #58

Comments

@wch
Copy link
Member

wch commented Jun 2, 2018

I believe this is the root cause of rstudio/shiny#2081.

When an interrupt occurs during later's internal call to sys.nframe(), it throws an exception that is not caught. The call is here:

later/src/later.cpp

Lines 39 to 40 in 207d4e0

PROTECT(e = Rf_lang1(Rf_install("sys.nframe")));
PROTECT(result = R_tryEval(e, R_BaseEnv, &errorOccurred));

It is called (indirectly) from here

if (!at_top_level()) {

and from here

if (!at_top_level()) {

I had a hard time reproducing this error by calling later() and pressing Esc or Ctrl-C. However, this code crashes with a similar stack trace -- it replaces sys.nframe with a version which calls stop() when it detects its being run at the top level.

# Make sure we throw only once -- in Windows it can happen multiple times if we're not careful
has_thrown <- FALSE
sys.nframe2 <- function() {
  if (!has_thrown && length(sys.calls()) == 1) {
    has_thrown <<- TRUE
    stop("sys.nframe called from later's callback execution code. Throwing error.")
  }
  .Internal(sys.nframe())
}
assignInNamespace("sys.nframe", sys.nframe2, "base")

later::later(function() cat("hello"))

This causes R to crash on both Windows and Mac.

If R is run from cmd.exe in Windows, it prints this:

Error in sys.nframe() :
  sys.nframe called from later's callback execution code. Throwing error.
terminate called after throwing an instance of 'Rcpp::exception'
  what():  Error occurred while calling sys.nframe()

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

If run in RGui, it puts up a dialog with the "terminate it in an unusual way" text. If run in RStudio, the IDE crashes.

On a Mac, it prints this:

Error in sys.nframe() : 
  sys.nframe called from later's callback mechanism. Throwing error.
libc++abi.dylib: terminating with uncaught exception of type Rcpp::exception: Error occurred while calling sys.nframe()
Abort trap: 6

To trigger the error under gdb, and catch it so that the state of the program can be inspected, first install later with debugging information:

install.packages("later", type="source", INSTALL_opts = "--debug")

Then run the following in cmd.exe:

c:\Rtools\mingw_64\bin\gdb c:\Progra~1\R\R-3.5.0\bin\x64\Rgui.exe
break Rcpp::exception::exception
y
run

In the RGui window, run the R code from above. This will cause an exception.

Here's a log of what I did in the cmd.exe window:

Breakpoint 1, Rcpp::exception::exception (this=0x1ab95480,
    message_=0x18e699f0 <CallEntries+304> "Error occurred while calling sys.nframe()", include_call=true)
    at C:/Users/winst/R/3.5/Rcpp/include/Rcpp/exceptions.h:36
36                  include_call_(include_call){
(gdb) bt
#0  Rcpp::exception::exception (this=0x1ab95480,
    message_=0x18e699f0 <CallEntries+304> "Error occurred while calling sys.nframe()", include_call=true)
    at C:/Users/winst/R/3.5/Rcpp/include/Rcpp/exceptions.h:36
#1  0x0000000018dd2df8 in at_top_level () at later.cpp:60
#2  0x0000000018dd352c in executeHandlers () at later_win32.cpp:36
#3  0x0000000018dd36cc in callbackWndProc (hWnd=0xf0684, message=275, wParam=1, lParam=0) at later_win32.cpp:73
#4  0x00007ffe7a8cb85d in USER32!CallWindowProcW () from C:\Windows\System32\user32.dll
#5  0x00007ffe7a8cb1ef in USER32!DispatchMessageW () from C:\Windows\System32\user32.dll
#6  0x000000006355751b in GA_doevent () from c:\Progra~1\R\R-3.5.0\bin\x64\Rgraphapp.dll
#7  0x000000006c720b95 in R_ProcessEvents () from c:\Progra~1\R\R-3.5.0\bin\x64\R.dll
#8  0x000000006c706b7d in freeConsoleData () from c:\Progra~1\R\R-3.5.0\bin\x64\R.dll
#9  0x000000006c72098f in R_setStartTime () from c:\Progra~1\R\R-3.5.0\bin\x64\R.dll
#10 0x000000006c82e732 in Rf_ReplIteration () from c:\Progra~1\R\R-3.5.0\bin\x64\R.dll
#11 0x000000006c82ea51 in Rf_ReplIteration () from c:\Progra~1\R\R-3.5.0\bin\x64\R.dll
#12 0x000000006c82eae2 in run_Rmainloop () from c:\Progra~1\R\R-3.5.0\bin\x64\R.dll
#13 0x00000000004016b7 in ?? ()
#14 0x000000000040155a in ?? ()
#15 0x00000000004013e8 in ?? ()
#16 0x00000000004014eb in ?? ()
#17 0x00007ffe7ae01fe4 in KERNEL32!BaseThreadInitThunk () from C:\Windows\System32\kernel32.dll
#18 0x00007ffe7c44efb1 in ntdll!RtlUserThreadStart () from C:\Windows\SYSTEM32\ntdll.dll
#19 0x0000000000000000 in ?? ()
Backtrace stopped: previous frame inner to this frame (corrupt stack?)
(gdb) fr 1
#1  0x0000000018dd2df8 in at_top_level () at later.cpp:60
60      later.cpp: No such file or directory.
(gdb) p nframe
$1 = -1
(gdb) c
Continuing.
[Thread 11144.0x2d10 exited with code 0]
terminate called after throwing an instance of 'Rcpp::exception'
  what():  Error occurred while calling sys.nframe()

At this point, RGui displays the following dialog:

image

@wch
Copy link
Member Author

wch commented Jun 2, 2018

A way to trigger the error with a user interrupt (instead of stop()) is to modify sys.nframe() to wait for a few seconds, and then press Esc/Ctrl-C while it's waiting.

sys.nframe2 <- function() {
  if (length(sys.calls()) == 1) {
    cat("pausing for 5 seconds. Press Esc or Ctrl-C now...\n", file = stderr())
    Sys.sleep(5)
    cat("done.\n", file = stderr())
  }
  .Internal(sys.nframe())
}
assignInNamespace("sys.nframe", sys.nframe2, "base")

later::later(function() cat("hello"))

The behavior is essentially the same as when stop() was called previously.

@sanjmeh
Copy link

sanjmeh commented Feb 4, 2019

Great to see this fixed. Is this fix already on the latest version (shiny 1.2) that is on cran?

@wch
Copy link
Member Author

wch commented Feb 4, 2019

@sanjmeh This issue was fixed in the later package in version 0.7.3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants