]> git.hungrycats.org Git - btrfs-progs/commitdiff
btrfs-progs: scrub: survive a progress client that closes early
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Fri, 30 Oct 2026 05:04:01 +0000 (01:04 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Fri, 30 Oct 2026 05:04:01 +0000 (01:04 -0400)
scrub start answers each connection to its progress socket by writing
the current progress to it.  A client that closes the connection without
reading makes that write raise SIGPIPE, which kills the scrub process
and with it the scrub.  scrub start itself is such a client: when the
socket is in use, it connects to see whether a scrub is running and
closes the connection at once.  So "btrfs scrub start -f" while another
"btrfs scrub start -B" runs on the same filesystem kills the first one,
and a scrub started with --limit then leaves the --limit value on the
devices.  A write error to a client also ended the progress thread, so
the progress of the rest of the scrub was not recorded.

Block SIGPIPE in the progress thread, so the write fails with EPIPE
instead, and do not treat a failed write to a client as an error of the
scrub: close the connection and go on.

Fixes: 4ba165318745 ("btrfs-progs: scrub userland implementation")
Assisted-by: Claude:claude-opus-5-5
cmds/scrub.c

index 2b6df6f5b3de9ab8b3d5bf1b87fb9fa9e77908e5..1843856bd210973d1fd4d2693114ba4ded079eb3 100644 (file)
@@ -1019,6 +1019,18 @@ static void *scrub_progress_cycle(void *ctx)
        };
        struct sockaddr_un peer;
        socklen_t peer_size = sizeof(peer);
+       sigset_t sigpipe;
+
+       /*
+        * A client may close the connection before reading the progress, e.g.
+        * the check for a running scrub in scrub start.  Writing to it must
+        * fail with EPIPE in this thread, not kill the scrub with SIGPIPE.
+        */
+       sigemptyset(&sigpipe);
+       sigaddset(&sigpipe, SIGPIPE);
+       perr = pthread_sigmask(SIG_BLOCK, &sigpipe, NULL);
+       if (perr)
+               goto out;
 
        perr = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old);
        if (perr)
@@ -1110,13 +1122,13 @@ static void *scrub_progress_cycle(void *ctx)
                                ret = -errno;
                                goto out;
                        }
-                       if (ret) {
-                               ret = scrub_write_file(
-                                       peer_fd, fsid,
-                                       &spc->progress[this * ndev], ndev);
-                               if (ret)
-                                       goto out;
-                       }
+                       /*
+                        * A failed write only means the client went away; keep
+                        * scrubbing and serving the next one.
+                        */
+                       if (ret)
+                               scrub_write_file(peer_fd, fsid,
+                                                &spc->progress[this * ndev], ndev);
                        close(peer_fd);
                        peer_fd = -1;
                }