Revert r1377398 (bug fix for PR 53693), which results
in more processes being used.  This needs more careful
analysis and modification before release.

Also revert r1397778 (PR 54000) and r1527358, which addressed issues
with r1377398.


git-svn-id: https://svn.apache.org/repos/asf/httpd/mod_fcgid/trunk@1529061 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/CHANGES-FCGID b/CHANGES-FCGID
index 4801c7c..c1a3ee1 100644
--- a/CHANGES-FCGID
+++ b/CHANGES-FCGID
@@ -1,6 +1,7 @@
                                                          -*- coding: utf-8 -*-
 Changes with mod_fcgid 2.3.9
 
+  *) Revert fix for PR 53693 (added in 2.3.8 but undocumented).
 
 Changes with mod_fcgid 2.3.8
 
diff --git a/modules/fcgid/fcgid_bridge.c b/modules/fcgid/fcgid_bridge.c
index 6091479..c8b45c2 100644
--- a/modules/fcgid/fcgid_bridge.c
+++ b/modules/fcgid/fcgid_bridge.c
@@ -124,6 +124,35 @@
     proctable_unlock(r);
 }
 
+static int count_busy_processes(request_rec *r, fcgid_command *command)
+{
+    int result = 0;
+    fcgid_procnode *previous_node, *current_node, *next_node;
+    fcgid_procnode *proc_table = proctable_get_table_array();
+    fcgid_procnode *busy_list_header = proctable_get_busy_list();
+
+    proctable_lock(r);
+
+    previous_node = busy_list_header;
+    current_node = &proc_table[previous_node->next_index];
+    while (current_node != proc_table) {
+        if (current_node->inode == command->inode
+            && current_node->deviceid == command->deviceid
+            && !strcmp(current_node->cmdline, command->cmdline)
+            && current_node->vhost_id == command->vhost_id
+            && current_node->uid == command->uid
+            && current_node->gid == command->gid) {
+            result++;
+        }
+        next_node = &proc_table[current_node->next_index];
+        current_node = next_node;
+    }
+
+    proctable_unlock(r);
+
+    return result;
+}
+
 apr_status_t bucket_ctx_cleanup(void *thectx)
 {
     /* Cleanup jobs:
@@ -416,19 +445,19 @@
             if (bucket_ctx->procnode)
                 break;
 
-            /* Send a spawn request if I can't get a process slot */
-            /* procmgr_send_spawn_cmd() return APR_SUCCESS if a process is created */
-            if( procmgr_send_spawn_cmd(&fcgi_request, r)==APR_SUCCESS ) {
-                bucket_ctx->procnode = apply_free_procnode(r, &fcgi_request);
-                if (bucket_ctx->procnode)
-                    break;
-            }
-            else {
+            /* Avoid sleeping the very first time through if there are no
+               busy processes; the problem is just that we haven't spawned
+               anything yet, so waiting is pointless */
+            if (i > 0 || j > 0 || count_busy_processes(r, &fcgi_request)) {
                 apr_sleep(apr_time_from_sec(1));
+
                 bucket_ctx->procnode = apply_free_procnode(r, &fcgi_request);
                 if (bucket_ctx->procnode)
                     break;
             }
+
+            /* Send a spawn request if I can't get a process slot */
+            procmgr_send_spawn_cmd(&fcgi_request, r);
         }
 
         /* Connect to the fastcgi server */
diff --git a/modules/fcgid/fcgid_pm.h b/modules/fcgid/fcgid_pm.h
index 697c1a1..b815a39 100644
--- a/modules/fcgid/fcgid_pm.h
+++ b/modules/fcgid/fcgid_pm.h
@@ -45,10 +45,7 @@
                                     request_rec * r);
 apr_status_t procmgr_fetch_cmd(fcgid_command * command,
                               server_rec * main_server);
-
-#define PROCMGR_PROC_CREATED 0
-#define PROCMGR_PROC_NOT_CREATED 1
-apr_status_t procmgr_finish_notify(server_rec * main_server, char proc_created);
+apr_status_t procmgr_finish_notify(server_rec * main_server);
 
 apr_status_t procmgr_child_init(server_rec * main_server,
                                 apr_pool_t * pchild);
diff --git a/modules/fcgid/fcgid_pm_main.c b/modules/fcgid/fcgid_pm_main.c
index 7a04fd8..74e1888 100644
--- a/modules/fcgid/fcgid_pm_main.c
+++ b/modules/fcgid/fcgid_pm_main.c
@@ -527,7 +527,7 @@
 
 /* End of common to util_script.c */
 
-static apr_status_t
+static void
 fastcgi_spawn(fcgid_command * command, server_rec * main_server,
               apr_pool_t * configpool)
 {
@@ -547,7 +547,7 @@
         proctable_pm_unlock(main_server);
         ap_log_error(APLOG_MARK, APLOG_WARNING, 0, main_server,
                      "mod_fcgid: too much processes, please increase FCGID_MAX_APPLICATION");
-        return APR_CHILD_NOTDONE;
+        return;
     }
     procnode = &proctable_array[free_list_header->next_index];
     free_list_header->next_index = procnode->next_index;
@@ -589,7 +589,7 @@
 
         ap_log_error(APLOG_MARK, APLOG_WARNING, rv, main_server,
                      "mod_fcgid: can't create pool for process");
-        return APR_CHILD_NOTDONE;
+        return;
     }
     /* Set up longer, system defaults before falling into parsing fixed-limit
      * request-by-request variables, so if any are overriden, they preempt
@@ -614,7 +614,7 @@
         apr_pool_destroy(procnode->proc_pool);
         link_node_to_list(main_server, free_list_header,
                           procnode, proctable_array);
-        return APR_CHILD_NOTDONE;
+        return;
     }
     else {
         /* The job done */
@@ -628,7 +628,6 @@
                      procnode->proc_id.pid);
         register_spawn(main_server, procnode);
     }
-    return APR_SUCCESS;
 }
 
 apr_status_t pm_main(server_rec * main_server, apr_pool_t * configpool)
@@ -636,17 +635,15 @@
     fcgid_command command;
 
     while (1) {
-        char proc_created = PROCMGR_PROC_NOT_CREATED;
         if (procmgr_must_exit())
             break;
 
         /* Wait for command */
         if (procmgr_fetch_cmd(&command, main_server) == APR_SUCCESS) {
             if (is_spawn_allowed(main_server, &command))
-                if( fastcgi_spawn(&command, main_server, configpool)==APR_SUCCESS )
-                    proc_created = PROCMGR_PROC_CREATED;
+                fastcgi_spawn(&command, main_server, configpool);
 
-            procmgr_finish_notify(main_server, proc_created);
+            procmgr_finish_notify(main_server);
         }
 
         /* Move matched node to error list */
diff --git a/modules/fcgid/fcgid_pm_unix.c b/modules/fcgid/fcgid_pm_unix.c
index 3e4975d..500d289 100644
--- a/modules/fcgid/fcgid_pm_unix.c
+++ b/modules/fcgid/fcgid_pm_unix.c
@@ -461,10 +461,9 @@
 apr_status_t procmgr_send_spawn_cmd(fcgid_command * command,
                                     request_rec * r)
 {
-    apr_status_t rv,result;
+    apr_status_t rv;
     char notifybyte;
     apr_size_t nbytes = sizeof(*command);
-    result = APR_SUCCESS;
 
     /* Get the global mutex before posting the request */
     if ((rv = apr_global_mutex_lock(g_pipelock)) != APR_SUCCESS) {
@@ -479,7 +478,6 @@
         /* Just print some error log and fall through */
         ap_log_rerror(APLOG_MARK, APLOG_WARNING, rv, r,
                       "mod_fcgid: can't write spawn command");
-        result = rv;
     } else {
         /* Wait the finish notify while send the request successfully */
         nbytes = sizeof(notifybyte);
@@ -488,9 +486,7 @@
                            &nbytes)) != APR_SUCCESS) {
             ap_log_rerror(APLOG_MARK, APLOG_WARNING, rv, r,
                           "mod_fcgid: can't get notify from process manager");
-            result = rv;
-        } else if( notifybyte!=PROCMGR_PROC_CREATED )
-            result = APR_CHILD_NOTDONE;
+        }
     }
 
     /* Release the lock */
@@ -500,16 +496,17 @@
         exit(0);
     }
 
-    return result;
+    return APR_SUCCESS;
 }
 
-apr_status_t procmgr_finish_notify(server_rec * main_server, char proc_created)
+apr_status_t procmgr_finish_notify(server_rec * main_server)
 {
     apr_status_t rv;
-    apr_size_t nbytes = sizeof(proc_created);
+    char notifybyte = 'p';
+    apr_size_t nbytes = sizeof(notifybyte);
 
     if ((rv =
-         apr_file_write(g_pm_write_pipe, &proc_created,
+         apr_file_write(g_pm_write_pipe, &notifybyte,
                         &nbytes)) != APR_SUCCESS) {
         ap_log_error(APLOG_MARK, APLOG_WARNING, rv, main_server,
                      "mod_fcgid: can't send notify from process manager");
diff --git a/modules/fcgid/fcgid_pm_win.c b/modules/fcgid/fcgid_pm_win.c
index 8287bfc..c983d40 100644
--- a/modules/fcgid/fcgid_pm_win.c
+++ b/modules/fcgid/fcgid_pm_win.c
@@ -160,7 +160,6 @@
 apr_status_t procmgr_send_spawn_cmd(fcgid_command * command,
                                     request_rec * r)
 {
-     char *notifybyte = NULL;
     if (g_thread && g_msgqueue && !g_must_exit
         && g_reqlock && g_notifyqueue) {
         apr_status_t rv;
@@ -191,6 +190,8 @@
             return rv;
         } else {
             /* Wait the respond from process manager */
+            char *notifybyte = NULL;
+
             if ((rv =
                  apr_queue_pop(g_notifyqueue,
                                (void **)&notifybyte)) != APR_SUCCESS) {
@@ -210,22 +211,13 @@
         }
     }
 
-    if( notifybyte && *notifybyte==PROCMGR_PROC_CREATED )
-        return APR_SUCCESS;
-    else
-        return APR_CHILD_NOTDONE;
+    return APR_SUCCESS;
 }
 
-static char g_proc_created = PROCMGR_PROC_CREATED;
-static char g_proc_not_created = PROCMGR_PROC_NOT_CREATED;
-apr_status_t procmgr_finish_notify(server_rec * main_server, char proc_created)
+apr_status_t procmgr_finish_notify(server_rec * main_server)
 {
     apr_status_t rv;
     char *notify = NULL;
-    if( proc_created==PROCMGR_PROC_CREATED )
-        notify = &g_proc_created;
-    else
-        notify = &g_proc_not_created;
 
     if ((rv = apr_queue_push(g_notifyqueue, notify)) != APR_SUCCESS) {
         ap_log_error(APLOG_MARK, APLOG_EMERG, rv, main_server,