Properly catch command exceptions

Don't hold sync_lock forever if something throws an exception.