Fix the fd leak bug.

Also take care when using "for .. in fd:" when the fd is a pipe, because for loop will use read() to read the fd, which will not return until EOF is meet.