Repository: CTFd/CTFd

Issue:
CTFd crash right after install
## Git + python 3.7.2 (3b1b82b9a0fbcb8731d7a3a3bbac99499c466c99)

[![asciicast](https://asciinema.org/a/iHDMVNRWSYJDTZUgIy4wIBsOK.svg)](https://asciinema.org/a/iHDMVNRWSYJDTZUgIy4wIBsOK)

## Git + python 2.7.15 (3b1b82b9a0fbcb8731d7a3a3bbac99499c466c99)

[![asciicast](https://asciinema.org/a/JRqfe0rMl0QeZAEklyvV1LQX9.svg)](https://asciinema.org/a/JRqfe0rMl0QeZAEklyvV1LQX9)

## 2.1.4 + python 3.7.2

[![asciicast](https://asciinema.org/a/wErebw8ZN2HFER9P71tLW4FBv.svg)](https://asciinema.org/a/wErebw8ZN2HFER9P71tLW4FBv)

**Environment**:

  - CTFd Version/Commit: see titles
  - Operating System: ArchLinux
  - Web Browser and Version: Firefox

**What happened?**

CTFd crash right after install.

**What did you expect to happen?**

Working or at least a more comprehensible error.

**How to reproduce your issue**

See the asciinema, but basically

- Download CTFd
- Install dependencies
- run flask
- crash

PS : with the untouched default config

**Any associated stack traces or error logs**

See the asciinema


Discussion:
In fact this is the just the README that is wrong

![image](https://user-images.githubusercontent.com/16578570/64371584-339df980-d021-11e9-8e2e-928ef7c31a5d.png)

One must not do `flask run` but rather as said on the wiki (https://github.com/CTFd/CTFd/wiki) `python serve.py`.

```
$ python serve.py
 * Loaded module, <module 'CTFd.plugins.challenges' from '/tmp/CTFd-2.1.4/CTFd/plugins/challenges/__init__.py'>
 * Loaded module, <module 'CTFd.plugins.dynamic_challenges' from '/tmp/CTFd-2.1.4/CTFd/plugins/dynamic_challenges/__init__.py'>
 * Loaded module, <module 'CTFd.plugins.flags' from '/tmp/CTFd-2.1.4/CTFd/plugins/flags/__init__.py'>
 * Serving Flask app "CTFd" (lazy loading)
 * Environment: development
 * Debug mode: on
 * Running on http://127.0.0.1:4000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Loaded module, <module 'CTFd.plugins.challenges' from '/tmp/CTFd-2.1.4/CTFd/plugins/challenges/__init__.py'>
 * Loaded module, <module 'CTFd.plugins.dynamic_challenges' from '/tmp/CTFd-2.1.4/CTFd/plugins/dynamic_challenges/__init__.py'>
 * Loaded module, <module 'CTFd.plugins.flags' from '/tmp/CTFd-2.1.4/CTFd/plugins/flags/__init__.py'>
 * Debugger is active!
 * Debugger PIN: 117-149-425
127.0.0.1 - - [05/Sep/2019 21:04:19] "GET / HTTP/1.1" 302 -
127.0.0.1 - - [05/Sep/2019 21:04:19] "GET /setup HTTP/1.1" 200 -
127.0.0.1 - - [05/Sep/2019 21:04:20] "GET /setup HTTP/1.1" 200 -
127.0.0.1 - - [05/Sep/2019 21:04:20] "GET /themes/core/static/css/vendor/bootstrap.min.css HTTP/1.1" 200 -
127.0.0.1 - - [05/Sep/2019 21:04:20] "GET /themes/core/static/css/vendor/font-awesome/fontawesome-fonts.css HTTP/1.1" 200 -
127.0.0.1 - - [05/Sep/2019 21:04:20] "GET /themes/core/static/css/vendor/font-awesome/fontawesome-all.min.css HTTP/1.1" 200 -
127.0.0.1 - - [05/Sep/2019 21:04:20] "GET /themes/core/static/css/vendor/font.css HTTP/1.1" 200 -
127.0.0.1 - - [05/Sep/2019 21:04:20] "GET /themes/core/static/css/jumbotron.css HTTP/1.1" 200 -
127.0.0.1 - - [05/Sep/2019 21:04:20] "GET /themes/core/static/css/sticky-footer.css HTTP/1.1" 200 -
127.0.0.1 - - [05/Sep/2019 21:04:20] "GET /themes/core/static/css/base.css HTTP/1.1" 200 -
127.0.0.1 - - [05/Sep/2019 21:04:20] "GET /static/user.css HTTP/1.1" 302 -
127.0.0.1 - - [05/Sep/2019 21:04:20] "GET /themes/core/static/js/vendor/promise-polyfill.min.js HTTP/1.1" 200 -
127.0.0.1 - - [05/Sep/2019 21:04:20] "GET /themes/core/static/js/vendor/eventsource.min.js HTTP/1.1" 200 -
127.0.0.1 - - [05/Sep/2019 21:04:20] "GET /themes/core/static/js/vendor/fetch.min.js HTTP/1.1" 200 -
127.0.0.1 - - [05/Sep/2019 21:04:20] "GET /themes/core/static/js/CTFd.js HTTP/1.1" 200 -
127.0.0.1 - - [05/Sep/2019 21:04:20] "GET /themes/core/static/js/vendor/nunjucks.min.js HTTP/1.1" 200 -
127.0.0.1 - - [05/Sep/2019 21:04:20] "GET /themes/core/static/js/vendor/moment.min.js HTTP/1.1" 200 -
127.0.0.1 - - [05/Sep/2019 21:04:20] "GET /themes/core/static/js/vendor/jquery.min.js HTTP/1.1" 200 -
127.0.0.1 - - [05/Sep/2019 21:04:20] "GET /themes/core/static/js/vendor/markdown-it.min.js HTTP/1.1" 200 -
127.0.0.1 - - [05/Sep/2019 21:04:20] "GET /themes/core/static/js/vendor/bootstrap.bundle.min.js HTTP/1.1" 200 -
127.0.0.1 - - [05/Sep/2019 21:04:20] "GET /themes/core/static/js/vendor/howler.min.js HTTP/1.1" 200 -
127.0.0.1 - - [05/Sep/2019 21:04:20] "GET /themes/core/static/js/vendor/window-controller.js HTTP/1.1" 200 -
127.0.0.1 - - [05/Sep/2019 21:04:20] "GET /themes/core/static/js/style.js HTTP/1.1" 200 -
127.0.0.1 - - [05/Sep/2019 21:04:20] "GET /themes/core/static/js/ezq.js HTTP/1.1" 200 -
127.0.0.1 - - [05/Sep/2019 21:04:20] "GET /themes/core/static/js/events.js HTTP/1.1" 200 -
127.0.0.1 - - [05/Sep/2019 21:04:20] "GET /themes/core/static/js/utils.js HTTP/1.1" 200 -
127.0.0.1 - - [05/Sep/2019 21:04:20] "GET /setup HTTP/1.1" 200 -
127.0.0.1 - - [05/Sep/2019 21:04:20] "GET /themes/core/static/sounds/notification.webm HTTP/1.1" 200 -
127.0.0.1 - - [05/Sep/2019 21:04:21] "GET /themes/core/static/img/favicon.ico HTTP/1.1" 200 -
127.0.0.1 - - [05/Sep/2019 21:04:21] "GET /events HTTP/1.1" 302 -
127.0.0.1 - - [05/Sep/2019 21:04:21] "GET /setup HTTP/1.1" 200 -
```

Resolving patch:
diff --git a/wsgi.py b/wsgi.py
--- a/wsgi.py
+++ b/wsgi.py
@@ -1,8 +1,14 @@
-from gevent import monkey
-monkey.patch_all()
+import os
+
+# Detect if we're running via `flask run` and don't monkey patch
+if not os.getenv("FLASK_RUN_FROM_CLI"):
+    from gevent import monkey
+
+    monkey.patch_all()
+
 from CTFd import create_app
 
 app = create_app()
 
-if __name__ == '__main__':
+if __name__ == "__main__":
     app.run(debug=True, threaded=True, host="127.0.0.1", port=4000)


Test patch: