fixed an errno handling bug that could lead to an infinite loop