diff --git a/mongoose.c b/mongoose.c index 94c8050e..250ddc31 100644 --- a/mongoose.c +++ b/mongoose.c @@ -3110,7 +3110,15 @@ static void handle_cgi_request(struct mg_connection *conn, const char *prog) { send_http_error(conn, 500, http_500_error, "Cannot spawn CGI process [%s]: %s", prog, strerror(ERRNO)); goto done; - } else if ((in = fdopen(fd_stdin[1], "wb")) == NULL || + } + + // spawn_process() must close those! + // If we don't mark them as closed, close() attempt before + // return from this function throws an exception on Windows. + // Windows does not like when closed descriptor is closed again. + fd_stdin[0] = fd_stdout[1] = -1; + + if ((in = fdopen(fd_stdin[1], "wb")) == NULL || (out = fdopen(fd_stdout[0], "rb")) == NULL) { send_http_error(conn, 500, http_500_error, "fopen: %s", strerror(ERRNO)); @@ -3120,12 +3128,6 @@ static void handle_cgi_request(struct mg_connection *conn, const char *prog) { setbuf(in, NULL); setbuf(out, NULL); - // spawn_process() must close those! - // If we don't mark them as closed, close() attempt before - // return from this function throws an exception on Windows. - // Windows does not like when closed descriptor is closed again. - fd_stdin[0] = fd_stdout[1] = -1; - // Send POST data to the CGI process if needed if (!strcmp(conn->request_info.request_method, "POST") && !forward_body_data(conn, in, INVALID_SOCKET, NULL)) {