Skip to content

Commit

Permalink
Explain the weird destruction order
Browse files Browse the repository at this point in the history
Signed-off-by: Raven Black <ravenblack@dropbox.com>
  • Loading branch information
ravenblackx committed Oct 2, 2024
1 parent 68032bf commit 096b142
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion source/extensions/filters/http/cache/cache_insert_queue.cc
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,16 @@ void CacheInsertQueue::onFragmentComplete(bool cache_success, bool end_stream, s
fragments_.clear();
// Clearing self-ownership might provoke the destructor, so take a copy of the
// abort callback to avoid reading from 'this' after it may be deleted.
//
// This complexity is necessary because if the queue *is not* currently
// self-owned, it will be deleted during insertQueueAborted, so
// clearing self_ownership_ second would be a write-after-destroy error.
// If it *is* currently self-owned, then we must still call the callback if
// any, but clearing self_ownership_ *first* would mean we got destroyed
// so we would no longer have access to the callback.
// Since destroying first *or* second can be an error, rearrange things
// so that destroying first *is not* an error. :)
auto callbacks = std::move(callbacks_);
callbacks_.reset();
self_ownership_.reset();
if (callbacks.has_value()) {
callbacks->insertQueueAborted();
Expand Down

0 comments on commit 096b142

Please sign in to comment.