title: Closing Flask-SocketIO gracefully in python
body: <p>I'm using flask-socketio for server side in python. When running on windows10 the <strong>.stop()</strong> function of the flask_socketio.SocketIO works and closes the socket which terminates my script but on <strong>Unubtu</strong> it thrown exception. I want it to be closed properly.
  My code:</p>
  
  <pre><code>import time
  import threading
  import flask
  import flask_socketio
  
  def start_server( vars ):
      vars[ "app" ] = flask.Flask(__name__)
      vars[ "app" ].config['SECRET_KEY'] = 'secret!'
      vars[ "socketio" ] = flask_socketio.SocketIO( vars[ "app" ] )
  
      vars[ "thread" ] = threading.Thread( target= vars[ "socketio" ].run , kwargs={
          "app": vars[ "app" ],
          "host": '127.0.0.1',
          "port": 5000,
      })
      vars["thread"].start()
  
      return vars
  
  vars = {}
  
  print( "flask version: {}".format( flask.__version__ ) )
  print( "flask_socketio version: {}".format( flask_socketio.__version__ ) )
  
  start_server( vars )
  
  time.sleep( 20 )
  
  print( "Stopps" )
  vars[ "socketio" ].stop()
  print( "Done" )
  </code></pre>
  
  <p>I get the following output:</p>
  
  <pre><code>flask version: 1.0.2
  flask_socketio version: 3.0.1
  WebSocket transport not available. Install eventlet or gevent and gevent-websocket for improved performance.
   * Serving Flask app "socket_closer" (lazy loading)
   * Environment: production
     WARNING: Do not use the development server in a production environment.
     Use a production WSGI server instead.
   * Debug mode: off
   * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
  Stopps
  Traceback (most recent call last):
    File "socket_closer.py", line 30, in &lt;module&gt;
      vars[ "socketio" ].stop()
    File "/home/administrator/miniconda3/envs/main/lib/python3.6/site-packages/flask_socketio/__init__.py", line 564, in stop
      func = flask.request.environ.get('werkzeug.server.shutdown')
    File "/home/administrator/miniconda3/envs/main/lib/python3.6/site-packages/werkzeug/local.py", line 347, in __getattr__
      return getattr(self._get_current_object(), name)
    File "/home/administrator/miniconda3/envs/main/lib/python3.6/site-packages/werkzeug/local.py", line 306, in _get_current_object
      return self.__local()
    File "/home/administrator/miniconda3/envs/main/lib/python3.6/site-packages/flask/globals.py", line 37, in _lookup_req_object
      raise RuntimeError(_request_ctx_err_msg)
  RuntimeError: Working outside of request context.
  
  This typically means that you attempted to use functionality that needed
  an active HTTP request.  Consult the documentation on testing for
  information about how to avoid this problem.
  </code></pre>