Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If julia functions which was brought to R throws an error, no real error is thrown, but an error object is returned #216 #508

Open
schlichtanders opened this issue Nov 18, 2023 · 3 comments · May be fixed by #525

Comments

@schlichtanders
Copy link

schlichtanders commented Nov 18, 2023

I created an issue at JuliaCall however after a first inspection, I think it is actually an RCall issue.

Concretely something in the following is not raising julia errors when run on R. Also the stacktrace is not reported.

RCall.jl/src/callback.jl

Lines 122 to 147 in 69c22dd

"""
Wrap a callable Julia object `f` an a R `ClosSxpPtr`.
Constructs the following R code
function(...) .External(juliaCallback, fExPtr, ...)
"""
function sexp(::Type{RClass{:function}}, f)
fptr = protect(sexp(RClass{:externalptr}, f))
body = protect(rlang_p(Symbol(".External"),
juliaCallback,
fptr,
Const.DotsSymbol))
nprotect = 2
local clos
try
args = protect(sexp_arglist_dots())
nprotect += 1
lang = rlang_p(:function, args, body)
clos = reval_p(lang)
finally
unprotect(nprotect)
end
clos
end

Helpful steps to provide error support

Looking into JuliaCall's error handling, there are two places:

  1. on JuliaCall julia side the following try catch converts a julia error to an R object https://github.com/Non-Contradiction/JuliaCall/blob/c05473bea78a0197c639f7e82ab1c6f2e943e1cc/inst/julia/setup.jl#L165-L194
function docall(call1)
    try
        # ...
    catch e
        Rerror(e, stacktrace(catch_backtrace())).p;
    end;
end
  1. on JuliaCall R side the following intercepts these R objects and raises them as R errors https://github.com/Non-Contradiction/JuliaCall/blob/c05473bea78a0197c639f7e82ab1c6f2e943e1cc/R/interface.R#L76-L78
    r <- .julia$do.call_(jcall)

    if (inherits(r, "error")) stop(r)

Both these steps would need to be replicated inside the conversion of a Julia Function to an R function.

@schlichtanders
Copy link
Author

I found where the julia error object is transformed into a simpleError, skipping stacktrace entirely

err = rcall(reval("base::simpleError"), string(e))

this should be changed to the RCall equivalent of JuliaCall's

err = Rerror(e, stacktrace(catch_backtrace())).p;

@schlichtanders
Copy link
Author

The second step needed is to surround the following with the if (inherits(r, "error")) stop(r) logic

RCall.jl/src/callback.jl

Lines 132 to 135 in 69c22dd

body = protect(rlang_p(Symbol(".External"),
juliaCallback,
fptr,
Const.DotsSymbol))

Unfortunately, I couldn't find a way yet how to add such if else logic from Julia.

@schlichtanders
Copy link
Author

schlichtanders commented Nov 18, 2023

I fixed the one part for me now, but it seems juliaCallback cannot be fixed outside of RCall itself.

As soon as I call setup_callbacks() again and run a typical failing example

julia_eval('() = error("fail")')()

I get the following error (or sometimes also a Segmentation fault)

ERROR: UndefVarError: `error_msg` not defined

I bet this has something todo with creating C functions and would be solved if this is officially fixed in RCall.jl

@schlichtanders schlichtanders linked a pull request May 2, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant