Copilot commented on code in PR #3282:
URL: https://github.com/apache/brpc/pull/3282#discussion_r3141618253
##########
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;
Review Comment:
`ever_printed_stat_err` is a non-atomic function-local static that can be
read/written concurrently by the sampler thread(s), which is a data race/UB and
can also result in multiple prints. Use a thread-safe one-time guard (e.g.,
`butil::static_atomic<bool>` with `exchange`, `std::atomic<bool>`, or
`pthread_once`) for the print-once behavior.
##########
src/bvar/default_variables.cpp:
##########
@@ -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;
Review Comment:
This warning prints `errno` for an `fscanf` parse failure, but `fscanf`
often fails without setting `errno` (so it may misleadingly print `errno=0`).
Consider resetting `errno` before `fscanf` and/or reporting the `fscanf` return
value / `ferror(fp)` (and only include `errno`/`strerror` when an actual I/O
error occurred).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]