diff --git a/lnet/utils/debug.c b/lnet/utils/debug.c
index 8dfc0fff2654a9d62f6430ebc128b854630e2ad3..2cbfcd1706179fa37eed8aff270e61c219ee3679 100644
--- a/lnet/utils/debug.c
+++ b/lnet/utils/debug.c
@@ -244,10 +244,11 @@ static int applymask(char* procpath, int value)
         if (rc != 0) {
                 fprintf(stderr, "Write to %s failed: %s\n",
                         procpath, strerror(errno));
-                return rc;
         }
+
         dbg_close_ctlhandle(fd);
-        return 0;
+
+        return rc;
 }
 
 static void applymask_all(unsigned int subs_mask, unsigned int debug_mask)
@@ -479,7 +480,7 @@ int jt_dbg_debug_kernel(int argc, char **argv)
         if (rc != 0) {
                 fprintf(stderr, "write(%s) failed: %s\n", filename,
                         strerror(errno));
-                close(fd);
+                dbg_close_ctlhandle(fd);
                 return 1;
         }
         dbg_close_ctlhandle(fd);