Repository navigation
Fix sending buffer race by using proper reference counting - #1394
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR introduces proper reference counting for buffers to fix a race condition, adds a Prometheus gauge for tracking in-flight sending buffers, and cleans up buffer usage on connection teardown.
- Added
Retain(),Release(), andRefCnt()to theBufferinterface and updated buffer pool logic - Switched to an
internal.BuffersPooland updated producer/connection code to callRetain/Releaseappropriately - Added
pulsar_client_sending_buffers_countmetric and tests to validate buffer retention and cleanup
Reviewed Changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| pulsar/producer_test.go | Added tests for buffer retention and metrics but missing imports and synchronization |
| pulsar/producer_partition.go | Switched to internal.BuffersPool, added reference counting, updated atomic connection storage |
| pulsar/internal/metrics.go | Registered new sending buffers gauge |
| pulsar/internal/connection.go | Ensured buffers are released on various connection shutdown paths |
| pulsar/internal/buffer.go | Implemented BuffersPool interface and atomic ref counting |
| pulsar/internal/batch_builder.go | Updated batch builder to use new GetBuffer signature |
| pulsar/consumer_multitopic_test.go | Adjusted dummy connection max message size |
Comments suppressed due to low confidence (4)
pulsar/producer_test.go:21
- Several packages used in the new tests (http, strings, strconv, sync, and testify/assert) are not imported, which will cause compilation errors. Please add the missing imports.
"bufio"
pulsar/producer_partition.go:1753
- Storing a pointer to the local
connvariable will lead to storing a pointer to a stack frame that goes out of scope. Use a heap-allocated value or revert toatomic.Valueto store the interface value safely.
p.conn.Store(&conn)
pulsar/producer_test.go:2709
- [nitpick] Starting the Prometheus HTTP server in a goroutine without synchronization can cause the test to attempt metrics fetch before the server is ready. Consider adding a start-up barrier or retry logic to ensure the server is listening before proceeding.
go func() {
pulsar/consumer_multitopic_test.go:357
- [nitpick] Using a raw literal for the max message size can be unclear. Consider defining a named constant (e.g.,
const testMaxMsgSize = 5 * 1024 * 1024) to improve readability.
return 5 * 1024 * 1024 // 5MB
RobertIndie
marked this pull request as draft
July 18, 2025 03:56
RobertIndie
marked this pull request as ready for review
July 18, 2025 05:33
BewareMyPower
approved these changes
Jul 21, 2025
RobertIndie
pushed a commit
that referenced
this pull request
Aug 21, 2025
…#1408) #1394 introduces the `pulsar_client_sending_buffers_count` metric to track how many buffers are allocated for send purpose and not put back to the pool. However, unlike other metrics, this metric is not client level, so it cannot be attached with `CustomMetricsLabels` in client options. When a send buffer was not put back to the pool, it means the `Release` method is not called due to some reason. Changing this metric to client level could help locate which client has triggered this bug in an application that has many client instances from different businesses. Here is an example metric when I configured `CustomMetricsLabels: map[string]string{"key": "value"}` after this change ``` pulsar_client_sending_buffers_count{client="go",key="value"} 1 ```
weiliu1031
added a commit
to weiliu1031/milvus
that referenced
this pull request
Dec 2, 2025
Upgrade apache/pulsar-client-go from v0.15.1 to v0.17.0 to fix send buffer race condition (apache/pulsar-client-go#1394) Signed-off-by: Wei Liu <[email protected]>
This was referenced Dec 2, 2025
weiliu1031
added a commit
to weiliu1031/milvus
that referenced
this pull request
Dec 2, 2025
issue: milvus-io#46006 pr: milvus-io#46007 Upgrade apache/pulsar-client-go from v0.15.1 to v0.17.0 to fix send buffer race condition (apache/pulsar-client-go#1394) Signed-off-by: Wei Liu <[email protected]>
sre-ci-robot
pushed a commit
to milvus-io/milvus
that referenced
this pull request
Dec 2, 2025
issue: #46006 Upgrade apache/pulsar-client-go from v0.15.1 to v0.17.0 to fix send buffer race condition (apache/pulsar-client-go#1394) Signed-off-by: Wei Liu <[email protected]>
sre-ci-robot
pushed a commit
to milvus-io/milvus
that referenced
this pull request
Dec 2, 2025
issue: #46006 pr: #46007 Upgrade apache/pulsar-client-go from v0.15.1 to v0.17.0 to fix send buffer race condition (apache/pulsar-client-go#1394) Signed-off-by: Wei Liu <[email protected]>
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.
Fixes #1371
Motivation
The issue is that the buffer is sent to the connection when the data is flushed at this line:
pulsar-client-go/pulsar/producer_partition.go
Line 910 in 0ab28c2
Because of this, both the data request in the connection and the pending item hold onto this buffer.
However, if the pending item fails due to a timeout or the producer closing, the buffer is returned to the buffer pool at this line:
pulsar-client-go/pulsar/producer_partition.go
Line 1747 in 0ab28c2
Yet, the connection still has this buffer. When the connection tries to handle the data request, it will try to use a buffer that has already been returned. This causes a data race.
This fix implements proper reference counting to ensure buffers are only returned to the pool when all references are released, preventing the data race.
Modifications
Retain()andRelease()methods to theBufferinterfaceatomic.Int64pulsar_client_sending_buffers_countto show the sending buffers count.Does this pull request potentially affect one of the following parts:
If
yeswas chosen, please highlight the changesDocumentation