--- mod_wsgi-3.2-original/mod_wsgi.c	2010-05-07 10:16:44.000000000 -0700
+++ mod_wsgi-3.2/mod_wsgi.c	2010-05-07 11:03:55.000000000 -0700
@@ -9030,6 +9030,7 @@
     status = wsgi_execute_remote(r);
 
     if (status != DECLINED)
+        /* success: pass along any normal HTTP status from the daemon */
         return status;
 #endif
 
@@ -9675,6 +9676,11 @@
 
 static void wsgi_signal_handler(int signum)
 {
+    ap_log_error(APLOG_MARK, WSGI_LOG_INFO(0),
+                 NULL,
+                 "mod_wsgi (pid=%d): Signal %d received, probably shutting down this process",
+                 getpid(), signum);
+    
     apr_size_t nbytes = 1;
 
     if (signum == SIGXCPU)
@@ -9691,7 +9697,16 @@
 static void wsgi_manage_process(int reason, void *data, apr_wait_t status)
 {
     WSGIDaemonProcess *daemon = data;
+    
+    ap_log_error(APLOG_MARK, WSGI_LOG_INFO(0),
+                 wsgi_server, "mod_wsgi (pid=%d): "
+                 "wsgi_manage_process(%d, '%s', %d)",
+                 daemon ? daemon->process.pid: -1,
+                 reason,
+                 (daemon && daemon->group) ? daemon->group->name : "(unknown daemon)", status);
 
+    pid_t old_pid = daemon->process.pid;
+    
     switch (reason) {
 
         /* Child daemon process has died. */
@@ -9715,18 +9730,39 @@
 
             stopping = 1;
 
-            if (ap_mpm_query(AP_MPMQ_MPM_STATE, &mpm_state) == APR_SUCCESS
+            int mpm_status = ap_mpm_query(AP_MPMQ_MPM_STATE, &mpm_state);
+            if (mpm_status == APR_SUCCESS
                 && mpm_state != AP_MPMQ_STOPPING) {
                 stopping = 0;
             }
-
+            else {
+                ap_log_error(APLOG_MARK, WSGI_LOG_INFO(0),
+                             wsgi_server, "mod_wsgi (pid=%d): "
+                             "Process '%s' has died, not restarting because mpm_status == %x, mpm_state == %x",
+                             daemon->process.pid, daemon->group->name,
+                             mpm_status, mpm_state);
+            }
+            
             if (!stopping) {
+                int start_status;
                 ap_log_error(APLOG_MARK, WSGI_LOG_INFO(0),
                              wsgi_server, "mod_wsgi (pid=%d): "
-                             "Process '%s' has died, restarting.",
-                             daemon->process.pid, daemon->group->name);
+                             "Process '%s' has died, (APR_OC_REASON_DEATH) restarting.",
+                             daemon->process.pid,
+                             daemon->group->name);
+                
 
-                wsgi_start_process(wsgi_parent_pool, daemon);
+                start_status = wsgi_start_process(wsgi_parent_pool, daemon);
+                if (start_status != OK)
+                    ap_log_error(APLOG_MARK, WSGI_LOG_INFO(0),
+                                 wsgi_server, "mod_wsgi (pid=%d): "
+                                 "Failed to fork a new process, status = %d",
+                                 old_pid, start_status);
+                else
+                    ap_log_error(APLOG_MARK, WSGI_LOG_INFO(0),
+                                 wsgi_server, "mod_wsgi (pid=%d): "
+                                 "Successfully replaced with pid=%d",
+                                 old_pid, daemon->process.pid);
             }
 
             break;
@@ -9737,7 +9773,11 @@
         case APR_OC_REASON_RESTART: {
 
             /* Stop watching the existing process. */
-
+            ap_log_error(APLOG_MARK, WSGI_LOG_INFO(0),
+                         wsgi_server, "mod_wsgi (pid=%d): "
+                         "Process '%s' restarting, (APR_OC_REASON_RESTART) unregistering",
+                             daemon->process.pid, daemon->group->name);
+            
             apr_proc_other_child_unregister(daemon);
 
             break;
@@ -9746,7 +9786,8 @@
         /* Child daemon process vanished. */
 
         case APR_OC_REASON_LOST: {
-
+            int start_status;
+            
             /* Stop watching the existing process. */
 
             apr_proc_other_child_unregister(daemon);
@@ -9755,10 +9796,20 @@
 
             ap_log_error(APLOG_MARK, WSGI_LOG_INFO(0),
                          wsgi_server, "mod_wsgi (pid=%d): "
-                         "Process '%s' has died, restarting.",
+                         "Process '%s' has died, (APR_OC_REASON_LOST) restarting.",
                          daemon->process.pid, daemon->group->name);
 
-            wsgi_start_process(wsgi_parent_pool, daemon);
+            start_status = wsgi_start_process(wsgi_parent_pool, daemon);
+            if (start_status != OK)
+                ap_log_error(APLOG_MARK, WSGI_LOG_INFO(0),
+                             wsgi_server, "mod_wsgi (pid=%d): "
+                             "failed to fork a new process, status = %d",
+                             old_pid, start_status);
+            else
+                ap_log_error(APLOG_MARK, WSGI_LOG_INFO(0),
+                             wsgi_server, "mod_wsgi (pid=%d): "
+                             "Successfully replaced with pid=%d",
+                             old_pid, daemon->process.pid);
 
             break;
         }
@@ -9768,9 +9819,21 @@
         case APR_OC_REASON_UNREGISTER: {
 
             /* Nothing to do at present. */
-
+            ap_log_error(APLOG_MARK, WSGI_LOG_INFO(0),
+                         wsgi_server, "mod_wsgi (pid=%d): "
+                         "Process '%s' unregistered, (APR_OC_REASON_UNREGISTER) not doing anything",
+                         daemon->process.pid, daemon->group->name);
+                         
             break;
         }
+
+        default: {
+            ap_log_error(APLOG_MARK, WSGI_LOG_INFO(0), wsgi_server,
+                         "mod_wsgi (pid=%d): Unknown error managing child '%s': %d",
+                         daemon->process.pid, daemon->group->name,
+                         reason);
+        }
+
     }
 }
 
@@ -10303,6 +10366,12 @@
             }
         }
 
+        if (wsgi_daemon_shutdown)
+            ap_log_error(APLOG_MARK, WSGI_LOG_INFO(0),
+                         wsgi_server, "mod_wsgi (pid=%d): "
+                         "Shutting down. wsgi_daemon_shutdown = %d",
+                         getpid(), wsgi_daemon_shutdown);
+
         apr_pool_create(&ptrans, p);
 
         /*
@@ -11480,6 +11549,7 @@
             }
         }
         else {
+            /* We have a connection */
             apr_pool_cleanup_register(r->pool, daemon, wsgi_close_socket,
                                       apr_pool_cleanup_null);
 
@@ -11984,8 +12054,12 @@
 
             /* Scan the CGI script like headers from daemon. */
 
-            if ((status = ap_scan_script_header_err_brigade(r, bbin, NULL)))
+            if ((status = ap_scan_script_header_err_brigade(r, bbin, NULL))) {
+                ap_log_rerror(APLOG_MARK, WSGI_LOG_ERR(0), r,
+                              "mod_wsgi (pid=%d): Bad headers from daemon, startup, status = %d",
+                              getpid(), status);
                 return HTTP_INTERNAL_SERVER_ERROR;
+            }
 
             /* Status must be zero for our special headers. */
 
@@ -12138,8 +12212,12 @@
 
     /* Scan the CGI script like headers from daemon. */
 
-    if ((status = ap_scan_script_header_err_brigade(r, bbin, NULL)))
+    if ((status = ap_scan_script_header_err_brigade(r, bbin, NULL))) {
+        ap_log_rerror(APLOG_MARK, WSGI_LOG_ERR(0), r,
+                      "mod_wsgi (pid=%d): Bad headers from daemon, shutdown, status = %d",
+                      getpid(), status);
         return HTTP_INTERNAL_SERVER_ERROR;
+    }
 
     /*
      * Look for special case of status being 0 and
