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