Fix threading issues with the flask server.