Skip to content

[fix][client] Fix data race on the close counter of ClientImpl - #626

Open
groeneai wants to merge 1 commit into
apache:mainfrom
groeneai:fix-close-counter-race
Open

groeneai wants to merge 1 commit into
apache:mainfrom
groeneai:fix-close-counter-race

Conversation

@groeneai

Copy link
Copy Markdown

Motivation

ClientImpl::closeAsync passes a shared counter of the open producers and consumers to handleClose, which the close callback of each of them invokes from the executor threads. The counter is a plain int, read and decremented there without synchronization, so concurrent close callbacks race on it (reported by ThreadSanitizer). A lost decrement leaves the counter above zero, and then the client is never shut down and the close callback is never invoked.

Modifications

The counter is a std::atomic<int>. handleClose decrements it with a compare-and-swap loop, and only the call that takes it from one to zero (or finds it at zero, when there was nothing to close) shuts the client down.

Verifying this change

  • Make sure that the change passes the CI checks.

This change is already covered by existing tests, such as ClientTest.testCloseClient and the other tests that close a client with open producers and consumers.

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

TSan detected a data race in ClickHouse CI (test_storage_pulsar, amd_tsan):
ClientImpl::handleClose decrements *numberOfOpenHandlers from the client's
executor threads while ClientImpl::closeAsync reads and decrements the same
plain int from the calling thread without synchronization.

Make the shared counter std::atomic<int> and decrement it with a CAS loop in
handleClose so that exactly one call observes the counter reaching zero and
proceeds to shut the client down.
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