healthchecks: reference count status file data - #13432
Open
bneradt wants to merge 1 commit into
Open
Conversation
Replaced file data was retired onto a freelist and freed after a timeout, but the deadline was computed from a timestamp taken before the blocking inotify read. That timestamp is stale by however long the watcher waited for an event, so the deadline was routinely already in the past and the data was freed while transactions still referenced it. The patch addresses this issue by holding the data in a shared_ptr and letting each transaction pin its own snapshot. The data now lives exactly as long as it is referenced, which removes both the freelist and the guess about how long a transaction can last. Fixes: apache#8735 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
bneradt
force-pushed
the
healthchecks-use-after-free
branch
from
July 27, 2026 16:33
79b3c22 to
5429a3b
Compare
Comment on lines
132
to
137
| if (nullptr != (fd = fopen(info->fname, "r"))) { | ||
| data->exists = 1; | ||
| do { | ||
| data->b_len = fread(data->body, 1, MAX_BODY_LEN, fd); | ||
| } while (!feof(fd)); /* Only save the last 16KB of the file ... */ | ||
| fclose(fd); |
Comment on lines
+83
to
+103
| /* Take a reference to the current data for this health check file. */ | ||
| HCFileDataPtr | ||
| get_data() | ||
| { | ||
| std::lock_guard<std::mutex> lock{_data_mutex}; | ||
| return _data; | ||
| } | ||
|
|
||
| /* Replace the current data for this health check file. Snapshots handed out by get_data() stay | ||
| valid until their last reference is dropped. */ | ||
| void | ||
| set_data(HCFileDataPtr data) | ||
| { | ||
| std::lock_guard<std::mutex> lock{_data_mutex}; | ||
| _data = std::move(data); | ||
| } | ||
|
|
||
| private: | ||
| std::mutex _data_mutex; /* Protects @a _data */ | ||
| HCFileDataPtr _data; /* Holds the current data for this health check file */ | ||
| }; |
moonchen
approved these changes
Jul 27, 2026
moonchen
left a comment
Contributor
There was a problem hiding this comment.
Thanks for fixing this.
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.
Replaced file data was retired onto a freelist and freed after a
timeout, but the deadline was computed from a timestamp taken before
the blocking inotify read. That timestamp is stale by however long the
watcher waited for an event, so the deadline was routinely already in
the past and the data was freed while transactions still referenced it.
The patch addresses this issue by holding the data in a shared_ptr and
letting each transaction pin its own snapshot. The data now lives
exactly as long as it is referenced, which removes both the freelist
and the guess about how long a transaction can last.
Fixes: #8735