Replicating CRAN valgrind issues - r

I am trying to fix some issue with my package CamelUp on CRAN. This package uses Rcpp to implement a board game. My recent CRAN submissions have come back with comments and output such as:
==32365== 16,591,624 (2,608,512 direct, 13,983,112 indirect) bytes in
20,379 blocks are definitely lost in loss record 3,036 of 3,036
==32365== at 0x4838E86: operator new(unsigned long)
(/builddir/build/BUILD/valgrind-3.15.0/coregrind/m_replacemalloc/vg_replace_malloc.c:344)
==32365== by 0x184ED3E5: Board::Board(Board const&)
(/tmp/CamelUp.Rcheck/00_pkg_src/CamelUp/src/Board.cpp:67)
...
==32365== by 0x1853045D: Simulator::simulateDecision(bool, int)
(/tmp/CamelUp.Rcheck/00_pkg_src/CamelUp/src/Simulator.cpp:64)
==32365== by 0x18536509: Rcpp::CppMethod2<Simulator, Rcpp::Vector<19,
Rcpp::PreserveStorage>, bool, int>::operator()(Simulator*, SEXPREC**)
(R-devel/site-library/Rcpp/include/Rcpp/module/Module_generated_CppMethod.h:195)
==32365== by 0x18535B32:
Rcpp::class_<Simulator>::invoke_notvoid(SEXPREC*, SEXPREC*, SEXPREC**,
int) (R-devel/site-library/Rcpp/include/Rcpp/module/class.h:234)
==32365== by 0x17B9EBE1: CppMethod__invoke_notvoid(SEXPREC*)
(/tmp/RtmpKDbrDI/R.INSTALL1d1838b282b2/Rcpp/src/module.cpp:220)
I'm having trouble replicating these errors and I'm wondering if there is a straightforward way to use valgrind with my package to reproduce these errors. I've tried running locally with valgrind but couldn't get the track origins option to work and make it clear where these errors were in my code. I have also tried using Travis-CI with the following .travis.yml file:
language: r
cache: packages
r_check_args: '--use-valgrind'
addons:
apt:
packages:
- valgrind
r:
- oldrel
- release
- devel
env:
- VALGRIND_OPTS='--tool=memcheck --memcheck:leak-check=full --track-origins=yes'
I'm hoping there is a way to replicate these errors so I can fix them.

I successfully used Docker to run valgrind for my tests, but I ended up deciding that the best way to integrate this testing was with Travis-CI. My .travis.yml file looks like this:
language: r
cache: packages
addons:
apt:
packages:
- valgrind
r:
- oldrel
- release
- devel
after_success:
- R -e "install.packages('${PKG_TARBALL}', repos=NULL, type='source')"
- cd tests
- R -d "valgrind --tool=memcheck --leak-check=full --track-origins=yes" --vanilla < testthat.R
- cd ..
This now runs the tests with valgrind, although I have to manually scroll through to see the results. At some point I will make the build fail if there are memory leaks, but for now this works for me. I was mostly unfamiliar with Docker and valgrind, and didn't realize I need to be in the tests directory to run the tests in testthat.R.

Related

M1 Silicon gfortran c++ in R install issues

I bought an M1 Silicon with Monterey back in January and --- after a three+ month pause -- am trying again to get Rcpp running so I can install RStan. Following these instructions in response to (I'd like to just comment on this other issue, but being new to posting on Stack Overflow it tells me that I cannot and what I am about to post is certainly NOT an answer):
Configuring compilers on Mac M1 (Big Sur, Monterey) for Rcpp and other tools
While step 3 (installing gfortran) looks to go well, when I ask about gfortran things seem less hopeful:
-bash: gfortran: command not found
Or maybe this is not how to ask if all went well with the installation?
No matter, I charge on to step 4 installing from openmp-13.0.0-darwin21-Release.tar.gz based on:
Apple clang version 13.1.6 (clang-1316.0.21.2.3)
Target: arm64-apple-darwin21.4.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
I confirm that I have the files where expected, then create the .R directory and the Makevars file (in nano) then try to run the R and get ....
+ #endif
+ }
+ ')
/Users/lizzie/.R/Makevars:6: *** missing separator. Stop.
Error in Rcpp::sourceCpp(code = "\n#include <RcppArmadillo.h>\n#ifdef _OPENMP\n# include <omp.h>\n#endif\n\n// [[Rcpp::depends(RcppArmadillo)]]\n// [[Rcpp::export]]\nvoid omp_test()\n{\n#ifdef _OPENMP\n Rprintf(\"OpenMP threads available: %d\\n\", omp_get_max_threads());\n#else\n Rprintf(\"OpenMP not supported\\n\");\n#endif\n}\n") :
Error 1 occurred building shared library.
WARNING: The tools required to build C++ code for R were not found.
Please install Command Line Tools for XCode (or equivalent).
Any thoughts or ideas much appreciated. Again, I am new to posting to StackOverflow so apologize as I am sure I am doing this wrong.

Error installing miniconda on GitHub Actions in an R project

I am trying to build and deploy this bookdown project with GitHub Actions. One of the chapters uses the keras R package, which means I need to install Conda (or set up a virtual environment). At the end of the Miniconda installation command, there is an error when trying to collect metadata.
2020-06-24T04:47:59.7495480Z * Miniconda has been successfully installed at '/Users/runner/Library/r-miniconda'.
2020-06-24T04:47:59.7496060Z [1] "/Users/runner/Library/r-miniconda"
2020-06-24T04:48:00.3909040Z * Project '~/runners/2.263.0/work/drake/drake' loaded. [renv 0.10.0]
2020-06-24T04:48:00.7964920Z * The project and lockfile are out of sync -- use `renv::status()` for more details.
2020-06-24T04:48:00.7968340Z Warning message:
2020-06-24T04:48:00.7969190Z Project requested R version '3.6.0' but '4.0.1' is currently being used
2020-06-24T04:48:05.2408080Z Collecting package metadata (current_repodata.json): ...working... failed
2020-06-24T04:48:05.2410390Z
2020-06-24T04:48:05.2410820Z NotWritableError: The current user does not have write permissions to a required path.
2020-06-24T04:48:05.2411080Z path: /usr/local/miniconda/pkgs/cache/b89cf7bf.json
2020-06-24T04:48:05.2411230Z uid: 501
2020-06-24T04:48:05.2411350Z gid: 20
2020-06-24T04:48:05.2411430Z
2020-06-24T04:48:05.2411690Z If you feel that permissions on this path are set incorrectly, you can manually
2020-06-24T04:48:05.2411940Z change them by executing
2020-06-24T04:48:05.2412010Z
2020-06-24T04:48:05.2412260Z $ sudo chown 501:20 /usr/local/miniconda/pkgs/cache/b89cf7bf.json
2020-06-24T04:48:05.2412330Z
2020-06-24T04:48:05.2413470Z In general, it's not advisable to use 'sudo conda'.
2020-06-24T04:48:05.2413570Z
2020-06-24T04:48:05.2414250Z
2020-06-24T04:48:05.2886400Z ##[error]Error: Error 1 occurred creating conda environment r-reticulate
2020-06-24T04:48:05.2890770Z Execution halted
2020-06-24T04:48:05.3050700Z ##[error]Process completed with exit code 1.
The full job log is here.
Depending on how R is set up, this post might be helpful for you. You might need to configure the .Renviron file.
Unable to change python path in reticulate (R)

How to run covr::codecov() for a R package on Travis CI

I am trying to add Codecov support via library(covr) to my personal R package sesh.
When I check locally the coverage tests run and report without incident:
covr::package_coverage()
sesh Coverage: 68.75%
R/executeDevtoolDocument.R: 0.00%
R/sesh.R: 69.23%
But when it runs on Travis it encounters an error for missing token:
$ Rscript -e 'covr::codecov()'
Error in if (nzchar(token)) { : argument is of length zero
Calls: <Anonymous>
Execution halted
The R CMD check runs successfully on Travis.
The contents of my .travis.yml:
language: R
matrix:
include:
- r: release
after_success: Rscript -e 'covr::codecov()'
r_github_packages:
- r-lib/covr
And a link to the most recent Travis report.
I have tried to faithfully follow the covr README for getting set up. And the README says Travis is supported without needing CODECOV_TOKEN, so I have not tried to pass one yet.
What am I missing here?
Following is my .travis.yml
language: r
cache: packages
script:
- R CMD build .
- R CMD check *tar.gz
r_github_packages:
- r-lib/covr
after_success:
- Rscript -e 'covr::codecov()'
Adding the repository upload token to codecov.yml avoids the error and successfully runs the coverage report.
codecov:
token: a1c53d1f-266f-47bc-bb23-3b3d67c57b2d
The token is found in the 'Settings(tab) >>> General(sidebar)' menu on the Codecov page for the repo (which is only visible once you are logged in).

nginx: [error] failed to initialize Lua VM

I am trying to check memory leaks in my modules of nginx with valgrind.I am trying the following command
valgrind --leak-check=full --tool=memcheck --show-reachable=yes --log-file="/tmp/val.out" -v /usr/local/nginx -c /usr/local/conf/nginx.conf
I am getting the error nginx: [error] failed to initialize Lua VM
I am using nginx-1.6.2 on Cent OS 7 wit lua 0.9.15.
I had the same problem and it was fixed by upgrading Valgrind and adding some additional flags to LuaJIT compilation.
Look into:
https://github.com/openresty/lua-nginx-module/issues/681
Specifically:
==52538== Using Valgrind-3.10.0 and LibVEX;
Your version of valgrind does not support the MAP_32BIT flag required by LuaJIT's own allocator. You need either an older version of valgrind (like 3.8.1) or a newer version (like 3.11.0).
https://groups.google.com/forum/#!topic/openresty/riEO_YXTwz4
Specifically (using Google translate):
This is because LuaJIT own memory allocator MAP_32BIT use this flag in Linux x86_64 above to call mmap ()
System call. And Valgrind 3.9.0 is no longer supported from the mmap MAP_32BIT this flag, it will make LuaJIT
Initialization failed.
The solution is to re-compile a special version of LuaJIT, force it to use a dispenser system, using a command like this at compile time LuaJIT:
make CCDEBUG=-g Q= XCFLAGS='-DLUAJIT_USE_VALGRIND -DLUAJIT_USE_SYSMALLOC'
Here the most important thing is LUAJIT_USE_SYSMALLOC this macro. Of course, for best results, you should also specify the following C compiler options:
-DLUA_USE_APICHECK -DLUA_USE_ASSERT

Trouble with Pandoc installation on Ubuntu 14.04LTS for using with R Markdown

This question is a corollary of my attempts to get some experience with creating reproducible reports from R Markdown documents via knitr and rmarkdown R packages. While it seems that .Rmd => HTML conversion is automated from within RStudio (Knit HTML button), my attempt to do the same outside of RStudio (Rscript -e 'library(rmarkdown); render("knitr-example-slides-1.Rmd")') failed due to, according to the message, lack of pandoc on my system. This is most likely false, since RStudio somehow managed to perform the conversion. Therefore, it is most likely an access and/or path issue.
Without knowledge of where RStudio maintains pandoc and details about the access, I have decided to install pandoc myself. Unfortunately, sudo apt-get install pandoc has not been very helpful, due to the fact that current version of pandoc in Ubuntu's trusty repository (14.04LTS) is 1.12.2.1. According to rmarkdown's message, version 1.12.3 or higher is required. "Not a big deal", I have thought, and followed instructions on installing pandoc in case of too old version in repository (http://johnmacfarlane.net/pandoc/installing.html). That requires installing the Haskell platform, which is pretty big and which output is rather verbose. After some time, I finally has been greeted with the following failure message:
[ 6 of 57] Compiling Text.Pandoc.Readers.TeXMath ( src/Text/Pandoc/Readers/TeXMath.hs, dist/build/Text/Pandoc/Readers/TeXMath.o )
src/Text/Pandoc/Readers/TeXMath.hs:30:38:
Ambiguous occurrence `readTeXMath'
It could refer to either `Text.Pandoc.Readers.TeXMath.readTeXMath',
defined at src/Text/Pandoc/Readers/TeXMath.hs:56:1
or `Text.TeXMath.readTeXMath',
imported from `Text.TeXMath' at src/Text/Pandoc/Readers/TeXMath.hs:33:1-19
(and originally defined in `Text.TeXMath.Parser')
Failed to install pandoc-1.12.4.2
cabal: Error: some packages failed to install:
pandoc-1.12.4.2 failed during the building phase. The exception was:
ExitFailure 1
pandoc-citeproc-0.3.1 depends on pandoc-1.12.4.2 which failed to install.
Firstly, I'm not sure how to fix it. Secondly, I very much suspect that there should be an easier way to enjoy generating reproducible reports with rmarkdown and pandoc than this. Your advice will be appreciated!
UPDATE (see comments):
Rscript -e 'library(rmarkdown); render("knitr-example-slides-1.Rmd")'
processing file: knitr-example-slides-1.Rmd
|....... | 11%
ordinary text without R code
|.............. | 22%
label: setup (with options)
List of 1
$ include: logi FALSE
Quitting from lines 6-8 (knitr-example-slides-1.Rmd)
Error in eval(expr, envir, enclos) : object 'opts_chunk' not found
Calls: render ... handle -> withCallingHandlers -> withVisible -> eval -> eval
Execution halted
Basically, #daroczig answered this question in his comment above, so I will repeat it here, plus will add an answer to the relevant minor question on the issue, appeared after the main question's issue fix.
1) "A binary compiled version of Pandoc is already shipped with RStudio, so you can simply create a symlink, so that you could easily use that outside of the RStudio eco-system: https://github.com/rstudio/rmarkdown/blob/master/PANDOC.md#newer-systems-debianubuntufedora";
2) I have fixed the error, experienced after applying the advice above (see my comment), by calling opts_chunk() using explicit package reference: knitr::opts_chunk(), following guidelines here: http://rmarkdown.rstudio.com/authoring_migrating_from_v1.html (thanks to #Yihui for pointing me to this document in a different question: Transitioning research project to knitr-based setup).

Resources