ad3991
diff --git a/mod_wsgi.c b/mod_wsgi.c
ad3991
index fd304bd..c65344e 100644
ad3991
--- a/mod_wsgi.c
ad3991
+++ b/mod_wsgi.c
ad3991
@@ -13659,14 +13659,37 @@ static int wsgi_hook_init(apr_pool_t *pconf, apr_pool_t *ptemp,
ad3991
      * Init function gets called twice during startup, we only
ad3991
      * need to actually do anything on the second time it is
ad3991
      * called. This avoids unecessarily initialising and then
ad3991
-     * destroying Python for no reason.
ad3991
+     * destroying Python for no reason. We also though have to
ad3991
+     * deal with a special case when a graceful restart is done.
ad3991
+     * For that we are only called once, which is generally okay
ad3991
+     * as the 'wsgi_init' key will be set from initial start up
ad3991
+     * of the server. The exception to this is where the module
ad3991
+     * is only loaded into Apache when the server is already
ad3991
+     * running. In this case we have to detect that it is not
ad3991
+     * the initial startup, but a subsequent restart. We can do
ad3991
+     * this by looking at whether the scoreboard has been
ad3991
+     * initialised yet. That is probably enough, but to be safe,
ad3991
+     * also check what generation it is.
ad3991
      */
ad3991
 
ad3991
     apr_pool_userdata_get(&data, userdata_key, s->process->pool);
ad3991
     if (!data) {
ad3991
         apr_pool_userdata_set((const void *)1, userdata_key,
ad3991
                               apr_pool_cleanup_null, s->process->pool);
ad3991
-        return OK;
ad3991
+
ad3991
+        /*
ad3991
+         * Check for the special case of a graceful restart and
ad3991
+         * the module being loaded for the first time. In this
ad3991
+         * case we still go onto perform initialisation as the
ad3991
+         * initialisation routine for the module will not be
ad3991
+         * called a second time.
ad3991
+         */
ad3991
+
ad3991
+        if (!ap_scoreboard_image ||
ad3991
+            ap_get_scoreboard_global()->running_generation == 0) {
ad3991
+
ad3991
+            return OK;
ad3991
+        }
ad3991
     }
ad3991
 
ad3991
     /* Setup module version information. */