--- parser3/src/targets/cgi/parser3.C 2026/10/26 22:52:46 1.386 +++ 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.386 2026/10/26 22:52:46 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" @@ -19,6 +19,8 @@ volatile const char * IDENT_PARSER3_C="$ #include "pa_sapi_info.h" #include "pa_code_cache.h" #include "pa_websocket.h" +#include "pa_os.h" +#include "pa_exec.h" #ifdef _MSC_VER #include @@ -100,21 +102,10 @@ static void pa_log(const char* stamp, co size_t size=0; // prefix -#ifndef _MSC_VER - char stamp_buffer[32]; -#endif - if(!stamp){ - time_t t=time(0); -#ifdef _MSC_VER - stamp=ctime(&t); // the buffer is per thread -#else - stamp=ctime_r(&t, stamp_buffer); // the buffer of ctime() is shared by the threads -#endif - } - if(stamp) { // never saw that - if(size_t len=strlen(stamp)) // saw once stamp being ="" - size=pa_snprintf(line, 64, "[%.*s] [%u] ", (int)len-1 /* exclude \n */, stamp, (unsigned int)pa_get_thread_id() ); - } + if(!stamp) + stamp=pa_ctime(time(0)); + if(size_t len=strlen(stamp)) // saw once stamp being ="" + size=pa_snprintf(line, 64, "[%.*s] [%u] ", (int)len-1 /* exclude \n */, stamp, (unsigned int)pa_get_thread_id() ); // message size+=remove_crlf(line+size, line+size+pa_vsnprintf(line+size, MAX_LOG_STRING, fmt, args)); @@ -126,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); @@ -141,6 +132,14 @@ void pa_log(const char* fmt, ...) { va_end(args); } +// for signal handlers, where pa_ctime can't be called: the stamp is prepared in advance +static void pa_log_at(const char* stamp, const char* fmt, ...) { + va_list args; + va_start(args,fmt); + pa_log(stamp, fmt, args); + va_end(args); +} + // appends to parser3.log located next to the config file if openable, to stderr otherwize void SAPI::log(SAPI_Info&, const char* fmt, ...) { va_list args; @@ -288,16 +287,79 @@ static const char* maybe_reconstruct_IIS #endif -class RequestController { +#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 +class Response_timeout { public: - RequestController(Request* r){ - request=r; - } - ~RequestController(){ - request=0; + static THREAD_LOCAL Response_timeout *current; + + /// @a akill_process: the process will be killed if the request is still running after the grace period + Response_timeout(bool akill_process) : kill_process(akill_process), expired(0), armed(false) { + current=this; + } + ~Response_timeout() { + set(0); + current=0; + } + + /// restarts the timer for the given number of seconds, 0 cancels it + void set(int aseconds) { + if(armed){ + pa_timer_stop(timer); + armed=false; + } + if(!aseconds) + return; + + seconds=aseconds; + expired=0; + if(kill_process) // pa_ctime can't be called from the handler, so preparing for the log in advance + stamp=pa_strdup(pa_ctime(time(0)+seconds+RESPONSE_TIMEOUT_GRACE)); + + armed=pa_timer_start(&timer, seconds, kill_process ? RESPONSE_TIMEOUT_GRACE : 0, handler); + } + +private: + bool kill_process; + volatile int expired; + bool armed; + pa_timer timer; + int seconds; + const char *stamp; + + static void handler(int) { + Response_timeout *timeout=current; + 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); +#ifndef _MSC_VER + kill(getpid(), SIGKILL); +#endif + return; + } + + timeout->expired=1; + if(thread_request) + thread_request->interrupt(); } }; +THREAD_LOCAL Response_timeout *Response_timeout::current=0; + +void SAPI::set_response_timeout(SAPI_Info&, int seconds) { + if(Response_timeout::current) + Response_timeout::current->set(seconds); +} + class RequestInfoController { public: RequestInfoController(Request_info* rinfo, SAPI_Info* sinfo){ @@ -355,6 +417,9 @@ static void connection_handler(SAPI_Info request_info.mail_received = false; request_info.argv = argv_extra; + // process can be killed on timeout only in parallel mode + Response_timeout response_timeout(HTTPD_Server::mode == HTTPD_Server::PARALLEL); + // prepare to process request Request r(info, request_info, String::Language(String::L_HTML|String::L_OPTIMIZE_BIT)); WebSocket websocket(connection); @@ -403,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() { @@ -561,13 +630,13 @@ static void real_parser_handler(bool cgi #endif { + // CGI process can be killed on timeout + Response_timeout response_timeout(true); + // 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 RequestController 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) }