Skip to content

Commit 62d6a79

Browse files
xinzhonggvisor-bot
authored andcommitted
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: 955675939
1 parent c1b4e12 commit 62d6a79

2 files changed

Lines changed: 11 additions & 7 deletions

File tree

test/syscalls/linux/BUILD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2444,12 +2444,15 @@ cc_binary(
24442444
deps = [
24452445
"//test/util:capability_util",
24462446
"//test/util:cleanup",
2447+
"//test/util:fs_util",
2448+
"//test/util:posix_error",
24472449
"//test/util:proc_util",
24482450
"//test/util:test_main",
24492451
"//test/util:test_util",
24502452
"//test/util:thread_util",
24512453
"@com_google_absl//absl/algorithm:container",
24522454
"@com_google_absl//absl/strings",
2455+
"@com_google_absl//absl/strings:str_format",
24532456
],
24542457
)
24552458

test/syscalls/linux/rlimits.cc

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,23 @@
1313
// limitations under the License.
1414

1515
#include <errno.h>
16+
#include <linux/capability.h>
1617
#include <stdlib.h>
1718
#include <sys/resource.h>
1819
#include <sys/time.h>
1920
#include <sys/types.h>
2021
#include <sys/wait.h>
2122
#include <unistd.h>
2223

23-
#include <algorithm>
24-
#include <climits>
2524
#include <string>
2625
#include <vector>
2726

2827
#include "absl/algorithm/container.h"
29-
#include "absl/strings/numbers.h"
30-
#include "absl/strings/str_split.h"
28+
#include "absl/strings/str_format.h"
3129
#include "test/util/capability_util.h"
3230
#include "test/util/cleanup.h"
31+
#include "test/util/fs_util.h"
32+
#include "test/util/posix_error.h"
3333
#include "test/util/proc_util.h"
3434
#include "test/util/test_util.h"
3535
#include "test/util/thread_util.h"
@@ -130,11 +130,12 @@ TEST(RlimitTest, RlimitNProc) {
130130
for (int i = 0; i < kNProc; i++) {
131131
pid_t pid = fork();
132132
if (pid == 0) {
133-
while (1) {
134-
sleep(1);
133+
// Child process: Wait passively until the parent terminates us.
134+
while (true) {
135+
pause();
135136
}
136-
_exit(1);
137137
}
138+
// Parent process: Record the child's PID and continue the test.
138139
EXPECT_THAT(pid, SyscallSucceeds());
139140
pids[i] = pid;
140141
}

0 commit comments

Comments
 (0)