Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix random value of CachedReader at first time #2556

Merged
merged 1 commit into from
Mar 12, 2024

Conversation

chenBright
Copy link
Contributor

@chenBright chenBright commented Mar 2, 2024

What problem does this PR solve?

Issue Number:

Problem Summary:

CachedReader::get_value中fn没有在锁内执行,第一次执行get_value的时候,可能会读到_cache的初始值。由于_cache没有显式初始化,而且实际的类型(如ProcMemory、ProcStat)没有实现默认构造函数,所以成员变量的值是随机值。相对于随机值,值为0,让人更好理解一些。

pthread_mutex_lock(&p->_mutex);
if (now > p->_mtime_us + CACHED_INTERVAL_US) {
p->_mtime_us = now;
pthread_mutex_unlock(&p->_mutex);
// don't run fn inside lock otherwise a slow fn may
// block all concurrent bvar dumppers. (e.g. /vars)
T result;
if (fn(&result)) {
pthread_mutex_lock(&p->_mutex);
p->_cached = result;
} else {
pthread_mutex_lock(&p->_mutex);
}
}
pthread_mutex_unlock(&p->_mutex);
}
return p->_cached;

What is changed and the side effects?

Changed:

Side effects:

  • Performance effects(性能影响):

  • Breaking backward compatibility(向后兼容性):


Check List:

  • Please make sure your changes are compilable(请确保你的更改可以通过编译).
  • When providing us with a new feature, it is best to add related tests(如果你向我们增加一个新的功能, 请添加相关测试).
  • Please follow Contributor Covenant Code of Conduct.(请遵循贡献者准则).

@wwbmmm
Copy link
Contributor

wwbmmm commented Mar 11, 2024

LGTM

1 similar comment
@serverglen
Copy link
Contributor

LGTM

@serverglen serverglen merged commit 965279f into apache:master Mar 12, 2024
18 checks passed
@chenBright chenBright deleted the fix_default_var branch March 12, 2024 02:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants