Fix[bmqp::RequestManager]: unsafe read without mutex - #1713
Open
678098 wants to merge 3 commits into
Open
Conversation
The justification to use reinterpret_cast used in bmqp::RequestManager is incorrect now, possible to simplify and remove it. Signed-off-by: Evgeny Malygin <emalygin@bloomberg.net>
bmqp::RequestManager docs mention invariant: all requests must be cancelled in the same order as they recorded. This commit adds a test for it. Signed-off-by: Evgeny Malygin <emalygin@bloomberg.net>
The existing code reads map bucket count without holding a mutex. Other thread might cause rehashing as we are reading this value, leading to UB. Signed-off-by: Evgeny Malygin <emalygin@bloomberg.net>
678098
commented
Aug 14, 2026
| BSLA_MAYBE_UNUSED bsl::pair<RequestMapIter, bool> | ||
| insertRC = requestsCopy.insert( | ||
| bsl::make_pair(it->first, it->second)); | ||
| BSLS_ASSERT_SAFE(insertRC.second); |
Collaborator
Author
There was a problem hiding this comment.
We iterate through bmqc::OrderedHashMap so we have guarantee that keys in this map are unique. Due to this, insert return codes checks are redundant. Moreover, we don't need a second map here and can use a vector
678098
commented
Aug 14, 2026
|
|
||
| // Create a new map so we can work on it outside the mutex | ||
| RequestMap requestsCopy(d_requests.bucket_count(), | ||
| d_requests.get_allocator()); |
Collaborator
Author
There was a problem hiding this comment.
d_requests.bucket_count() is not thread-safe: this call here was a bug
678098
commented
Aug 14, 2026
| { | ||
| bslmt::LockGuard<bslmt::Mutex> guard(&d_mutex); // MUTEX LOCKED | ||
|
|
||
| requestsCopy.reserve(d_requests.size()); |
Collaborator
Author
There was a problem hiding this comment.
This reserve might over-reserve memory, if we are cancelling only a small part of all requests. This raises a question what is better: dynamic resizes or one resize for a possibly big chunk of memory.
I think one allocation is better here
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.
reinterpret_castvector, not themap, to iterate through the requests that has to be cancelled