Handle device disconnects and errors, fix possible thread deadlock