PythonCall.jl icon indicating copy to clipboard operation
PythonCall.jl copied to clipboard

finalizer may never be called

Open dpinol opened this issue 1 year ago • 2 comments

Is your feature request related to a problem? Please describe. In Julia, it looks like finalizers are always called when exiting. E.g., the following code always prints "hola".

mutable struct S
    a
end
function S()
    return finalizer(_->Core.println("hola"), S(3))
end
S()

However, if we wrap it with juliacall, the finalizer is never executed.

import juliacall
juliacall.Main.include("kk.jl")

Describe the solution you'd like I'd like finalizers to always be executed

Describe alternatives you've considered My current workaround is finishing with

juliacall.Main.GC.gc()

dpinol avatar Jun 26 '24 16:06 dpinol

Ok yeah looks like I should call jl_atexit_hook to stop the Julia runtime when Python exits. https://github.com/JuliaLang/julia/blob/f3298ee5e2b08f633a6a313d094c732ff1ce7e85/src/init.c#L236

cjdoris avatar Jun 27 '24 16:06 cjdoris

thanks, @cjdoris what about #516 ? I works with this

Main.seval(
            """ mutable struct S
                    a
                end;
                function S()
                    return finalizer(_->Core.println("works!!!"), S(3));
                end;
                S();
        """

dpinol avatar Jul 01 '24 16:07 dpinol