You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Manage abort, assert, stderr, and stdout in C library for embedding it into R (and other similar environments) #3363
...
── R CMD check results ──────────────────────── tskitr 0.0.1 ────
Duration: 30.8s
❯ checking compiled code ... WARNING
File 'tskitr/libs/tskitr.so':
Found '___assert_rtn', possibly from 'assert' (C)
Objects: 'tskit/haplotype_matching.o', 'tskit/kastore.o',
'tskit/tables.o', 'tskit/trees.o'
Found '___stderrp', possibly from 'stderr' (C)
Objects: 'tskit/core.o', 'tskit/genotypes.o',
'tskit/haplotype_matching.o', 'tskit/tables.o', 'tskit/trees.o'
Found '___stdoutp', possibly from 'stdout' (C)
Object: 'tskit/core.o'
Found '_abort', possibly from 'abort' (C)
Objects: 'tskit/core.o', 'tskit/genotypes.o',
'tskit/haplotype_matching.o', 'tskit/tables.o', 'tskit/trees.o'
Compiled code should not call entry points which might terminate R nor
write to stdout/stderr instead of to the console, nor use Fortran I/O
nor system RNGs nor [v]sprintf.
See 'Writing portable packages' in the 'Writing R Extensions' manual.
0 errors ✔ | 1 warning ✖ | 0 notes ✔
...
A standard way of dealing with this issue in R packages is to avoid crashing R session because the underlying process crashed and communicating abort/errors/warnings/messages to R session from the underlying process. I believe this is done using something like #ifdef R_BUILD (to be revised) and using Rcpp::stop for stderr (possibly also for assert though sometimes I see that in R packages these are removed from low-level code at compile time using -DNDEBUG - not sure if we should error though?, maybe using Rcpp::warning instead?) and similarly for stderr and stdout.
The above describes R way of doing things, but I wonder if we should be thinking of this more generally.
I think most of these will be coming from the tsk_bug_assert macro which you could either undefine at compile time (probably?) or we could add some flags to conditionally compile it.
Ideally, we would redefine the macro in the tskit-R c++ code, which could be as simple as
#definetsk_bug_assert(condition) \
do { \
if (!(condition)) { \
correct_way_to_raise_error_message("Bug detected in %s at line %d. %s\n", __FILE__, __LINE__, \
TSK_BUG_ASSERT_MESSAGE); \
Rcpp::stop()... \
} \
} while (0)
Otherwise, there may be a few stray printfs, which I'm very happy to add conditional compiles for.
Thanks for this. I have now used PKG_CFLAGS = -DNDEBUG to remove the warning about assert calls (HighlanderLab/RcppTskit@92d39c6). I will bite into redefining the tsk_bug_assert macro next and report back.
...
✔ checking pragmas in C/C++ headers and code ...
✔ checking compilation flags used ...
N checking compiled code ...
File ‘tskitr/libs/tskitr.so’:
Found ‘___stdoutp’, possibly from ‘stdout’ (C)
Object: ‘tskit/core.o’
Compiled code should not call entry points which might terminate R nor
write to stdout/stderr instead of to the console, nor use Fortran I/O
nor system RNGs nor [v]sprintf.
See ‘Writing portable packages’ in the ‘Writing R Extensions’ manual.
✔ checking examples (4.2s)
...
Searching for stdout I only find it used in core.h
This one is more challenging for me ... If I understand this correctly, it returns a FILE stream (?) and this is how it's used downstream, say in tsk_matvec_calculator_print_state(const tsk_matvec_calculator_t *self, FILE *out). I don't know how to go around here.
This is a trickier one and will probably need a small change to tskit C to accomodate. I've made a draft proposal in #3364 which might work. Is there a "stdout"-like file stream available in R that C code can write to?
If not then we can probably just define TSK_DEFAULT_DEBUG_STREAM to NULL, but we would need to make sure that the TSK_DEBUG flag cannot be passed to any of the C functions (which should be straightforward I think, but it depends on how much control you give the R code over the low-level parameters passed to C).
We could also #define away all the calls to the x_print_state functions, but I think that would lead to different compiler warnings.
The most useful function for printing from a C routine compiled into R is Rprintf.
This is used in exactly the same way as printf, but is guaranteed to write to R’s output
(which might be a GUI console rather than a file, and can be re-directed by sink).
It is wise to write complete lines (including the "\n") before returning to R. It is defined in R_ext/Print.h.
The function REprintf is similar but writes on the error stream (stderr)
which may or may not be different from the standard output stream.
Functions Rvprintf and REvprintf are analogues using the vprintf interface.
Because that is a C99[168](https://cran.r-project.org/doc/manuals/r-release/R-exts.html#FOOT168) interface,
they are only defined by R_ext/Print.h in C++ code if the macro R_USE_C99_IN_CXX
is defined before it is included or (as from R 4.0.0) a C++11 compiler is used.
Another circumstance when it may be important to use these functions
is when using parallel computation on a cluster of computational nodes,
as their output will be re-directed/logged appropriately.
So for R side we should send the output to these functions so it can be captured by R. The functions are Rprintf() and REprintf(). There is also Rf_error() and Rf_warning() but we are only interested in Rprintf() here.
Would this work (I am on thin ice here): If you #define FILE *tsk_get_debug_stream() I could redefine it to call Rprintf() like I did with tsk_bug_assert()?
It's not worth the bother. These are just debug messages for C developers, and are not enabled in the Python interface. We'll just set the stream to NULL as above, and just make sure the debug flag can't be passed.
I've just merge this @gregorgorjanc - we can put out a C point release if that would help? (But maybe best to see if there's any more niggles that pop up?)
@jeromekelleher I have managed to work with your recent changes in #3364 and now don't have R CMD check complaints mentioned in this issue. So, happy to close it;) Thanks!
As to the new version, I don't know what is versioning framework you follow. This change is pretty small so maybe a new release is over the top. I use a git submodule, so I just updated to which commit it points to on my end.
Great! We usually recommend pinning to a release tag for the C API. We can easily tag a point release of the C API to formalise this once you've got things running and ready to push to CRAN.
Activity
jeromekelleher commentedon Jan 10, 2026
I think most of these will be coming from the tsk_bug_assert macro which you could either undefine at compile time (probably?) or we could add some flags to conditionally compile it.
Ideally, we would redefine the macro in the tskit-R c++ code, which could be as simple as
Otherwise, there may be a few stray printfs, which I'm very happy to add conditional compiles for.
jeromekelleher commentedon Jan 10, 2026
For the
assertcalls, I think compiling with-DNDEBUGis perfectly acceptable (and indeed the standard practise)gregorgorjanc commentedon Jan 10, 2026
Thanks for this. I have now used
PKG_CFLAGS = -DNDEBUGto remove the warning aboutassertcalls (HighlanderLab/RcppTskit@92d39c6). I will bite into redefining thetsk_bug_assertmacro next and report back.gregorgorjanc commentedon Jan 11, 2026
I now also managed to address
abort(redefiningtsk_bug_assertHighlanderLab/RcppTskit@b2a29f1, I later also foundkas_bug_assert()HighlanderLab/RcppTskit@00870d9) andstderr(redefiningtsk_trace_error()HighlanderLab/RcppTskit@00870d9). All downstream, so no need to do anything intskit!I am now down to this feedback from R CMD check:
Searching for
stdoutI only find it used incore.hThis one is more challenging for me ... If I understand this correctly, it returns a FILE stream (?) and this is how it's used downstream, say in
tsk_matvec_calculator_print_state(const tsk_matvec_calculator_t *self, FILE *out). I don't know how to go around here.Any suggestions?
Thanks!
jeromekelleher commentedon Jan 12, 2026
This is a trickier one and will probably need a small change to tskit C to accomodate. I've made a draft proposal in #3364 which might work. Is there a "stdout"-like file stream available in R that C code can write to?
If not then we can probably just define
TSK_DEFAULT_DEBUG_STREAMto NULL, but we would need to make sure that the TSK_DEBUG flag cannot be passed to any of the C functions (which should be straightforward I think, but it depends on how much control you give the R code over the low-level parameters passed to C).We could also #define away all the calls to the
x_print_statefunctions, but I think that would lead to different compiler warnings.gregorgorjanc commentedon Jan 14, 2026
I hunted this one down in the R extensions manual - https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Printing-1, which says
So for R side we should send the output to these functions so it can be captured by R. The functions are
Rprintf()andREprintf(). There is alsoRf_error()andRf_warning()but we are only interested inRprintf()here.Would this work (I am on thin ice here): If you
#define FILE *tsk_get_debug_stream()I could redefine it to callRprintf()like I did withtsk_bug_assert()?jeromekelleher commentedon Jan 14, 2026
It's not worth the bother. These are just debug messages for C developers, and are not enabled in the Python interface. We'll just set the stream to NULL as above, and just make sure the debug flag can't be passed.
gregorgorjanc commentedon Jan 14, 2026
Ok! That makes it even simpler for work downstream! Let me know when you commit these changes so I can pull latest tskit C code. Thanks!
jeromekelleher commentedon Jan 19, 2026
I've just merge this @gregorgorjanc - we can put out a C point release if that would help? (But maybe best to see if there's any more niggles that pop up?)
jeromekelleher commentedon Jan 19, 2026
Also, if the update works, can you close this issue please?
gregorgorjanc commentedon Jan 20, 2026
@jeromekelleher I have managed to work with your recent changes in #3364 and now don't have
R CMD checkcomplaints mentioned in this issue. So, happy to close it;) Thanks!As to the new version, I don't know what is versioning framework you follow. This change is pretty small so maybe a new release is over the top. I use a git submodule, so I just updated to which commit it points to on my end.
jeromekelleher commentedon Jan 20, 2026
Great! We usually recommend pinning to a release tag for the C API. We can easily tag a point release of the C API to formalise this once you've got things running and ready to push to CRAN.