Skip to content

[fix][client] Fix data races on the random generators of generateRandomName and ConnectionPool - #627

Open
groeneai wants to merge 1 commit into
apache:mainfrom
groeneai:fix-random-generator-races
Open

groeneai wants to merge 1 commit into
apache:mainfrom
groeneai:fix-random-generator-races

Conversation

@groeneai

Copy link
Copy Markdown

Motivation

generateRandomName (used for the default consumer name and the reader subscription name) mutates a process-wide std::mt19937 without synchronization, so clients creating consumers or readers concurrently race on it (reported by ThreadSanitizer). ConnectionPool::generateRandomIndex is called both from the caller thread and from the executor threads (every HandlerBase takes a connection key suffix from it), so its randomEngine_ is raced on too.

Modifications

  • generateRandomName uses a thread_local generator seeded from std::random_device.
  • ConnectionPool::generateRandomIndex guards its generator with a mutex.

Verifying this change

  • Make sure that the change passes the CI checks.

This change is already covered by existing tests that create consumers and readers.

Documentation

  • doc-required
    (Your PR needs to update docs and you will update later)

  • doc-not-needed
    (Please explain why)
    Internal change, no API change.

This fix comes from the fork of this repository that ClickHouse uses for its Pulsar table engine (ClickHouse/ClickHouse#64184).

🤖 Generated with Claude Code

`generateRandomName` mutated a process-wide `std::mt19937` without synchronization, so
subscriptions created concurrently by different clients raced on it (undefined behavior,
reported by TSan). Use thread-local generator state instead. `ConnectionPool::generateRandomIndex`
is called from both the caller and the executor threads, so guard its generator with a mutex.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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