From de57e26bec4e0a566177de2416c36195564f374a Mon Sep 17 00:00:00 2001 From: Xin Zhong Date: Fri, 31 Jul 2026 18:14:01 -0700 Subject: [PATCH] Fix unreachable code warning and unsafe sleep in rlimits test The fork() loop in RLIMIT_NPROC tests previously used an infinite while(1) { sleep(1); } loop followed by an unreachable _exit(1). This caused a -Wunreachable-code warning. Additionally, invoking sleep() immediately after a bare fork() can occasionally hang when test frameworks are run with memory interceptors like TSan. PiperOrigin-RevId: 957422827 --- test/syscalls/linux/BUILD | 3 +++ test/syscalls/linux/rlimits.cc | 15 ++++++++------- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/test/syscalls/linux/BUILD b/test/syscalls/linux/BUILD index 1f1d66b3a5..3db8109b4e 100644 --- a/test/syscalls/linux/BUILD +++ b/test/syscalls/linux/BUILD @@ -2444,12 +2444,15 @@ cc_binary( deps = [ "//test/util:capability_util", "//test/util:cleanup", + "//test/util:fs_util", + "//test/util:posix_error", "//test/util:proc_util", "//test/util:test_main", "//test/util:test_util", "//test/util:thread_util", "@com_google_absl//absl/algorithm:container", "@com_google_absl//absl/strings", + "@com_google_absl//absl/strings:str_format", ], ) diff --git a/test/syscalls/linux/rlimits.cc b/test/syscalls/linux/rlimits.cc index 142469610f..092e273802 100644 --- a/test/syscalls/linux/rlimits.cc +++ b/test/syscalls/linux/rlimits.cc @@ -13,6 +13,7 @@ // limitations under the License. #include +#include #include #include #include @@ -20,16 +21,15 @@ #include #include -#include -#include #include #include #include "absl/algorithm/container.h" -#include "absl/strings/numbers.h" -#include "absl/strings/str_split.h" +#include "absl/strings/str_format.h" #include "test/util/capability_util.h" #include "test/util/cleanup.h" +#include "test/util/fs_util.h" +#include "test/util/posix_error.h" #include "test/util/proc_util.h" #include "test/util/test_util.h" #include "test/util/thread_util.h" @@ -130,11 +130,12 @@ TEST(RlimitTest, RlimitNProc) { for (int i = 0; i < kNProc; i++) { pid_t pid = fork(); if (pid == 0) { - while (1) { - sleep(1); + // Child process: Wait passively until the parent terminates us. + while (true) { + pause(); } - _exit(1); } + // Parent process: Record the child's PID and continue the test. EXPECT_THAT(pid, SyscallSucceeds()); pids[i] = pid; }