Finally made the exception changs in the flask app, removed the locking to see if it prevents the deadlocks.