--- parser3/src/targets/cgi/parser3.C 2026/10/26 22:41:38 1.385 +++ parser3/src/targets/cgi/parser3.C 2026/11/21 01:27:01 1.392 @@ -5,7 +5,7 @@ Authors: Konstantin Morshnev , Alexandr Petrosian */ -volatile const char * IDENT_PARSER3_C="$Id: parser3.C,v 1.385 2026/10/26 22:41:38 moko Exp $"; +volatile const char * IDENT_PARSER3_C="$Id: parser3.C,v 1.392 2026/11/21 01:27: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, it allocates under Windows 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; @@ -211,23 +210,9 @@ static const char* full_disk_path(const return pa_strcat(backslashes_to_slashes(getcwd(cwd, MAX_STRING) ? cwd : ""), "/", path); } -static void log_signal(const char* signal_name) { - pa_log("%s received %s processing request", signal_name, request ? "while" : "before or after"); -} - #ifdef SIGPIPE -#define SIGPIPE_NAME "SIGPIPE" -static const String sigpipe_name(SIGPIPE_NAME); -static void SIGPIPE_handler(int /*sig*/){ - Value* sigpipe=0; - if(request) - sigpipe=request->main_class.get_element(sigpipe_name); - if(sigpipe && sigpipe->as_bool()) - log_signal(SIGPIPE_NAME); - - if(request) - request->set_skip(Request::SKIP_INTERRUPTED); -} +// write to a closed socket or pipe fails with EPIPE. Not SIG_IGN, as the ignored signal is inherited by the executed programs +static void SIGPIPE_handler(int /*sig*/){} #endif // requires pa_thread_request() in entry_exists() under Windows @@ -302,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){ @@ -369,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); @@ -417,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() { @@ -424,10 +479,6 @@ static void httpd_mode() { SOCKET sock = HTTPD_Server::bind(httpd_host_port); -#ifdef SIGPIPE - signal(SIGPIPE, SIG_IGN); -#endif - while(1){ #ifndef _MSC_VER pid_t pid=1; @@ -579,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) }