diff --git a/src/libckpool.c b/src/libckpool.c
index 0c1b891e..4c315dfb 100644
--- a/src/libckpool.c
+++ b/src/libckpool.c
@@ -620,13 +620,17 @@ void block_socket(int fd)
 
 void _close(int *fd, const char *file, const char *func, const int line)
 {
+	int sockd;
+
 	if (*fd < 0)
 		return;
-	LOGDEBUG("Closing file handle %d", *fd);
-	if (unlikely(close(*fd)))
-		LOGWARNING("Close of fd %d failed with errno %d:%s from %s %s:%d",
-			   *fd, errno, strerror(errno), file, func, line);
+	sockd = *fd;
+	LOGDEBUG("Closing file handle %d", sockd);
 	*fd = -1;
+	if (unlikely(close(sockd))) {
+		LOGWARNING("Close of fd %d failed with errno %d:%s from %s %s:%d",
+			   sockd, errno, strerror(errno), file, func, line);
+	}
 }
 
 int bind_socket(char *url, char *port)