diff --git a/proof/proofd/src/proofexecv.cxx b/proof/proofd/src/proofexecv.cxx
index 9a31a37c678294eff396df4776d2b8e55aaa0888..db1ed9cf51dfe915fd758c4ff34d51713c3b2392 100644
--- a/proof/proofd/src/proofexecv.cxx
+++ b/proof/proofd/src/proofexecv.cxx
@@ -639,9 +639,13 @@ int exportsock(rpdunix *conn)
    if (d == 0 || d == 1 || d == 2) {
       int fd = -1;
       int natt = 1000;
-      while (natt > 0 && (fd = dup(d)) <= 2) { natt--; }
+      while (natt > 0 && (fd = dup(d)) <= 2) {
+         if (fd != d) close(fd);
+         natt--;
+      }
       if (natt <= 0 && fd <= 2) {
          Info("exportsock: ERROR: no free filedescriptor!");
+         if (fd != d) close(fd);
          close(d);
          return -1;
       }