I have more than once found that on some errors the device is not disconnected, even though the cleanup function ought to do this. So check: 1) Is it indeed the case that the cleanup function isn't running in these cases? 2) Really do we need the try/catch statements? Maybe there is a less nasty way of achieving the same effect. 3) Is there a way of liberating a blocked device? Perhaps a MAX reset? If so, document it.