Repository: learningequality/kolibri

Issue:
Unable to stop server on mac
<!--
Instructions:
 * Fill out the sections below, replace …'s with information about your issue
 * Use the 'preview' function above this text box to verify formatting before submitting
-->

### Observed behavior
<!--
Description of the behavior that was observed, including screenshots or other references when applicable
-->

When running on mac, I started to randomly get logging messages
```
INFO     ENGINE Caught signal SIGTERM.
INFO     ENGINE Bus STOPPING
```
Sometimes, the program is able to continue running; sometimes, it isn't. 
But every time after this error shows up, I am not able to stop the server using Ctrl+C.
The program hangs at this step for more than 10 minutes before I use Ctrl+Z to kill it:
```
INFO     ENGINE Waiting for child threads to terminate...
```
This does not happen when I ran Kolibri on Ubuntu both foreground and background.

### Expected behavior
<!--
Description of what behavior was expected but did not occur
-->

Users should be able to stop the server successfully

### User-facing consequences
<!--
Implications and real-world consequences for learners, coaches, admins, and other users of the application
-->

Users will have to kill the program and kill all the corresponding python processes to stop Kolibri

### Errors and logs
<!--
Relevant logs from:
 * the command line
 * ~/.kolibri/logs/kolibri.txt
 * the browser console

Please wrap errors in triple backticks for clean formatting like this:
```
01:10 info: something happened
01:12 error: something bad happened
```
-->

```
INFO     Starting Kolibri 0.13.0b7
INFO     ENGINE Listening for SIGTERM.
INFO     ENGINE Listening for SIGHUP.
INFO     ENGINE Listening for SIGUSR1.
INFO     ENGINE Listening for SIGINT.
INFO     ENGINE Bus STARTING
INFO     Started new JOBCHECKER thread ID#42378ffb12594e50807bde3603407cc5
INFO     Started new JOBCHECKER thread ID#f4471e9694eb47e0873a6b77aedd77e2
INFO     Started new SCHEDULECHECKER thread ID#ed80030c77d949daa0c31b5b87b6b977
INFO     Registering ourselves to zeroconf network with id '74aefcce9d7550b3021615db97b7b484'...
INFO     ENGINE Serving on http://0.0.0.0:8080
INFO     ENGINE Bus STARTED
INFO     Ping succeeded! (response: {'id': 506061, 'messages': []})
INFO     Sqlite database Vacuum finished.
INFO     ENGINE Caught signal SIGTERM.
INFO     ENGINE Bus STOPPING
INFO     ENGINE Caught signal SIGTERM.
INFO     ENGINE Caught signal SIGTERM.
INFO     ENGINE Bus STOPPING
INFO     ENGINE Bus STOPPING
INFO     ENGINE Caught signal SIGTERM.
INFO     ENGINE Caught signal SIGTERM.
INFO     ENGINE Bus STOPPING
INFO     ENGINE Bus STOPPING
INFO     ENGINE Caught signal SIGTERM.
INFO     ENGINE Bus STOPPING
INFO     ENGINE Caught signal SIGTERM.
INFO     ENGINE Bus STOPPING
INFO     ENGINE Caught signal SIGTERM.
INFO     ENGINE Caught signal SIGTERM.
INFO     ENGINE Caught signal SIGTERM.
INFO     ENGINE Bus STOPPING
INFO     ENGINE Bus STOPPING
INFO     ENGINE Bus STOPPING
INFO     ENGINE Caught signal SIGTERM.
INFO     ENGINE Bus STOPPING
^C  <--- trying to stop the server with Ctrl+C here
INFO     ENGINE Caught signal SIGINT.
INFO     ENGINE Bus STOPPING
INFO     ENGINE Caught signal SIGINT.
INFO     ENGINE Bus STOPPING
INFO     ENGINE Caught signal SIGINT.
INFO     ENGINE Caught signal SIGINT.
INFO     ENGINE Caught signal SIGINT.
INFO     ENGINE Bus STOPPING
INFO     ENGINE Bus STOPPING
INFO     ENGINE Caught signal SIGINT.
INFO     ENGINE Caught signal SIGINT.
INFO     ENGINE Caught signal SIGINT.
INFO     ENGINE Caught signal SIGINT.
INFO     ENGINE Bus STOPPING
INFO     ENGINE Bus STOPPING
INFO     ENGINE Bus STOPPING
INFO     ENGINE Caught signal SIGINT.
INFO     ENGINE Bus STOPPING
INFO     ENGINE Bus STOPPING
INFO     ENGINE Bus STOPPING
INFO     ENGINE HTTP Server cherrypy._cpwsgi_server.CPWSGIServer(('0.0.0.0', 8080)) shut down
INFO     
Kolibri instance '74aefcce9d7550b3021615db97b7b484' has left the zeroconf network.

WARNING  JOBCHECKER shut down event received; closing.
WARNING  SCHEDULECHECKER shut down event received; closing.
WARNING  JOBCHECKER shut down event received; closing.
INFO     ENGINE Bus STOPPED
INFO     ENGINE Bus EXITING
INFO     ENGINE Bus EXITED
INFO     ENGINE Waiting for child threads to terminate...
^Z <--- kill the program with Ctrl+Z here after waiting for 10 minutes
```

### Steps to reproduce
<!--
Precise steps that someone else can follow in order to see this behavior
-->

1. on mac, start kolibri
2. after the setup wizard, check the log, if it shows `ENGINE Caught signal SIGTERM.`, try to stop the server
3. if it doesn't show the error message, then import a channel until it shows up.

### Context
<!--
Tell us about your environment, including:
 * Kolibri version
 * Operating system
 * Browser
-->

Kolibri version: 0.13.0b7 (both python2 and 3)
Operating system: MacOSX
Browser: Google Chrome



Discussion:
Hrm - it seems like the async task terminating is triggering the `SIGTERM` handler in the parent process.
@lyw07 this is the beta7 pex?
@indirectlylit no, it's beta7 whl

Resolving patch:
diff --git a/kolibri/utils/server.py b/kolibri/utils/server.py
--- a/kolibri/utils/server.py
+++ b/kolibri/utils/server.py
@@ -282,6 +282,16 @@ def run_server(port, serve_http=True):
     pid_plugin = CleanUpPIDPlugin(cherrypy.engine)
     pid_plugin.subscribe()
 
+    process_pid = os.getpid()
+
+    original_handler = cherrypy.engine.signal_handler._handle_signal
+
+    def handler(signum, frame):
+        if os.getpid() == process_pid:
+            original_handler(signum, frame)
+
+    cherrypy.engine.signal_handler._handle_signal = handler
+
     cherrypy.engine.signal_handler.handlers.update(
         {
             "SIGINT": cherrypy.engine.exit,


Test patch: