title: Repeatedly fork Android app process, where child can access System Services via JNI
body: <p><strong>Context</strong></p>
  <p>I am developing an Android app that embeds a Python interpreter. I use JNI to execute C code and the C-Python API to run Python code and also to make Android functionality available in Python via extensions. The Python interpreter needs to:</p>
  <ul>
  <li>run in its own process</li>
  <li>be able to call itself to start another (many) Python interpreter(s), each in its own process (via Python extension)</li>
  <li>be able to call back into Java and do Android things, e.g. request a System Service to use device sensors (via Python extension)</li>
  </ul>
  <p>Here is a diagram of my current <a href="https://github.com/aaaaalbert/sensibility-testbed/blob/native-sensors-jni/docs/sensibility_overview.png" rel="nofollow noreferrer">implementation</a>.
  It works pretty well as long as I am in the same process. When I try to fork it gets messy.</p>
  <p><strong>Approach 1 - fork in C using fork()</strong></p>
  <p>The app starts a background Service (in the app’s process). The Service calls a native method where I use the fork() system call. The child process inits Python and the Python extensions. The extensions are used to start new Python interpreters; and call into Java code (via JNI) to e.g. use System Services.
  Forking repeatedly (recursively), running Python code, using C-Python extensions and calling back into Java works.</p>
  <p>What does not work is using the Android Application Context from the parent process to use System Services, which leads to segmentation faults. Something that is probably not surprising when one forks and still wants to use stuff from the parent’s address space.</p>
  <p><em>Btw. I get the context from a static function in an Application subclass, where I store it when the app first gets started, which I know is discouraged.</em></p>
  <p><strong>Approach 2 - Let Android fork using android:process</strong></p>
  <p>I let Android fork my background Service by adding android:process to the service definition in AndroidManifest.xml. Then I call a native method that inits Python and the extensions. Now, when I use my extensions to call into Java and use System Services I don’t get segmentation faults. Probably because Android has created a new Virtual Machine, with a fresh context and I don’t have to access anything from the parent process’ address space (especially now that the parent of my new process is Zygote and not my original App process).
  The problem with this approach is that I can’t start multiple instances of a Service/Process to have multiple Python interpreters.</p>
  <h1>TL;DR</h1>
  <ul>
  <li>When I fork a process in an Android native method, how can I avoid segmentation faults when I try to call System Services in that child process?</li>
  <li>Is there a way to have my App create new processes (ideally with a new Virtual Machine), without specifying them in the AndroidManifest.xml</li>
  </ul>
  <p><strong>Resources</strong> (I don't have enough reputations to post the links)</p>
  <ul>
  <li><em>Android Guide - Process and Threads</em> (This somehow tells me, this is do-able, but not how)</li>
  <li><em>Kivy Python for Android</em> (Interpreter does not fork)</li>
  <li><em>Script Layer for Android</em> (Interpreter forks but does not use JNI to call into Java)</li>
  <li><em>Java ProcessBuilder</em> (Forks a new process to execv a native program, which can’t call into Java)</li>
  </ul>