cgi: Fix the possible memleak and close pipes in case of error.