--- parser3/src/targets/cgi/parser3.C 2026/10/27 23:05:00 1.387 +++ parser3/src/targets/cgi/parser3.C 2026/11/21 01:34:01 1.393 @@ -5,7 +5,7 @@ Authors: Konstantin Morshnev , Alexandr Petrosian */ -volatile const char * IDENT_PARSER3_C="$Id: parser3.C,v 1.387 2026/10/27 23:05:00 moko Exp $"; +volatile const char * IDENT_PARSER3_C="$Id: parser3.C,v 1.393 2026/11/21 01:34:01 moko Exp $"; #include "pa_config_includes.h" @@ -20,6 +20,7 @@ volatile const char * IDENT_PARSER3_C="$ #include "pa_code_cache.h" #include "pa_websocket.h" #include "pa_os.h" +#include "pa_exec.h" #ifdef _MSC_VER #include @@ -116,7 +117,7 @@ static void pa_log(const char* stamp, co size+=pa_snprintf(line+size, sizeof(line)-size, " [no request info]\n"); const char* slog=parser3_log_filespec(); - int fd=strcmp(slog,"-") ? pa_open(slog, O_WRONLY | O_APPEND | O_CREAT | O_CLOEXEC, 0666) : -1; + int fd=strcmp(slog,"-") ? open(slog, O_WRONLY | O_APPEND | O_CREAT | O_CLOEXEC, 0666) : -1; // not pa_open, under Windows it allocates and uses pa_thread_request() if(fd<0) fd=STDERR_FILENO; // fallback to stderr PA_UNUSED int ignore_result=write(fd, line, (unsigned int)size); @@ -286,16 +287,6 @@ static const char* maybe_reconstruct_IIS #endif -class RequestController { -public: - RequestController(Request* r){ - request=r; - } - ~RequestController(){ - request=0; - } -}; - #define RESPONSE_TIMEOUT_GRACE 1 // seconds to finish after the interruption, then the process is killed /// $response:timeout: interrupts the request that is not finished in time, the object is created before the request and destroyed after it @@ -331,7 +322,7 @@ public: private: bool kill_process; - volatile sig_atomic_t expired; + volatile int expired; bool armed; pa_timer timer; int seconds; @@ -342,6 +333,11 @@ private: if(!timeout) return; +#ifndef _MSC_VER + if(pa_exec_active_pid) + kill(-pa_exec_active_pid, SIGKILL); // kill the whole process group so nothing is left orphaned +#endif + if(timeout->expired && timeout->kill_process){ // the request is still running after the grace period pa_log_at(timeout->stamp, "response timeout (%d s) exceeded, process killed", timeout->seconds); @@ -352,8 +348,8 @@ private: } timeout->expired=1; - if(request) - request->set_skip(Request::SKIP_INTERRUPTED); + if(thread_request) + thread_request->interrupt(); } }; @@ -426,7 +422,6 @@ static void connection_handler(SAPI_Info // prepare to process request Request r(info, request_info, String::Language(String::L_HTML|String::L_OPTIMIZE_BIT)); - RequestController rc(&r); WebSocket websocket(connection); r.websocket=&websocket; // process the request @@ -473,6 +468,10 @@ static void reject_connection(HTTPD_Conn SAPI_Info_HTTPD info(connection); SAPI::send_error(info, "too many connections", "503"); + + // closing the socket with unread request resets the connection and the response can be lost: non-blocking reading for what has arrived + char buf[4096]; + for(int i=0; i<16 && pa_sock_ready(connection.sock, false, 0)>0 && connection.receive(buf, sizeof(buf)); i++); } static void httpd_mode() { @@ -636,11 +635,8 @@ static void real_parser_handler(bool cgi // prepare to process request Request r(*sapi_info, request_info, cgi ? String::Language(String::L_HTML|String::L_OPTIMIZE_BIT) : String::L_AS_IS); - // initing ::request ptr for signal handlers - RequestController rc(&r); // process the request r.core(locate_config(config_filespec, parser3_filespec)); - // clearing ::request in rc destructor to prevent signal handlers from accessing invalid memory // then the request is destroyed, before the libraries are unloaded: its pool cleanups call them (memcached) }