Fix IOBuf TLS block pool self-loop caused by double-return (#3243)#3244
Open
walterzhaoJR wants to merge 3 commits intoapache:masterfrom
Open
Fix IOBuf TLS block pool self-loop caused by double-return (#3243)#3244walterzhaoJR wants to merge 3 commits intoapache:masterfrom
walterzhaoJR wants to merge 3 commits intoapache:masterfrom
Conversation
release_tls_block() and release_tls_block_chain() do not guard against a block being returned to TLS when it is already the list head. The assignment `b->portal_next = block_head` becomes `b->portal_next = b`, forming a self-loop that causes remove_tls_block_chain() or share_tls_block() to spin forever, silently hanging the thread at exit. Fix: - release_tls_block(): add early return when b == block_head, skipping the duplicate insertion. - release_tls_block_chain(): during the existing chain walk, check each node against block_head before linking. Return early if overlap is detected so that num_blocks stays consistent with the actual list length (remove_tls_block_chain verifies this via CHECK_EQ). Add three unit tests that reproduce the self-loop through: 1. Direct double release_tls_block() of the same block. 2. release_tls_block_chain() with a chain overlapping the TLS head. 3. IOBufAsZeroCopyOutputStream::BackUp() followed by a second release.
There was a problem hiding this comment.
Pull request overview
Fixes a TLS IOBuf block-pool corruption case where double-returning a block can create a cyclic free-list and hang the thread on later traversal (notably at thread exit cleanup).
Changes:
- Add a guard in
release_tls_block()intended to prevent re-inserting the TLS head and forming a self-loop. - Add an overlap check in
release_tls_block_chain()intended to prevent linking a chain that would create a cycle and to keepnum_blocksconsistent. - Add three unit tests reproducing the reported hang scenarios from issue #3243.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 9 comments.
| File | Description |
|---|---|
src/butil/iobuf_inl.h |
Adds a duplicate-return guard in release_tls_block() to prevent self-looping the TLS free list. |
src/butil/iobuf.cpp |
Adds an overlap check and uses a saved old_head when returning a block chain to TLS. |
test/iobuf_unittest.cpp |
Adds regression tests that attempt to reproduce and detect the TLS free-list cycle described in #3243. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/butil/iobuf.cpp
Outdated
Comment on lines
+333
to
+337
| @@ -334,18 +334,32 @@ void release_tls_block_chain(IOBuf::Block* b) { | |||
| g_num_hit_tls_threshold.fetch_add(n, butil::memory_order_relaxed); | |||
| return; | |||
| } | |||
| IOBuf::Block* const old_head = tls_data.block_head; | |||
test/iobuf_unittest.cpp
Outdated
Comment on lines
+1982
to
+1986
| // acquire_tls_block() sets portal_next = NULL, breaking the loop. | ||
| butil::iobuf::acquire_tls_block(); | ||
| // block_head is still b (was self-referencing), acquire again to drain. | ||
| butil::iobuf::acquire_tls_block(); | ||
| // TLS is now empty — thread can exit without hanging. |
src/butil/iobuf_inl.h
Outdated
Comment on lines
+614
to
+618
| @@ -615,6 +615,14 @@ inline void release_tls_block(IOBuf::Block* b) { | |||
| b->dec_ref(); | |||
| // g_num_hit_tls_threshold.fetch_add(1, butil::memory_order_relaxed); | |||
| inc_g_num_hit_tls_threshold(); | |||
| } else if (b == tls_data->block_head) { | |||
src/butil/iobuf.cpp
Outdated
Comment on lines
+343
to
+347
| // If any block in the incoming chain is already the TLS block_head, | ||
| // linking last_b->portal_next = block_head would create a cycle: | ||
| // - Single-block chain [B] where B == block_head: | ||
| // last_b->portal_next = B, i.e. B->portal_next = B (self-loop) | ||
| // - Multi-block chain [A -> B] where A == block_head: |
test/iobuf_unittest.cpp
Outdated
Comment on lines
+2028
to
+2029
| butil::iobuf::acquire_tls_block(); | ||
| butil::iobuf::acquire_tls_block(); |
test/iobuf_unittest.cpp
Outdated
Comment on lines
+2081
to
+2082
| butil::iobuf::acquire_tls_block(); | ||
| butil::iobuf::acquire_tls_block(); |
test/iobuf_unittest.cpp
Outdated
Comment on lines
+1993
to
+1999
| TEST_F(IOBufTest, release_tls_block_double_return_creates_self_loop) { | ||
| bool self_loop = false; | ||
| pthread_t tid; | ||
| ASSERT_EQ(0, pthread_create(&tid, NULL, | ||
| double_return_release_tls_block_thread, | ||
| &self_loop)); | ||
| ASSERT_EQ(0, pthread_join(tid, NULL)); |
test/iobuf_unittest.cpp
Outdated
Comment on lines
+2036
to
+2042
| TEST_F(IOBufTest, release_tls_block_chain_overlap_creates_self_loop) { | ||
| bool self_loop = false; | ||
| pthread_t tid; | ||
| ASSERT_EQ(0, pthread_create(&tid, NULL, | ||
| double_return_release_tls_block_chain_thread, | ||
| &self_loop)); | ||
| ASSERT_EQ(0, pthread_join(tid, NULL)); |
test/iobuf_unittest.cpp
Outdated
Comment on lines
+2091
to
+2099
| TEST_F(IOBufTest, backup_then_double_release_creates_self_loop) { | ||
| bool self_loop = false; | ||
| pthread_t tid; | ||
| ASSERT_EQ(0, pthread_create(&tid, NULL, | ||
| backup_double_return_thread, &self_loop)); | ||
| ASSERT_EQ(0, pthread_join(tid, NULL)); | ||
|
|
||
| EXPECT_FALSE(self_loop) | ||
| << "After IOBufAsZeroCopyOutputStream::BackUp() returned a block to " |
fix review comment add new ut
add more ut
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.
release_tls_block() and release_tls_block_chain() do not guard against a block being returned to TLS when it is already the list head. The assignment
b->portal_next = block_headbecomesb->portal_next = b, forming a self-loop that causes remove_tls_block_chain() or share_tls_block() to spin forever, silently hanging the thread at exit.Fix:
Add three unit tests that reproduce the self-loop through:
What problem does this PR solve?
Issue Number: resolve
Problem Summary:
What is changed and the side effects?
Changed:
Side effects:
Performance effects:
Breaking backward compatibility:
Check List: