kivy / kivy/python-for-android

Improve process for stopping python

Open
#2,610 0 comments 0 reactions 0 assignees View on GitHub
bug feature-request need-analysis need-discussion
Dominant language
Python
Stars
8.9k
Forks
2k
Avg merge
12h 20m
Merged PRs (30d)
9

Description

### Checklist

- [x] the issue is indeed a bug and not a support request
- [x] issue doesn't already exist: https://github.com/kivy/python-for-android/issues
- [ ] I have a short, runnable example that reproduces the issue
- [x] I reproduced the problem with the latest development version (`p4a.branch = develop`)
- [x] I used the grave accent (aka backticks) to format code or logs when appropriated

### Versions

- Python: 3.9
- OS: Linux
- Kivy: N/A
- Cython: N/A
- OpenJDK: 11

### Description

Right now the way the `Activity` and `Service` classes stop python is to kill the whole process. That's a very heavy handed way to stop the python thread since it prevents the application taking any action when it has to stop.

I think an one way to do this would be to define a custom python exception and raise it in the python interpreter. The application could catch the exception, perform any actions it needs, and then return gracefully. From the Java side, it would run this via the JNI from a different thread than python is running in. In that case, I believe you could use one of [these](https://docs.python.org/3/c-api/init.html#asynchronous-notifications) [two](https://docs.python.org/3/c-api/init.html#gilstate) mechanisms to [raise the exception](https://docs.python.org/3/c-api/exceptions.html#raising-exceptions) in the python main thread.

Contributor guide

Open the contributing guide

Research direction

Start by locating the Activity and Service classes that currently stop Python by killing the process, then read the linked Python C API documentation on asynchronous notifications, GIL state, and raising exceptions. Determine how Java-side JNI calls could request a graceful stop from another thread, and define completion as allowing the application to catch the stop condition and perform cleanup before returning.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, java, python
Domain
mobile
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.