Skip to content

Commit 919926d

Browse files
committed
Update interpreter finalization logic for Android
Refactored _finalizeInterpreter to remove unnecessary GIL release after Py_FinalizeEx. Interpreter is no longer finalized in runPythonProgramInIsolate to prevent crashes when re-initializing extension modules on Android.
1 parent 0fdfe40 commit 919926d

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

src/serious_python_android/lib/src/cpython.dart

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,11 @@ void _finalizeInterpreter(CPython cpython) {
6161
if (cpython.Py_IsInitialized() == 0) {
6262
return;
6363
}
64+
// Acquire the GIL but avoid releasing it after finalize; Py_FinalizeEx will
65+
// tear down the current thread state.
6466
cpython.PyGILState_Ensure();
65-
try {
66-
cpython.Py_FinalizeEx();
67-
_debug("after Py_FinalizeEx()");
68-
} finally {
69-
// Do NOT call PyGILState_Release after finalize; the thread state is gone.
70-
}
67+
cpython.Py_FinalizeEx();
68+
_debug("after Py_FinalizeEx()");
7169
}
7270

7371
Future<String> runPythonProgramFFI(bool sync, String dynamicLibPath,
@@ -148,9 +146,8 @@ Future<String> runPythonProgramInIsolate(List<Object> arguments) async {
148146
return "";
149147
});
150148
} finally {
151-
// Always finalize interpreter so the next run starts clean and can obtain the GIL.
152-
_finalizeInterpreter(cpython);
153-
_cpython = null;
149+
// Keep interpreter alive between runs; finalizing caused crashes when
150+
// re-initializing extension modules (e.g. _ctypes) on Android.
154151
}
155152

156153
sendPort.send(result);

0 commit comments

Comments
 (0)