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

Call Py_FinalizeEx() when process exits #187

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
1 change: 1 addition & 0 deletions ext/pycall/libpython.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ pycall_init_libpython_api_table(VALUE libpython_handle)
INIT_API_TABLE_ENTRY(PyUnicode_Type, required);

INIT_API_TABLE_ENTRY(Py_InitializeEx, required);
INIT_API_TABLE_ENTRY(Py_FinalizeEx, required);
INIT_API_TABLE_ENTRY(Py_IsInitialized, required);
INIT_API_TABLE_ENTRY(Py_GetVersion, required);

Expand Down
10 changes: 10 additions & 0 deletions ext/pycall/pycall.c
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,15 @@ pycall_libpython_api_PyList_GetItem(VALUE mod, VALUE pyptr, VALUE idx)
return pycall_pyptr_new(pyobj_item);
}

static VALUE
pycall_libpython_api_Py_FinalizeEx(VALUE mod)
{
assert(Py_API(Py_IsInitialized()));

return Py_API(Py_FinalizeEx)();
}


/* ==== PyCall::Helpers ==== */

static VALUE
Expand Down Expand Up @@ -2372,6 +2381,7 @@ Init_pycall(void)
rb_define_module_function(mAPI, "PyObject_Dir", pycall_libpython_api_PyObject_Dir, 1);
rb_define_module_function(mAPI, "PyList_Size", pycall_libpython_api_PyList_Size, 1);
rb_define_module_function(mAPI, "PyList_GetItem", pycall_libpython_api_PyList_GetItem, 2);
rb_define_module_function(mAPI, "Py_FinalizeEx", pycall_libpython_api_Py_FinalizeEx, 0);

/* PyCall::LibPython::Helpers */

Expand Down
1 change: 1 addition & 0 deletions ext/pycall/pycall_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,7 @@ typedef struct {
PyObject *PyExc_TypeError;

void (* Py_InitializeEx)(int);
int (* Py_FinalizeEx)(void);
int (* Py_IsInitialized)();
char const * (* Py_GetVersion)();

Expand Down
11 changes: 11 additions & 0 deletions lib/pycall/init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ class << LibPython
require 'pycall/slice'
const_set(:PYTHON_VERSION, LibPython::PYTHON_VERSION)
const_set(:PYTHON_DESCRIPTION, LibPython::PYTHON_DESCRIPTION)

@init_thread_id = Thread.current.object_id
at_exit do
# Finalize is only safe to call if we're on the same thread that initialized PyCall
finalize if Thread.current.object_id == @init_thread_id
end

true
end

def self.finalize
LibPython::API.Py_FinalizeEx()
end
end
14 changes: 14 additions & 0 deletions pycall_hangs_main_thread.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env ruby

side_thread = Thread.new do
require 'pycall'
PyCall.import_module('sys')
PyCall.import_module('pandas')

PyCall.finalize # if this line is commented out, the process will hang on exit
puts "side thread: exiting"
end
side_thread.join

puts "main thread: exiting"
#=> process exits!
5 changes: 5 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

set -ex

bundle exec ruby -I ext/pycall pycall_hangs_main_thread.rb 2> /dev/null