Skip to content

Commit 0cdbe3f

Browse files
authored
[core] (cgroups) Use /proc/mounts if mount file is missing. (#58577)
Signed-off-by: irabbani <irabbani@anyscale.com>
1 parent 22fbee3 commit 0cdbe3f

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

src/ray/common/cgroup2/sysfs_cgroup_driver.cc

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,29 @@
4646

4747
namespace ray {
4848
Status SysFsCgroupDriver::CheckCgroupv2Enabled() {
49-
FILE *fp = setmntent(mount_file_path_.c_str(), "r");
49+
std::string mount_file_path = mount_file_path_;
50+
51+
int fd = open(mount_file_path.c_str(), O_RDONLY);
52+
53+
if (fd == -1) {
54+
mount_file_path = fallback_mount_file_path_;
55+
RAY_LOG(WARNING) << absl::StrFormat(
56+
"Failed to open mount fail at %s because of error '%s'. Using fallback mount "
57+
"file at %s.",
58+
mount_file_path_,
59+
strerror(errno),
60+
fallback_mount_file_path_);
61+
} else {
62+
close(fd);
63+
}
64+
65+
FILE *fp = setmntent(mount_file_path.c_str(), "r");
5066

5167
if (!fp) {
5268
return Status::Invalid(
5369
absl::StrFormat("Failed to open mount file at %s. Could not verify that "
5470
"cgroupv2 was mounted correctly. \n%s",
55-
mount_file_path_,
71+
mount_file_path,
5672
strerror(errno)));
5773
}
5874

@@ -71,7 +87,7 @@ Status SysFsCgroupDriver::CheckCgroupv2Enabled() {
7187
return Status::Invalid(
7288
absl::StrFormat("Failed to parse mount file at %s. Could not verify that "
7389
"cgroupv2 was mounted correctly.",
74-
mount_file_path_));
90+
mount_file_path));
7591
}
7692

7793
if (found_cgroupv1 && found_cgroupv2) {

src/ray/common/cgroup2/sysfs_cgroup_driver.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,11 @@ class SysFsCgroupDriver : public CgroupDriverInterface {
4141
/**
4242
* @param mount_file_path only used for testing.
4343
*/
44-
explicit SysFsCgroupDriver(std::string mount_file_path = MOUNTED)
45-
: mount_file_path_(std::move(mount_file_path)) {}
44+
explicit SysFsCgroupDriver(
45+
std::string mount_file_path = MOUNTED,
46+
std::string fallback_mount_file_path = kFallbackMountsFilePath)
47+
: mount_file_path_(std::move(mount_file_path)),
48+
fallback_mount_file_path_(fallback_mount_file_path) {}
4649

4750
~SysFsCgroupDriver() override = default;
4851
SysFsCgroupDriver(const SysFsCgroupDriver &other) = delete;
@@ -286,10 +289,12 @@ class SysFsCgroupDriver : public CgroupDriverInterface {
286289

287290
// Used for unit testing through the constructor.
288291
std::string mount_file_path_;
292+
std::string fallback_mount_file_path_;
289293

290294
static constexpr std::string_view kCgroupProcsFilename = "cgroup.procs";
291295
static constexpr std::string_view kCgroupSubtreeControlFilename =
292296
"cgroup.subtree_control";
293297
static constexpr std::string_view kCgroupControllersFilename = "cgroup.controllers";
298+
static inline std::string kFallbackMountsFilePath = "/proc/mounts";
294299
};
295300
} // namespace ray

src/ray/common/cgroup2/tests/sysfs_cgroup_driver_test.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ TEST(SysFsCgroupDriverTest,
6262
ASSERT_TRUE(s.IsInvalid()) << s.ToString();
6363
}
6464

65+
TEST(SysFsCgroupDriverTest,
66+
CheckCgroupv2EnabledSucceedsIfMountFileNotFoundButFallbackFileIsCorrect) {
67+
TempFile temp_fallback_mount_file;
68+
temp_fallback_mount_file.AppendLine("cgroup2 /sys/fs/cgroup cgroup2 rw 0 0\n");
69+
SysFsCgroupDriver driver("/does/not/exist", temp_fallback_mount_file.GetPath());
70+
Status s = driver.CheckCgroupv2Enabled();
71+
EXPECT_TRUE(s.ok()) << s.ToString();
72+
}
73+
6574
TEST(SysFsCgroupDriverTest, CheckCgroupv2EnabledSucceedsIfOnlyCgroupv2Mounted) {
6675
TempFile temp_mount_file;
6776
temp_mount_file.AppendLine("cgroup2 /sys/fs/cgroup cgroup2 rw 0 0\n");

0 commit comments

Comments
 (0)