Shut down the api cleanly when the api thread is cancelled. This should allow the api socket to be closed successfully to next be reopened with app_restart.