Skip to content

Commit a0371f9

Browse files
committed
Ensure Python interpreter finalization after execution
Refactored runPythonProgramInIsolate to always finalize the Python interpreter in a finally block, ensuring proper cleanup and GIL management after script or module execution.
1 parent cc1a237 commit a0371f9

File tree

1 file changed

+41
-31
lines changed

1 file changed

+41
-31
lines changed

src/serious_python_android/lib/src/cpython.dart

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -96,42 +96,52 @@ Future<String> runPythonProgramInIsolate(List<Object> arguments) async {
9696
_debug("after Py_Initialize()");
9797
}
9898

99-
final result = _withGIL(cpython, () {
100-
final logcatSetupError = _setupLogcatForwarding(cpython);
101-
if (logcatSetupError != null) {
102-
return logcatSetupError;
103-
}
104-
105-
if (script != "") {
106-
// run script
107-
_debug("Running script: $script");
108-
final scriptPtr = script.toNativeUtf8();
109-
int sr = cpython.PyRun_SimpleString(scriptPtr.cast<Char>());
110-
_debug("PyRun_SimpleString for script result: $sr");
111-
malloc.free(scriptPtr);
112-
if (sr != 0) {
113-
return getPythonError(cpython);
99+
String result = "";
100+
try {
101+
result = _withGIL(cpython, () {
102+
final logcatSetupError = _setupLogcatForwarding(cpython);
103+
if (logcatSetupError != null) {
104+
return logcatSetupError;
114105
}
115-
} else {
116-
// run program
117-
_debug("Running program module: $programModuleName");
118-
final moduleNamePtr = programModuleName.toNativeUtf8();
119-
var modulePtr = cpython.PyImport_ImportModule(moduleNamePtr.cast<Char>());
120-
if (modulePtr == nullptr) {
121-
final error = getPythonError(cpython);
106+
107+
if (script != "") {
108+
// run script
109+
_debug("Running script: $script");
110+
final scriptPtr = script.toNativeUtf8();
111+
int sr = cpython.PyRun_SimpleString(scriptPtr.cast<Char>());
112+
_debug("PyRun_SimpleString for script result: $sr");
113+
malloc.free(scriptPtr);
114+
if (sr != 0) {
115+
return getPythonError(cpython);
116+
}
117+
} else {
118+
// run program
119+
_debug("Running program module: $programModuleName");
120+
final moduleNamePtr = programModuleName.toNativeUtf8();
121+
var modulePtr =
122+
cpython.PyImport_ImportModule(moduleNamePtr.cast<Char>());
123+
if (modulePtr == nullptr) {
124+
final error = getPythonError(cpython);
125+
malloc.free(moduleNamePtr);
126+
return error;
127+
}
122128
malloc.free(moduleNamePtr);
123-
return error;
124129
}
125-
malloc.free(moduleNamePtr);
126-
}
127130

128-
return "";
129-
});
131+
_debug("Python program finished");
130132

131-
_debug("Python program finished");
132-
133-
// cpython.Py_FinalizeEx();
134-
// _debug("after Py_FinalizeEx()");
133+
return "";
134+
});
135+
} finally {
136+
// Always finalize interpreter so the next run starts clean and can obtain the GIL.
137+
_withGIL(cpython, () {
138+
if (cpython.Py_IsInitialized() != 0) {
139+
cpython.Py_FinalizeEx();
140+
_debug("after Py_FinalizeEx()");
141+
}
142+
});
143+
_cpython = null;
144+
}
135145

136146
sendPort.send(result);
137147

0 commit comments

Comments
 (0)