add workaround for blocking cgi waitpid

Thu, 23 May 2024 12:31:30 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 23 May 2024 12:31:30 +0200
changeset 519
857e5b92828b
parent 518
538a8a22f622
child 520
0b09f0cb6098

add workaround for blocking cgi waitpid

src/server/safs/cgi.c file | annotate | diff | comparison | revisions
--- a/src/server/safs/cgi.c	Wed May 22 20:44:00 2024 +0200
+++ b/src/server/safs/cgi.c	Thu May 23 12:31:30 2024 +0200
@@ -714,10 +714,7 @@
     return REQ_PROCEED;
 }
 
-int cgi_close(CGIProcess *p) {
-    int status = -1;
-    waitpid(p->pid, &status, 0);
-    
+int cgi_close(CGIProcess *p) {  
     if(p->in[0] != -1) {
         system_close(p->in[0]);
     }
@@ -737,6 +734,20 @@
         system_close(p->err[1]);
     }
     
+    // TODO: Because of WNOHANG, waitpid could fail and the process
+    //       is still running. In that case, another waitpid call should
+    //       be done later somewhere.
+    int status = -1;
+    if(waitpid(p->pid, &status, WNOHANG) == 0) {
+        log_ereport(LOG_DEBUG, "cgi_close: waitpid returned 0: pid: %d", (int)p->pid);
+        // cgi process still running
+        // workaround: sleep 1 sec and try again, if that fails again
+        sleep(1);
+        if(waitpid(p->pid, &status, WNOHANG) == 0) {
+            log_ereport(LOG_DEBUG, "cgi_close: waitpid returned 0 again: pid: %d", (int)p->pid);
+        }
+    }
+    
     return status;
 }
 

mercurial