Skip to content

Commit f7da474

Browse files
committed
Prevent cffi from unloading Go shared library
1 parent 3bbe911 commit f7da474

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

‎.github/workflows/ci.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ jobs:
109109
runs-on: ${{ matrix.platform }}
110110
env:
111111
GOPY_BACKEND: cffi
112+
# print the python stack if the process crashes, e.g. at exit
113+
PYTHONFAULTHANDLER: 1
112114
steps:
113115
- name: Checkout code
114116
uses: actions/checkout@v4

‎bind/cffi_build.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,20 @@ def wrapper(name, ret, params, exported):
116116
MODULE_HEAD = '''# python bindings for package @NAME@ using cffi.
117117
# File is generated by gopy version @VERSION@. Do not edit.
118118
import builtins
119+
import ctypes
119120
import os
120121
from operator import index as _index
121122
122123
import cffi
123124
124125
_ffi = cffi.FFI()
125126
_ffi.cdef(@CDEFS@)
126-
_lib = _ffi.dlopen(os.path.join(os.path.dirname(os.path.abspath(__file__)), "@NAME@_go@LIBEXT@"))
127+
_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "@NAME@_go@LIBEXT@")
128+
_lib = _ffi.dlopen(_path)
129+
# Go cannot unload its runtime. cffi unloads _lib at interpreter shutdown but
130+
# ctypes never unloads, so this second handle keeps the library loaded until
131+
# the process exits, as for an extension module.
132+
_pin = ctypes.CDLL(_path)
127133
128134
129135
def _enc(s, argn):

0 commit comments

Comments
 (0)