Skip to content

Commit

Permalink
Fix file descriptor leak. (#7704)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivialfis authored Feb 25, 2022
1 parent 1b25dd5 commit 5eed299
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/common/threading_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
* Copyright 2022 by XGBoost Contributors
*/
#include "threading_utils.h"
#if defined(__linux__)
#include <fcntl.h>
#include <unistd.h>
#endif // defined(__linux__)

#include <fstream>
#include <string>

#include "xgboost/logging.h"

namespace xgboost {
Expand All @@ -26,12 +26,12 @@ int32_t GetCfsCPUCount() noexcept {
// swap limt /sys/fs/cgroup/memory.memsw.limit_in_bytes

auto read_int = [](char const* const file_path) noexcept {
auto const fd(::open(file_path, O_RDONLY, 0));
if (fd == -1) {
std::ifstream fin(file_path);
if (!fin) {
return -1;
}
char value[64];
CHECK(::read(fd, value, sizeof(value)) < signed(sizeof(value)));
std::string value;
fin >> value;
try {
return std::stoi(value);
} catch (std::exception const&) {
Expand Down

0 comments on commit 5eed299

Please sign in to comment.