Commit eb60c82
Fixed app restart on Android 10, Python output redirect to logcat (flet-dev#187)
* Make setenv calls awaitable in Python init
Refactored the setenv function to return a Future<void> and updated all calls to setenv to use await. This ensures environment variables are set in the correct order before proceeding.
* Ensure single CPython interpreter per process
Introduces a guard to prevent repeated initialization and finalization of the CPython interpreter, addressing crashes on second launch with CPython 3.12. Also ensures the current native thread is registered with the GIL when running Python code, improving stability when re-entering Python from new Dart isolates or threads.
* Remove 'Warning:' prefix from debug message
Updated the debugPrint statement to remove the 'Warning:' prefix when unable to load libpyjni.so. This change streamlines log output for consistency.
* Revert "Ensure single CPython interpreter per process"
This reverts commit 3f4adc0.
* Refactor debug logging and Python runtime init
Replaces direct debugPrint calls with a private _debug function for consistent logging. Adds a check to only initialize the Python runtime if it is not already active, and removes unconditional Py_Finalize call to avoid finalizing an already active runtime.
* Add logcat forwarding for Python stdout/stderr
Introduces a Python initialization script that redirects stdout and stderr to Android logcat using a custom writer and logging handler. Adds a Dart function to inject this script into the Python interpreter, ensuring Python logs are visible in Android logcat. Also adds debug messages to trace script execution.
* Ensure GIL is held for all Python C API calls
Introduced a _withGIL helper to wrap all calls to the Python C API with GIL acquisition and release. This change improves thread safety and correctness when interacting with the Python interpreter from Dart, especially in isolate contexts.
* Refactor GIL usage in Python execution and error handling
Moved _withGIL calls to wrap larger code blocks instead of individual function calls in runPythonProgramInIsolate and getPythonError. This simplifies the code and reduces redundant GIL management, improving readability and maintainability.
* Add AppLifecycleListener to finalize Python on detach
Replaces flutter/foundation.dart with flutter/widgets.dart and adds an AppLifecycleListener to call Py_FinalizeEx when the app detaches. This ensures proper cleanup of the Python interpreter when the app lifecycle ends.
* Move Py_FinalizeEx call to end of isolate function
Removed AppLifecycleListener-based finalization and now explicitly call cpython.Py_FinalizeEx() at the end of runPythonProgramInIsolate. This ensures Python finalization occurs after the program finishes, improving resource management.
* Comment out Py_FinalizeEx call in runPythonProgramInIsolate
The calls to cpython.Py_FinalizeEx() and its debug log have been commented out in runPythonProgramInIsolate. This may be to prevent issues related to finalizing the Python interpreter in this context.
* 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.
* Add safe interpreter finalization for CPython
Introduced the _finalizeInterpreter function to safely finalize the CPython interpreter without releasing the GIL after Py_FinalizeEx, preventing fatal errors. Updated runPythonProgramInIsolate to use this new function for proper cleanup.
* Fix memory deallocation for native UTF-8 strings
Replaced incorrect use of Py_DecRef with malloc.free for freeing native UTF-8 strings allocated with toNativeUtf8. This ensures proper memory management and prevents potential memory leaks.
* 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.
* Comment out GIL management in _withGIL function
The GIL acquisition and release logic in _withGIL has been commented out, and the function now directly executes the action. Interpreter finalization code in runPythonProgramInIsolate remains commented out, with updated comments explaining the rationale.
* Enable GIL management and interpreter finalization
Uncommented code to properly acquire and release the Python GIL in _withGIL, and to finalize the interpreter after running a Python program in an isolate. This ensures thread safety and clean interpreter state between runs.
* Improve CPython isolate handling and logcat setup
Ensures logcat forwarding is idempotent across Dart isolate restarts and avoids finalizing the CPython interpreter between runs to prevent native crashes. Also refines isolate management to prevent killing isolates that may leave the interpreter in a bad state, and improves error handling for logcat forwarding setup.
* Refactor Python execution to use sub-interpreters
Each Python invocation now runs in a fresh sub-interpreter to reduce cross-run leakage and prevent GIL deadlocks. The logcat initialization script is now idempotent and only runs once per process. The unused _finalizeInterpreter function was removed.
* Add process termination support for Android plugin
Introduces a 'terminate' method to the Android plugin and Dart interface, allowing the app to kill its process for a clean restart. Also disables GIL management in _withGIL due to potential issues with embedded CPython state after Dart isolate restarts.
* Enable GIL management in _withGIL function
Uncommented and restored the logic to acquire and release the Python GIL in the _withGIL function, ensuring thread safety when interacting with the Python interpreter.
* Refactor CPython FFI execution and error handling
Simplifies the CPython FFI execution flow by removing sub-interpreter usage and the custom GIL management helper. Updates error handling, logging, and resource management to use debugPrint and proper finalization. Also improves asynchronous execution and logcat forwarding setup.
* Replace debugPrint with spDebug and add log utility
Introduced a new spDebug function in src/log.dart to standardize debug logging with a '[serious_python]' prefix. Updated all debugPrint calls in serious_python_android.dart and cpython.dart to use spDebug for consistent log output.
* Add debug logs for CPython loading and initialization
Added debug statements to log when CPython is loaded and when Python is already initialized, aiding in troubleshooting and execution flow visibility.
* Add debug print for missing asset hash file
Adds a debugPrint statement to log when the asset hash file is not found during asset extraction, aiding in debugging missing hash issues.
* Add debug prints for asset hash extraction and writing
Added debugPrint statements to log asset hash extraction, hash file writing, and conditional hash file operations in extractAssetOrFile. This improves traceability and debugging of asset handling.
* Refactor asset hash reading in extractAssetOrFile
Moved asset hash reading outside of the checkHash block to ensure it is always attempted. Removed redundant debug prints and simplified hash file writing logic.
* Remove terminate method from AndroidPlugin
Eliminated the handling of the 'terminate' method call, including the delayed process termination logic, from the AndroidPlugin class.
* Remove unused imports from AndroidPlugin.java
Deleted unused imports for Handler, Looper, and Process to clean up the code and improve maintainability.
* Remove terminate method from SeriousPythonAndroid
The terminate() override and its related comments were removed from SeriousPythonAndroid. This simplifies the class and removes the process termination logic previously handled via methodChannel.
* Bump version to 0.9.7 and update changelogs
Release 0.9.7 for all serious_python packages. This update fixes app restart on Android 10 and redirects Python output to logcat. All relevant pubspec, build, and podspec files have been updated to reflect the new version.1 parent 927624c commit eb60c82
File tree
18 files changed
+131
-25
lines changed- src
- serious_python_android
- android
- lib
- src
- serious_python_darwin
- darwin
- serious_python_linux
- serious_python_platform_interface
- lib/src
- serious_python_windows
- serious_python
18 files changed
+131
-25
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
1 | 6 | | |
2 | 7 | | |
3 | 8 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
| 5 | + | |
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
1 | 6 | | |
2 | 7 | | |
3 | 8 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
| 2 | + | |
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| |||
Lines changed: 5 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
| |||
43 | 44 | | |
44 | 45 | | |
45 | 46 | | |
46 | | - | |
| 47 | + | |
47 | 48 | | |
48 | 49 | | |
49 | 50 | | |
50 | 51 | | |
51 | 52 | | |
52 | | - | |
| 53 | + | |
53 | 54 | | |
54 | 55 | | |
55 | 56 | | |
| |||
59 | 60 | | |
60 | 61 | | |
61 | 62 | | |
62 | | - | |
| 63 | + | |
63 | 64 | | |
64 | 65 | | |
65 | 66 | | |
| |||
73 | 74 | | |
74 | 75 | | |
75 | 76 | | |
76 | | - | |
| 77 | + | |
77 | 78 | | |
78 | 79 | | |
79 | 80 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
7 | 6 | | |
8 | 7 | | |
9 | 8 | | |
| 9 | + | |
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
14 | 46 | | |
15 | 47 | | |
16 | 48 | | |
| |||
46 | 78 | | |
47 | 79 | | |
48 | 80 | | |
49 | | - | |
50 | | - | |
51 | | - | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
52 | 84 | | |
53 | 85 | | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
54 | 93 | | |
55 | | - | |
| 94 | + | |
56 | 95 | | |
57 | 96 | | |
58 | 97 | | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
59 | 105 | | |
60 | 106 | | |
61 | 107 | | |
62 | 108 | | |
63 | | - | |
| 109 | + | |
64 | 110 | | |
65 | 111 | | |
66 | 112 | | |
| |||
76 | 122 | | |
77 | 123 | | |
78 | 124 | | |
79 | | - | |
| 125 | + | |
80 | 126 | | |
81 | 127 | | |
82 | 128 | | |
| |||
94 | 140 | | |
95 | 141 | | |
96 | 142 | | |
97 | | - | |
| 143 | + | |
98 | 144 | | |
99 | 145 | | |
100 | 146 | | |
| |||
127 | 173 | | |
128 | 174 | | |
129 | 175 | | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
| 5 | + | |
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
1 | 6 | | |
2 | 7 | | |
3 | 8 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
| 7 | + | |
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| |||
0 commit comments