Error: C stack usage is too close to the limit

error c stack usage 15922992 is too close to the limit
c stack usage 7969428 is too close to the limit
error c stack usage 7971424 is too close to the limit
error c stack usage 7971184 is too close to the limit
c stack usage 7970128 is too close to the limit
error c stack usage 7970228 is too close to the limit
c stack usage 7969408 is too close to the limit
error c stack usage 15924000 is too close to the limit

I'm attempting to run some fairly deep recursive code in R and it keeps giving me this error:

Error: C stack usage is too close to the limit

My output from CStack_info() is:

Cstack_info()
    size    current  direction eval_depth 
67108864       8120          1          2 

I have plenty of memory on my machine, I'm just trying to figure out how I can increase the CStack for R.

EDIT: Someone asked for a reproducible example. Here's some basic sample code that causes the problem. Running f(1,1) a few times you'll get the error. Note that I've already set --max-ppsize = 500000 and options(expressions=500000) so if you don't set those you might get an error about one of those two things instead. As you can see, the recursion can go pretty deep here and I've got no idea how to get it to work consistently. Thanks.

f <- function(root=1,lambda=1) {
    x <- c(0,1);
    prob <- c(1/(lambda+1),lambda/(lambda+1));
        repeat {
      if(root == 0) {
        break;
      }
      else {
        child <- sample(x,2,replace=TRUE,prob);
        if(child[1] == 0 && child[2] == 0) {
          break;
        }
        if(child[1] == 1) {
          child[1] <- f(root=child[1],lambda);
        }
        if(child[2] == 1 && child[1] == 0) {
          child[2] <- f(root=child[2],lambda);
        }
      }
      if(child[1] == 0 && child[2] == 0) {
        break;
      }
      if(child[1] == 1 || child[2] == 1) {
        root <- sample(x,1,replace=TRUE,prob);
      }
        }
    return(root)
}

The stack size is an operating system parameter, adjustable per-process (see setrlimit(2)). You can't adjust it from within R as far as I can tell, but you can adjust it from the shell before starting R, with the ulimit command. It works like this:

$ ulimit -s # print default
8192
$ R --slave -e 'Cstack_info()["size"]'
   size 
8388608

8388608 = 1024 * 8192; R is printing the same value as ulimit -s, but in bytes instead of kilobytes.

$ ulimit -s 16384 # enlarge stack limit to 16 megs
$ R --slave -e 'Cstack_info()["size"]'
    size 
16777216 

To make a permanent adjustment to this setting, add the ulimit command to your shell startup file, so it's executed every time you log in. I can't give more specific directions than that, because it depends on exactly which shell you have and stuff. I also don't know how to do it for logging into a graphical environment (which will be relevant if you're not running R inside a terminal window).

Error: C stack usage 7969952 is too close to the limit. I have tried increasing the C stack usage limit through the Mac terminal, but it still runs the  In my case, I can't run using RCall without the REPL choking and spitting out. Error: C stack usage 140727749152224 is too close to the limit Error: C stack usage 140727749152272 is too close to the limit Error: C stack usage 140727749152176 is too close to the limit Fatal error: unable to initialize the JIT.

I suspect that, regardless of stack limit, you'll end up with recursions that are too deep. For instance, with lambda = Inf, f(1) leads to an immediate recursion, indefinitely. The depth of the recursion seems to be a random walk, with some probability r of going deeper, 1 - r of finishing the current recursion. By the time you've hit the stack limit, you've made a large number of steps 'deeper'. This implies that r > 1 / 2, and the very large majority of time you'll just continue to recurse.

Also, it seems like it is almost possible to derive an analytic or at least numerical solution even in the face of infinite recursion. One can define p as the probability that f(1) == 1, write implicit expressions for the 'child' states after a single iteration, and equate these with p, and solve. p can then be used as the chance of success in a single draw from a binomial distribution.

r-studio server, when I am trying to acces it from shiny-server it crashes with this errors: C stack usage 814747143012 is too close to the limit Error: C stack usage 7969952 is too close to the limit. I have tried increasing the C stack usage limit through the Mac terminal, but it still runs the same error when I have tried it again. I am using the newest versions of R and RStudio. maraApril 17, 2019, 11:36am #2.

This happened to me for a completely different reason. I accidentally created a superlong string while combining two columns:

output_table_subset = mutate(big_data_frame,
     combined_table = paste0(first_part, second_part, col = "_"))

instead of

output_table_subset = mutate(big_data_frame,
     combined_table = paste0(first_part, second_part, sep = "_"))

Took me for ever to figure it out as I never expected the paste to have caused the problem.

C Stack Usage is close to the limit and how to solve it. In this example, we run the pop_pringles function and we will keep eating it until the tube is empty. Running into the C stack error oftentimes means that you're letting a recursive function run indefinitely. Error: C stack usage 112472876 is too close to the limit. This is what I have used to read the file in, and I think it may have something to do with the backend of readr, and using connections to read in text files. I know this csv file was structurally incorrect, so I bet that reading in the file like this does not make it any less easier.

This error is not due to memory it is due to recursion. A function is calling itself. To illustrate the point, here is a minimal example of 2 functions that call each other:

change_to_factor <- function(x){
  x <- change_to_character(x)
  as.factor(x)
} 

change_to_character <- function(x){
  x <- change_to_factor(x)
  as.character(x)
}

change_to_character("1")

Error: C stack usage 7971600 is too close to the limit

The functions will continue to call each other recursively and will theoretically never complete. It is only checks within your system that prevent this from occurring indefinitely and consuming all of the compute resources of your machine. You need to alter the functions to ensure that they don't call itself (or each other) recursively.

I got the following error C stack usage 8024819 is too close to the limit in RStudio​. After I rendered .R script (output file: X.knit.md is produced  My UI file begins with loading data.image (~100mb). While app is working fine on my local pc and via r-studio server, when I am trying to acces it from shiny-server it crashes with this errors: C stack usage 814747143012 is too close to the limit.

I encountered the same problem of receiving the "C stack usage is too close to the limit" error (albeit for another application than the one stated by user2045093 above). I tried zwol's proposal but it didn't work out.

To my own surprise, I could solve the problem by installing the newest version of R for OS X (currently: version 3.2.3) as well as the newest version of R Studio for OS X (currently: 0.99.840), since I am working with R Studio.

Hopefully, this may be of some help to you as well.

Error: C stack usage 1031500945536 is too close to the limit Execution halted. How can I have a 1031 Gig C Stack on an instance with only 4  C stack usage is too close to the limit. error. I played with it and it appears to come after I printed a fixed. number of text to R via Rprintf (not from the main thread). Didn't happen on the old machine. Old version was 2.14. Now on 3.0.3. Not sure whats going on. Florian.

I have the following code in a R Snippet in Knime. ngrams <- as.list(knime.in$​Ngram) calc <- function(e, l){ if(length(l) == 0){ return(e) } else if  error: C stack usage is too close to the limit. I am trying to set up a RApache server on my Ubuntu 8.04. I have installed apache2, R-2.7.0, and the RApache plugin.

I'm getting the message “Error: C stack usage 19924192 is too close to the limit.” How can I solve this error in RStudio? Change the limit: https://stackoverflow.com/a/14719448/3020740 Posted by baritone at 9:18 AM

in C code, when I run the the > R code in one child thread , > it always print error info: > Error: C stack usage is too close to the limit > > I also  Error: C stack usage is too close to the limit I also try to set R_CStackLimit = (uintptr_t)-1 to disable the C stack check as the R-ext doc say, but it still does not work.

Comments
  • This question suggests perhaps options(expressions = somethinglarge)
  • @mnel The expression nesting depth, the pointer protection stack, and the C stack are three separate (but related) things.
  • Thanks so much for your prompt response, Zack. I think that your answer may be for a Linux OS though? I'm currently running Windows 7 64 bit, does that change things at all? Thanks again for any help.
  • Googling the error message shows that in the past this has usually been an error in user code, so you should probably reduce your problem to a simple reproducible example and post that here.
  • I'm not sure there is an error in the code at all. This is simply a case of probabilities that could in theory end up with infinite recursion. f(1,1) is basically flipping a coin. It could keep coming up heads forever. For a condition where the level of recursion is unknown and unbounded, you are better off coming up with something more iterative, using memoization of prior sample() results to inform future operations. Then the only thing you risk is running out of vector memory, or disk, depending on where you are storing your backlog of results. Recursion can be expensive and brittle.
  • ...or just set it to unlimited.
  • The RAppArmor package offers an interface to setrlimit(2). This functionality may become available in the ulimit package at some point.
  • This function no longer exists in the RAppArmor package. Any ideas where it went?
  • What is the fix for Windows?
  • Changing the limit will not resolve this. A recursive function will simply continue to run until the higher limit is reached.
  • here is actually hidden correct answer - make sure you don't get that deep in recusion...
  • In my case, the error is caused by sourcing the same R script multiple times (i.e. in multiple R scripts) in my project.
  • Same here, but I was doing a summarize. I had it like this: summarize( states = paste0(state,collapse=', ') ). When I should have done something like: summarize( states = paste0(sort(unique(state)),collapse=', ') ). Goal was to get a comma separated list of unique states available for each subgroup.
  • Recursion itself is not the problem though, only the recursion depth.
  • I switched to a higher version of R. It worked once, but the error reappeared and is consistent now. Help!