title: caught segfault - 'memory not mapped' error in R
body: <p>I have a problem running some R scripts on our cluster. The problems appeared suddenly (all the scripts were working just fine but one day they started giving a <code>caught segfault</code> error). I cannot provide reproducible code because I can't even reproduce the error on my own computer - it only happens on the cluster. I am also using the same code for two sets of data - one is quite small and runs fine, the other one works with bigger data frames (about 10 million rows) and collapses at certain points. I am only using packages from CRAN repository; R and all the packages should be up to date. The error shows up at totally unrelated actions, see the examples below:</p>
  
  <p>Session info:</p>
  
  <pre><code>R version 3.4.3 (2017-11-30)
  Platform: x86_64-redhat-linux-gnu (64-bit)
  Running under: CentOS Linux 7 (Core)
  </code></pre>
  
  <p><strong>Writing variable to NetCDF file</strong></p>
  
  <pre><code># code snippet
  library(ncdf4)
  library(reshape2)
  
  input &lt;- read.csv("input_file.csv")
  species &lt;- "no2"
  dimX &lt;- ncdim_def(name="x", units = "m", vals = unique(input$x), unlim = FALSE)
  dimY &lt;- ncdim_def(name="y", units = "m", vals = unique(input$y), unlim = FALSE)
  dimTime &lt;- ncdim_def(name = "time", units = "hours", unlim = TRUE)
  
  varOutput &lt;- ncvar_def(name = species, units = "ug/m3",
                  dim = list(dimX, dimY, dimTime), missval = -9999, longname = species)
  
  nc_file &lt;- nc_create(filename = "outFile.nc", vars = list(varOutput), force_v4 = T)
  
  ncvar_put(nc = nc_file, varid = species, vals = acast(input, x~y), start = c(1,1,1),
        count = c(length(unique(input$x)), length(unique(input$y)), 1))
  </code></pre>
  
  <p>At this point, I get the following error:</p>
  
  <pre><code> *** caught segfault ***
  address 0x2b607cac2000, cause 'memory not mapped'
  
  Traceback:
   1: id(rev(ids), drop = FALSE)
   2: cast(data, formula, fun.aggregate, ..., subset = subset, fill = fill,     drop = drop, value.var = value.var)
   3: acast(result, x ~ y)
   4: ncvar_put(nc = nc_file, varid = species, vals = acast(result, x ~     y), start = c(1, 1), count = c(length(unique(result$x)),     length(unique(result$y))))
  An irrecoverable exception occurred. R is aborting now ...
  /opt/sge/default/spool/node10/job_scripts/122270: line 3: 13959 Segmentation fault      (core dumped)
  </code></pre>
  
  <hr>
  
  <p><strong>Complex code with parallel computation</strong></p>
  
  <pre><code> *** caught segfault ***
  address 0x330d39b40, cause 'memory not mapped'
  
  Traceback:
   1: .Call(gstat_fit_variogram, as.integer(fit.method), as.integer(fit.sills),     as.integer(fit.ranges))
   2: fit.variogram(experimental_variogram, model = vgm(psill = psill,     model = model, range = range, nugget = nugget, kappa = kappa),     fit.ranges = c(fit_range), fit.sills = c(fit_nugget, fit_sill),     debug.level = 0)
   3: doTryCatch(return(expr), name, parentenv, handler)
   4: tryCatchOne(expr, names, parentenv, handlers[[1L]])
   5: tryCatchList(expr, classes, parentenv, handlers)
   6: tryCatch(expr, error = function(e) {    call &lt;- conditionCall(e)          if (!is.null(call)) {        if (identical(call[[1L]], quote(doTryCatch)))             call &lt;- sys.call(-4L)        dcall &lt;- deparse(call)[1L]        prefix &lt;- paste("Error in", dcall, ": ")        LONG &lt;- 75L        msg &lt;- conditionMessage(e)        sm &lt;- strsplit(msg, "\n")[[1L]]        w &lt;- 14L + nchar(dcall, type = "w") + nchar(sm[1L], type = "w")        if (is.na(w))             w &lt;- 14L + nchar(dcall, type = "b") + nchar(sm[1L],                 type = "b")        if (w &gt; LONG)             prefix &lt;- paste0(prefix, "\n  ")    }    else prefix &lt;- "Error : "    msg &lt;- paste0(prefix, conditionMessage(e), "\n")    .Internal(seterrmessage(msg[1L]))    if (!silent &amp;&amp; identical(getOption("show.error.messages"),         TRUE)) {        cat(msg, file = outFile)        .Internal(printDeferredWarnings())    }    invisible(structure(msg, class = "try-error", condition = e))})
   7: try(fit.variogram(experimental_variogram, model = vgm(psill = psill,     model = model, range = range, nugget = nugget, kappa = kappa),     fit.ranges = c(fit_range), fit.sills = c(fit_nugget, fit_sill),     debug.level = 0), TRUE)
   8: getModel(initial_sill - initial_nugget, m, initial_range, k,     initial_nugget, fit_range, fit_sill, fit_nugget, verbose = verbose)
   9: autofitVariogram(lmResids ~ 1, obsDf, model = "Mat", kappa = c(0.05,     seq(0.2, 2, 0.1), 3, 5, 10, 15), fix.values = c(NA, NA, NA),     start_vals = c(NA, NA, NA), verbose = F)
  10: main_us(obsDf[obsDf$class == "rural" | obsDf$class == "rural-nearcity" |     obsDf$class == "rural-regional" | obsDf$class == "rural-remote",     ], grd_alt, grd_pop, lm_us, fitvar_us, logTransform, plots,     "RuralSt", period, preds)
  11: doTryCatch(return(expr), name, parentenv, handler)
  12: tryCatchOne(expr, names, parentenv, handlers[[1L]])
  13: tryCatchList(expr, classes, parentenv, handlers)
  14: tryCatch(main_us(obsDf[obsDf$class == "rural" | obsDf$class ==     "rural-nearcity" | obsDf$class == "rural-regional" | obsDf$class ==     "rural-remote", ], grd_alt, grd_pop, lm_us, fitvar_us, logTransform,     plots, "RuralSt", period, preds), error = function(e) e)
  15: eval(.doSnowGlobals$expr, envir = .doSnowGlobals$exportenv)
  16: eval(.doSnowGlobals$expr, envir = .doSnowGlobals$exportenv)
  17: doTryCatch(return(expr), name, parentenv, handler)
  18: tryCatchOne(expr, names, parentenv, handlers[[1L]])
  19: tryCatchList(expr, classes, parentenv, handlers)
  20: tryCatch(eval(.doSnowGlobals$expr, envir = .doSnowGlobals$exportenv),     error = function(e) e)
  21: (function (args) {    lapply(names(args), function(n) assign(n, args[[n]], pos = .doSnowGlobals$exportenv))    tryCatch(eval(.doSnowGlobals$expr, envir = .doSnowGlobals$exportenv),         error = function(e) e)})(quote(list(timeIndex = 255L)))
  22: do.call(msg$data$fun, msg$data$args, quote = TRUE)
  23: doTryCatch(return(expr), name, parentenv, handler)
  24: tryCatchOne(expr, names, parentenv, handlers[[1L]])
  25: tryCatchList(expr, classes, parentenv, handlers)
  26: tryCatch(do.call(msg$data$fun, msg$data$args, quote = TRUE),     error = handler)
  27: doTryCatch(return(expr), name, parentenv, handler)
  28: tryCatchOne(expr, names, parentenv, handlers[[1L]])
  29: tryCatchList(expr, classes, parentenv, handlers)
  30: tryCatch({    msg &lt;- recvData(master)    if (msg$type == "DONE") {        closeNode(master)        break    }    else if (msg$type == "EXEC") {        success &lt;- TRUE        handler &lt;- function(e) {            success &lt;&lt;- FALSE            structure(conditionMessage(e), class = c("snow-try-error",                 "try-error"))        }        t1 &lt;- proc.time()        value &lt;- tryCatch(do.call(msg$data$fun, msg$data$args,             quote = TRUE), error = handler)        t2 &lt;- proc.time()        value &lt;- list(type = "VALUE", value = value, success = success,             time = t2 - t1, tag = msg$data$tag)        msg &lt;- NULL        sendData(master, value)        value &lt;- NULL    }}, interrupt = function(e) NULL)
  31: slaveLoop(makeSOCKmaster(master, port, timeout, useXDR))
  32: parallel:::.slaveRSOCK()
  An irrecoverable exception occurred. R is aborting now ...
  </code></pre>
  
  <hr>
  
  <p>Is it likely that there is an issue with the cluster rather than the code (or R)? I don't know if it could be related, but since some time ago we've been getting error messages like these:</p>
  
  <pre><code>Message from syslogd@master1 at Mar  8 13:51:37 ...
   kernel:[Hardware Error]: MC4 Error (node 1): DRAM ECC error detected on the NB.
  
  Message from syslogd@master1 at Mar  8 13:51:37 ...
   kernel:[Hardware Error]: Error Status: Corrected error, no action required.
  
  Message from syslogd@master1 at Mar  8 13:51:37 ...
   kernel:[Hardware Error]: CPU:4 (15:2:0) MC4_STATUS[-|CE|MiscV|-|AddrV|-|-|CECC]: 0x9c08400067080a13
  
  Message from syslogd@master1 at Mar  8 13:51:37 ...
  kernel:[Hardware Error]: MC4_ADDR: 0x000000048f32b490
  
  Message from syslogd@master1 at Mar  8 13:51:37 ...
   kernel:[Hardware Error]: cache level: L3/GEN, mem/io: MEM, mem-tx: RD, part-proc: RES (no timeout)
  </code></pre>
  
  <p>I have tried to uninstall and reinstall packages based on <a href="https://stackoverflow.com/questions/25672974/caught-segfault-error-in-r">this question</a> but it didn't help.</p>