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

Finalize Python runtime while shutting down Julia #603

Merged
merged 4 commits into from
Nov 4, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/PyCall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,11 @@ it is equivalent to a `PyNULL()` object.
"""
ispynull(o::PyObject) = o.o == PyPtr_NULL

function pydecref_(o::Union{PyPtr,PyObject})
function pydecref_(o::Union{PyPtr,PyObject}) :: Nothing
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't use the return value anywhere. So why not return nothing always?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems better to return something potentially useful (o), so that the result can be chained with some other function. Also, this way it mirrors pyincref. Why not just return o always?

if _finalized[]
return
end
ccall(@pysym(:Py_DecRef), Cvoid, (PyPtr,), o)
return o
end

function pydecref(o::PyObject)
Expand Down
36 changes: 36 additions & 0 deletions src/pyinit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,40 @@ function __init__()
end
end
end

# Configure finalization steps.
#
# * In julia/PyCall, `julia` needs to call `Py_Finalize` to
# finalize Python runtime to invoke Python functions registered
# in Python's exit hook. This is done by Julia's `atexit` exit
# hook.
#
# * In PyJulia, `python` needs to call `jl_atexit_hook` in its
# exit hook instead.
#
# In both cases, it is important to not invoke GC of the finalized
# runtime. This is ensured by:
@pycheckz ccall((@pysym :Py_AtExit), Cint, (Ptr{Cvoid},),
@cfunction($_set_finalized, Cvoid, ()))
if !already_inited
# Once `_set_finalized` is successfully registered to
# `Py_AtExit`, it is safe to call `Py_Finalize` during
# finalization of this Julia process.
atexit(Py_Finalize)
end
end

const _finalized = Ref(false)
# This flag is set via `Py_AtExit` to avoid calling `pydecref_` after
# Python is finalized.

function _set_finalized()
# This function MUST NOT invoke any Python APIs.
# https://docs.python.org/3/c-api/sys.html#c.Py_AtExit
_finalized[] = true
return nothing
end

function Py_Finalize()
ccall(@pysym(:Py_Finalize), Cvoid, ())
end
14 changes: 14 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -666,4 +666,18 @@ def try_call(f):
pybuiltin("Exception"))
end

@testset "atexit" begin
script = """
$(Base.load_path_setup_code())

using PyCall

pyimport("atexit")[:register]() do
println("atexit called")
end
"""
out = read(`$(Base.julia_cmd()) -e $script`, String)
@test occursin("atexit called", out)
end

include("test_pyfncall.jl")