#include "xsk.h"
 #include "bpf_util.h"
 
+#define FD_NOT_USED (-1)
+
 #ifndef SOL_XDP
  #define SOL_XDP 283
 #endif
 {
        struct xsk_ctx *ctx = xsk->ctx;
 
+       if (ctx->xsks_map_fd == FD_NOT_USED)
+               return;
+
        bpf_map_delete_elem(ctx->xsks_map_fd, &ctx->queue_id);
        close(ctx->xsks_map_fd);
 }
        ctx->umem = umem;
        ctx->queue_id = queue_id;
        bpf_strlcpy(ctx->ifname, ifname, IFNAMSIZ);
+       ctx->prog_fd = FD_NOT_USED;
+       ctx->link_fd = FD_NOT_USED;
+       ctx->xsks_map_fd = FD_NOT_USED;
 
        ctx->fill = fill;
        ctx->comp = comp;
 
        if (ctx->refcount == 1) {
                xsk_delete_bpf_maps(xsk);
-               close(ctx->prog_fd);
-               if (ctx->has_bpf_link)
+               if (ctx->prog_fd != FD_NOT_USED)
+                       close(ctx->prog_fd);
+               if (ctx->has_bpf_link && ctx->link_fd != FD_NOT_USED)
                        close(ctx->link_fd);
        }