fix: avoid SIGSEGV in read_proc_status during static initialization#3282
Open
gamezhoulei wants to merge 1 commit intoapache:masterfrom
Open
fix: avoid SIGSEGV in read_proc_status during static initialization#3282gamezhoulei wants to merge 1 commit intoapache:masterfrom
gamezhoulei wants to merge 1 commit intoapache:masterfrom
Conversation
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 <zhoulei@xsky.com>
Author
|
Hi @chenBright , could you please help review this PR when you have time? This fixes a static-initialization crash in bvar when read_proc_status() logs through glog before the application has a chance to initialize glog. |
There was a problem hiding this comment.
Pull request overview
This PR addresses a startup-time crash risk in bvar::read_proc_status() by removing PLOG/PLOG_ONCE usage in Linux /proc/self/stat failure paths, avoiding glog invocation during static initialization (before main() and google::InitGoogleLogging()).
Changes:
- Replace
/proc/self/statopen failure logging with a one-timefprintf(stderr, ...)warning. - Replace
/proc/self/statparse failure logging withfprintf(stderr, ...)warning. - Keep return values and sampling behavior unchanged while making failure-path logging safer pre-
main().
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+84
to
+88
| 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; |
Comment on lines
101
to
104
| &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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What problem does this PR solve?
Issue Number: resolve
Problem Summary:
bvar::read_proc_status()may be sampled while default bvars are initializedbefore
main(). If reading or parsing/proc/self/statfails in that phase,the current code logs the failure with
PLOG/PLOG_ONCE. For applications thatinitialize glog in
main(), this can call glog before it is ready and crashduring static initialization.
The crash happens before entering
main(), so application code has no chance tocall
google::InitGoogleLogging()first.One observed stack of the crashing thread is:
Another thread was already the bvar sampler thread, which shows that the crash
was triggered by the default bvar sampling path:
The faulting instruction dereferenced a null glog flag pointer while initializing
a log message:
The corresponding variable value in the core was null:
The effective trigger path is:
This is similar to the static-initialization crash fixed for
read_proc_io()in#3184, but this path goes through
/proc/self/stat.What is changed and the side effects?
Changed:
/proc/self/statopen-failurePLOG_ONCEinread_proc_status()with a one-timefprintf(stderr, ...)warning./proc/self/statparse-failurePLOGinread_proc_status()withfprintf(stderr, ...).Side effects:
paths.
Check List: