diff --git a/htools/Ganeti/Confd/Server.hs b/htools/Ganeti/Confd/Server.hs
index 35aab41b858a87688f63e539a676f00e34ee8da6..39bef213b8d140e71a7b12b58c7a676befab6bab 100644
--- a/htools/Ganeti/Confd/Server.hs
+++ b/htools/Ganeti/Confd/Server.hs
@@ -58,7 +58,7 @@ import Ganeti.Logging
 import Ganeti.Utils
 import qualified Ganeti.Constants as C
 import qualified Ganeti.Path as Path
-import Ganeti.Query.Server (runQueryD)
+import Ganeti.Query.Server (prepQueryD, runQueryD)
 
 -- * Types and constants definitions
 
@@ -526,7 +526,9 @@ main opts _ _ = do
   _ <- forkIO $ onTimeoutTimer inotiaction Path.clusterConfFile cref statemvar
   -- fork the polling timer
   _ <- forkIO $ onReloadTimer inotiaction Path.clusterConfFile cref statemvar
+  -- prepare the queryd listener
+  query_data <- prepQueryD Nothing
   -- launch the queryd listener
-  _ <- forkIO $ runQueryD Nothing (configReader cref)
+  _ <- forkIO $ runQueryD query_data (configReader cref)
   -- and finally enter the responder loop
   forever $ listener s hmac (responder cref)
diff --git a/htools/Ganeti/Query/Server.hs b/htools/Ganeti/Query/Server.hs
index 6b33a9ea92bc347ed215f2e6416bd01a050085e5..6909cc9c2397e52964080770b94ceff41f097786 100644
--- a/htools/Ganeti/Query/Server.hs
+++ b/htools/Ganeti/Query/Server.hs
@@ -27,6 +27,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 
 module Ganeti.Query.Server
   ( ConfigReader
+  , prepQueryD
   , runQueryD
   ) where
 
@@ -218,13 +219,17 @@ mainLoop creader socket = do
   _ <- forkIO $ clientLoop client creader
   mainLoop creader socket
 
--- | Main function that runs the query endpoint. This should be the
--- only one exposed from this module.
-runQueryD :: Maybe FilePath -> ConfigReader -> IO ()
-runQueryD fpath creader = do
+-- | Function that prepares the server socket.
+prepQueryD :: Maybe FilePath -> IO (FilePath, S.Socket)
+prepQueryD fpath = do
   let socket_path = fromMaybe Path.defaultQuerySocket fpath
   cleanupSocket socket_path
-  bracket
-    (getServer socket_path)
-    (closeServer socket_path)
-    (mainLoop creader)
+  s <- getServer socket_path
+  return (socket_path, s)
+
+-- | Main function that runs the query endpoint.
+runQueryD :: (FilePath, S.Socket) -> ConfigReader -> IO ()
+runQueryD (socket_path, server) creader =
+  finally
+    (mainLoop creader server)
+    (closeServer socket_path server)