69

Is there a simple way to trigger a crash in R? This is for testing purposes only, to see how a certain program that uses R in the background reacts to a crash and help determine if some rare problems are due to crashes or not.

6
  • 3
    I've tried options(expressions=300000) then running an infinite recursion but R is written well enough that it doesn't crash :)
    – Szabolcs
    Aug 5, 2014 at 12:50
  • 10
    bugs.r-project.org/bugzilla/…
    – James
    Aug 5, 2014 at 13:02
  • 1
    This may depend on your platform. Want to add that info? Aug 5, 2014 at 13:02
  • 4
    Can downvoters explain what they consider to be wrong with the question? @DirkEddelbuettel Please be tolerant, that does not make the solution obvious to everyone. A link to the man page doesn't make it clear how to do this.
    – Szabolcs
    Aug 5, 2014 at 15:27
  • 4
    Must it be a crash? Could you simply quit with a non-zero status instead? Aug 5, 2014 at 17:08

6 Answers 6

53

The easiest way is to call C-code. C provides a standard function abort()[1] that does what you want. You need to call: .Call("abort").

As @Phillip pointed out you may need to load libc via:

  • on Linux, dyn.load("/lib/x86_64-linux-gnu/libc.so.6") before issuing .Call("abort"). The path may of course vary depending on your system.

  • on OS X, dyn.load("/usr/lib/libc.dylib")

  • on Windows (I just tested it on XP as I could not get hold of a newer version.) you will need to install Rtools[2]. After that you should load dyn.load("C:/.../Rtools/bin/cygwin1.dll").

9
  • 1
    When running R from the command line or using the official GUI, I get Error in .Call("abort") : C symbol name "abort" not in load table. When using RStudio it crashes.
    – Szabolcs
    Aug 5, 2014 at 13:12
  • 2
    You've got to load libc before: dyn.load("/lib/x86_64-linux-gnu/libc.so.6"). The path may vary on your system, use locate libc.so.6 to find it.
    – Phillip
    Aug 5, 2014 at 13:14
  • 3
    The crash package calls abort as well. At least as far as I can see but I'm not nearly as experienced as you are! Aug 5, 2014 at 13:33
  • 1
    So it'll never get on CRAN :) Aug 5, 2014 at 13:34
  • 3
    You probably meant the Rtools package as R was never supported on Cygwin. What Joshua was too polite to mention directly is that your answer isn't fully portable this way. But yes, abort() is the key. Aug 6, 2014 at 1:09
49

There is an entire package on GitHub dedicated to this:

crash

R package that purposely crash an R session. WARNING: intended for test.

How to install a package from github is covered in other questions.

2
  • 21
    And it calls abort, nice. Aug 5, 2014 at 13:33
  • 8
    Assuming you have the required tools installed (*nix, Windows), one way to install this package from github is: library(devtools); install_github('jdanielnd/crash'). Then you can crash your R session with library(crash); crash() Aug 6, 2014 at 11:24
17

I'm going to steal an idea from @Spacedman, but I'm giving him full conceptual credit by copying from his Twitter feed:

Segfault #rstats in one easy step: options(device=function(){});plot(1) reported Danger, will crash your R session. — Barry Rowlingson (@geospacedman) July 16, 2014

1
  • 1
    This is useful because it doesn't exit right away, instead it presents a prompt and asks what to do next. It's a different sort of behaviour which may also be what's going wrong in my own project ...
    – Szabolcs
    Aug 5, 2014 at 13:45
15

As mentioned in a comment to your question, the minimal approach is a simple call to the system function abort(). One way to do this in one line is to

R> Rcpp::cppFunction('int crashMe(int ignored) { ::abort(); }'); 
R> crashMe(123)
Aborted (core dumped)
$ 

or you can use the inline package:

R> library(inline)
R> crashMe <- cfunction(body="::abort();")
R> crashMe()
Aborted (core dumped)
$ 

You can of course also do this outside of Rcpp or inline, but then you need to deal with the system-dependent ways of compiling, linking and loading.

2
  • The above snippet crashes to desktop (in a low-mem env) after the first line. Here be dragons. Aug 6, 2014 at 9:24
  • 1
    @DeerHunter. I also noticed that on maybe one out five attempts. There must be a race somewhere. Then again, R isn't exactly designed to be abort()ed. Aug 6, 2014 at 13:54
6

I'll do this in plain C because my C++-foo isn't Dirkian:

Create a C file, segv.c:

#include <signal.h>
void crashme(){raise(SIGSEGV);}

Compile it at the command line (windows users will have to work this out for themselves):

R CMD SHLIB segv.c

In R, load and run:

dyn.load("segv.so") # or possibly .dll for Windows users
.C("crashme")

Producing a segfault:

> .C("crashme")

 *** caught segfault ***
address 0x1d9e, cause 'unknown'

Traceback:
 1: .C("crashme")

Possible actions:
1: abort (with core dump, if enabled)
2: normal R exit
3: exit R without saving workspace
4: exit R saving workspace
Selection: 1
aborting ...
Segmentation fault

This is the same behaviour as the one Thomas references in the graphics system bug report which I have filed and might get fixed one day. However this two-liner will always raise a segfault...

Maybe Dirk can one-line-Rcpp-ise it?

3
  • Re-read my post, the inline use is entirely C -- I use cfunction() i in C mode. You could do the same here to make your answer easier/more concise/less OS-dependent. The Rcpp use is 'merely' to deploy the even easier build mechanism, there is no C++ there per se. Aug 5, 2014 at 17:14
  • 3
    spacedman <- inline::cfunction(body="raise(SIGSEGV);", include="#include <signal.h>") -- and no C++ was harmed^Hused in this answer. Aug 5, 2014 at 17:16
  • spacedman <- Rcpp::cppFunction("void crashme() { ::raise(SIGSEGV); }", includes="#include <signal.h>") -- so there. Aug 5, 2014 at 17:29
0

If you want to crash your R, try this

lapply("", function(x) eval(sys.call(1)))

(Save everything before running because this immediately results in "R Session Aborted")

Edit: This works for me on Windows 10.

1
  • At least on macOS there seems to be some protection in place against stack overflow. This results in "Error: C stack usage 7971744 is too close to the limit", but no crash.
    – Szabolcs
    Mar 2, 2020 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.