Fix synchronization of object pool free chunks by songgaoye · Pull Request #3615 · apache/brpc · GitHub
Skip to content

Fix synchronization of object pool free chunks - #3615

Open
songgaoye wants to merge 3 commits into
apache:masterfrom
songgaoye:fix/pool-free-chunk-race
Open

songgaoye wants to merge 3 commits into
apache:masterfrom
songgaoye:fix/pool-free-chunk-race

Conversation

@songgaoye

@songgaoye songgaoye commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

Issue Number: resolve

Problem Summary:

ObjectPool and ResourcePool access _free_chunks from multiple threads.

pop_free_chunk() previously read _free_chunks.empty() before acquiring
_free_chunks_mutex, while push_free_chunk() modifies the same vector under
the mutex. Concurrent reads and writes of std::vector metadata are a C++ data
race.

What is changed and the side effects?

Changed:

  • Add _nfree_chunks, a relaxed atomic count of global free chunks, to both
    ObjectPool and ResourcePool.
  • Update the count under _free_chunks_mutex after every successful push and
    pop.
  • Use the count as a race-free fast-path hint before locking in
    pop_free_chunk().
  • Keep the existing locked _free_chunks.empty() check after acquiring the
    mutex.
  • Add ObjectPool and ResourcePool tests covering free-chunk push/pop and
    cross-thread reuse.

Side effects:

  • Performance effects:

    The empty-list fast path no longer reads std::vector without
    synchronization. A relaxed atomic load avoids mutex acquisition when no
    global free chunk is available, preserving the intended hot-path behavior.

  • Breaking backward compatibility:

    None.


Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The synchronization issue is addressed consistently in both reviewed files, with no unresolved blocking issues.

0 open findings

What changed in this PR

This PR fixes a data race in concurrent access to object and resource pool free chunks.

Changes:

  • Removes unlocked _free_chunks.empty() checks.
  • Protects all free-chunk access with the existing mutex.
File Description
src/​butil/​resource_pool_inl.h Synchronizes resource-pool free-chunk access.
src/​butil/​object_pool_inl.h Synchronizes object-pool free-chunk access.

🧠 Review effort: Lite


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@songgaoye
songgaoye force-pushed the fix/pool-free-chunk-race branch from 32aa81f to f2960fc Compare October 18, 2026 00:59

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The reviewed synchronization changes address the reported data race with no unresolved blocking issues.

No open issues 🎉

Review effort: Lite

@wwbmmm wwbmmm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[replied by brpc-oncall robot] Removing the unlocked read fixes the data race, but it also deletes a deliberate fast path that avoided the global mutex in the high-contention case; please keep a race-free fast path (atomic free-chunk count) or show the cost is acceptable.

Comment thread src/butil/object_pool_inl.h
@songgaoye

songgaoye commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor Author

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.

3 participants