From b593d86d1bbfc84c992d5d9dc887f19bc1eb1994 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 2 Jun 2024 09:10:03 +0900 Subject: [PATCH 1/2] test-async: wait a while for fd to be closed When this is run on slow environment, e.g. sanitizer is enabled, then waiting for 1 second may not be enough. Hopefully fixes #33150. --- src/test/test-async.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/test/test-async.c b/src/test/test-async.c index 4076213d83..2771cbd5aa 100644 --- a/src/test/test-async.c +++ b/src/test/test-async.c @@ -10,6 +10,7 @@ #include "path-util.h" #include "process-util.h" #include "signal-util.h" +#include "time-util.h" #include "tests.h" #include "tmpfile-util.h" @@ -17,6 +18,18 @@ TEST(asynchronous_sync) { ASSERT_OK(asynchronous_sync(NULL)); } +static void wait_fd_closed(int fd) { + for (unsigned trial = 0; trial < 100; trial++) { + usleep_safe(100 * USEC_PER_MSEC); + if (fcntl(fd, F_GETFD) < 0) { + assert_se(errno == EBADF); + return; + } + } + + assert_not_reached(); +} + TEST(asynchronous_close) { _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-asynchronous_close.XXXXXX"; int fd, r; @@ -24,11 +37,7 @@ TEST(asynchronous_close) { fd = mkostemp_safe(name); ASSERT_OK(fd); asynchronous_close(fd); - - sleep(1); - - ASSERT_EQ(fcntl(fd, F_GETFD), -1); - assert_se(errno == EBADF); + wait_fd_closed(fd); r = safe_fork("(subreaper)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGKILL|FORK_LOG|FORK_WAIT, NULL); ASSERT_OK(r); @@ -41,11 +50,7 @@ TEST(asynchronous_close) { fd = open("/dev/null", O_RDONLY|O_CLOEXEC); ASSERT_OK(fd); asynchronous_close(fd); - - sleep(1); - - ASSERT_EQ(fcntl(fd, F_GETFD), -1); - assert_se(errno == EBADF); + wait_fd_closed(fd); _exit(EXIT_SUCCESS); } From 94e8dd54d023f1d1a0b4be7b66f8ad7220ac0b54 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 2 Jun 2024 09:17:40 +0900 Subject: [PATCH 2/2] test-async: also check if the directory is removed --- src/test/test-async.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/test/test-async.c b/src/test/test-async.c index 2771cbd5aa..ee6bae2698 100644 --- a/src/test/test-async.c +++ b/src/test/test-async.c @@ -56,6 +56,18 @@ TEST(asynchronous_close) { } } +static void wait_rm_rf(const char *path) { + for (unsigned trial = 0; trial < 100; trial++) { + usleep_safe(100 * USEC_PER_MSEC); + if (access(path, F_OK) < 0) { + assert_se(errno == ENOENT); + return; + } + } + + assert_not_reached(); +} + TEST(asynchronous_rm_rf) { _cleanup_free_ char *t = NULL, *k = NULL; int r; @@ -64,8 +76,9 @@ TEST(asynchronous_rm_rf) { assert_se(k = path_join(t, "somefile")); ASSERT_OK(touch(k)); ASSERT_OK(asynchronous_rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL)); + wait_rm_rf(t); - /* Do this once more, form a subreaper. Which is nice, because we can watch the async child even + /* Do this once more, from a subreaper. Which is nice, because we can watch the async child even * though detached */ r = safe_fork("(subreaper)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_WAIT, NULL);