Skip to content

Commit 7643306

Browse files
authored
[backport] Catch exceptions during file read. (dmlc#10623) (dmlc#10625)
1 parent 9fe50c4 commit 7643306

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

src/common/threading_utils.cc

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,26 @@ std::int32_t GetCGroupV2Count(std::filesystem::path const& bandwidth_path) noexc
7474

7575
std::int32_t GetCfsCPUCount() noexcept {
7676
namespace fs = std::filesystem;
77-
fs::path const bandwidth_path{"/sys/fs/cgroup/cpu.max"};
78-
auto has_v2 = fs::exists(bandwidth_path);
79-
if (has_v2) {
80-
return GetCGroupV2Count(bandwidth_path);
77+
78+
try {
79+
fs::path const bandwidth_path{"/sys/fs/cgroup/cpu.max"};
80+
auto has_v2 = fs::exists(bandwidth_path);
81+
if (has_v2) {
82+
return GetCGroupV2Count(bandwidth_path);
83+
}
84+
} catch (std::exception const&) {
85+
return -1;
8186
}
8287

83-
fs::path const quota_path{"/sys/fs/cgroup/cpu/cpu.cfs_quota_us"};
84-
fs::path const peroid_path{"/sys/fs/cgroup/cpu/cpu.cfs_period_us"};
85-
auto has_v1 = fs::exists(quota_path) && fs::exists(peroid_path);
86-
if (has_v1) {
87-
return GetCGroupV1Count(quota_path, peroid_path);
88+
try {
89+
fs::path const quota_path{"/sys/fs/cgroup/cpu/cpu.cfs_quota_us"};
90+
fs::path const peroid_path{"/sys/fs/cgroup/cpu/cpu.cfs_period_us"};
91+
auto has_v1 = fs::exists(quota_path) && fs::exists(peroid_path);
92+
if (has_v1) {
93+
return GetCGroupV1Count(quota_path, peroid_path);
94+
}
95+
} catch (std::exception const&) {
96+
return -1;
8897
}
8998

9099
return -1;

0 commit comments

Comments
 (0)