Looking at this in more detail, I feel like r is being conflated in its purpose. If we look at line 675, we check to see if r is negative one and set it to errno if that's the case. However, functions like uv__cloexec and uv__close return -errno. In this case it's conceivable that any of these operations could legitimately return -1 (based on whatever errno 1 is on a given system), and then we'll do the wrong thing, especially as we've just clobbered errno up above.

Also uv__close() can return an error. Is there some reason you're not checking it?