Skip to content

Fix bthread key metadata races - #3768

Open
songgaoye wants to merge 1 commit into
apache:masterfrom
songgaoye:fix/bthread-key-metadata-race
Open

songgaoye wants to merge 1 commit into
apache:masterfrom
songgaoye:fix/bthread-key-metadata-race

Conversation

@songgaoye

@songgaoye songgaoye commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

Issue Number: resolve

Problem Summary:

Key creation, deletion, and key-table cleanup can concurrently access the same
key metadata without synchronization. When a key slot is reused while a thread
exits, cleanup may observe inconsistent version, destructor, and argument
values.

What is changed and the side effects?

Changed:

  • Protect key metadata updates and destructor snapshots with the existing key
    mutex.
  • Make the key version atomic for the lock-free bthread_setspecific check.
  • Invoke destructors after releasing the mutex so they can create or delete keys.
  • Add tests for key reuse during thread exit and key recreation in a destructor.

Side effects:

  • Performance effects: Key-table cleanup now acquires the key mutex when it
    processes a non-null value. The impact has not been benchmarked.

  • Breaking backward compatibility: No API changes.


Check List:

  • The new tests passed using an existing local test binary. Rebuilding the
    current commit was blocked by a missing macOS SDK in the local configuration.
  • Added regression tests for the concurrency and destructor-reentrancy paths.
  • Please follow Contributor Covenant Code of Conduct.

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.

🔵 Needs a closer look

Because the change modifies low-level concurrent key lifecycle synchronization and destructor cleanup, and the commit could not be rebuilt or stress-tested here, human runtime validation remains necessary.

No open issues 🎉

What changed in this PR

Fixes races in bthread key metadata during key creation, deletion, reuse, and thread-local cleanup while preserving destructor reentrancy.

Changes:

  • Synchronizes key metadata updates and destructor snapshots with the key mutex.
  • Makes key versions atomic for lock-free validation.
  • Invokes destructors after releasing the mutex.
  • Adds regression tests for key reuse during thread exit and destructor-driven key recreation.
File Description
test/​bthread_key_unittest.cpp Adds regression tests covering key reuse during thread exit and key recreation from destructors.
src/​bthread/​key.cpp Synchronizes key metadata lifecycle operations, uses atomic key versions, and invokes destructors outside the metadata mutex.

Review effort: Lite

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.

2 participants