From 6913eda9811090b025c363b28a773a376e893cfe Mon Sep 17 00:00:00 2001 From: zhoulei Date: Fri, 24 Apr 2026 23:02:35 -0400 Subject: [PATCH] fix: avoid SIGSEGV in read_proc_status during static initialization read_proc_status can be sampled while default bvars are initialized before main(). If reading /proc/self/stat fails at that time, logging through glog may access uninitialized glog state and crash. Print the warning to stderr instead, matching the read_proc_io fallback. Signed-off-by: zhoulei --- src/bvar/default_variables.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/bvar/default_variables.cpp b/src/bvar/default_variables.cpp index 1d60063526..40d30c56e7 100644 --- a/src/bvar/default_variables.cpp +++ b/src/bvar/default_variables.cpp @@ -81,7 +81,12 @@ static bool read_proc_status(ProcStat &stat) { // see http://man7.org/linux/man-pages/man5/proc.5.html butil::ScopedFILE fp("/proc/self/stat", "r"); if (NULL == fp) { - PLOG_ONCE(WARNING) << "Fail to open /proc/self/stat"; + static bool ever_printed_stat_err = false; + if (!ever_printed_stat_err) { + fprintf(stderr, "WARNING: Fail to open /proc/self/stat, errno=%d. " + "Process status related bvars will be unavailable.\n", errno); + ever_printed_stat_err = true; + } return false; } if (fscanf(fp, "%d %*s %c " @@ -94,7 +99,8 @@ static bool read_proc_status(ProcStat &stat) { &stat.flags, &stat.minflt, &stat.cminflt, &stat.majflt, &stat.cmajflt, &stat.utime, &stat.stime, &stat.cutime, &stat.cstime, &stat.priority, &stat.nice, &stat.num_threads) != 19) { - PLOG(WARNING) << "Fail to fscanf"; + fprintf(stderr, "WARNING: Fail to fscanf /proc/self/stat, errno=%d. " + "Process status related bvars will be unavailable.\n", errno); return false; } return true;