swrap: Update free-list only when refcount is zero
[socket_wrapper.git] / src / socket_wrapper.c
index ed42b2d2bbdac169123648b6a4e142ecda1ba175..6c9ec510ed3c15fdac9043c3945c735e8d7bf499 100644 (file)
@@ -2015,8 +2015,6 @@ static void swrap_remove_stale(int fd)
 
        SWRAP_DLIST_REMOVE(socket_fds, fi);
 
-       swrap_set_next_free(si, first_free);
-       first_free = si_index;
        swrap_dec_refcount(si);
 
        free(fi);
@@ -2029,6 +2027,9 @@ static void swrap_remove_stale(int fd)
                unlink(si->un_addr.sun_path);
        }
 
+       swrap_set_next_free(si, first_free);
+       first_free = si_index;
+
 out:
        SWRAP_UNLOCK_SI(si);
        SWRAP_UNLOCK(first_free);
@@ -5887,9 +5888,6 @@ static int swrap_close(int fd)
 
        ret = libc_close(fd);
 
-       swrap_set_next_free(si, first_free);
-       first_free = si_index;
-
        swrap_dec_refcount(si);
 
        free(fi);
@@ -5912,6 +5910,9 @@ static int swrap_close(int fd)
                unlink(si->un_addr.sun_path);
        }
 
+       swrap_set_next_free(si, first_free);
+       first_free = si_index;
+
 out:
        SWRAP_UNLOCK_SI(si);
        SWRAP_UNLOCK(first_free);