Close device descriptor if an error occurred