Skip to content

Fix sending buffer race by using proper reference counting - #1394

Merged
RobertIndie merged 10 commits into
apache:masterfrom
RobertIndie:fix-buffer-race
Jul 21, 2025
Merged

RobertIndie merged 10 commits into
apache:masterfrom
RobertIndie:fix-buffer-race

Conversation

@RobertIndie

Copy link
Copy Markdown
Member

Fixes #1371

Motivation

The issue is that the buffer is sent to the connection when the data is flushed at this line:

p._getConn().WriteData(ctx, buffer)

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:

defer buffersPool.Put(i.buffer)

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

  • Added Retain() and Release() methods to the Buffer interface
  • Implemented atomic reference counting using atomic.Int64
  • Buffers are only returned to the pool when reference count reaches zero
  • Add new metric pulsar_client_sending_buffers_count to show the sending buffers count.
  • This PR also fixes serval minor issues like data/request cleanup when closing the connection

Does this pull request potentially affect one of the following parts:

If yes was chosen, please highlight the changes

  • Dependencies (does it add or upgrade a dependency): (yes / no)
  • The public API: (yes / no)
  • The schema: (yes / no / don't know)
  • The default values of configurations: (yes / no)
  • The wire protocol: (yes / no)

Documentation

  • Does this pull request introduce a new feature? (yes / no)
  • If yes, how is the feature documented? (not applicable / docs / GoDocs / not documented)
  • If a feature is not applicable for documentation, explain why?
  • If a feature is not documented yet in this PR, please create a followup issue for adding the documentation

@RobertIndie RobertIndie self-assigned this Jul 17, 2025
@RobertIndie
RobertIndie requested a review from Copilot July 17, 2025 13:00

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.

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(), and RefCnt() to the Buffer interface and updated buffer pool logic
  • Switched to an internal.BuffersPool and updated producer/connection code to call Retain/Release appropriately
  • Added pulsar_client_sending_buffers_count metric 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 conn variable will lead to storing a pointer to a stack frame that goes out of scope. Use a heap-allocated value or revert to atomic.Value to 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 RobertIndie added this to the v0.16.0 milestone Jul 18, 2025
@RobertIndie
RobertIndie marked this pull request as draft July 18, 2025 03:56
@RobertIndie
RobertIndie marked this pull request as ready for review July 18, 2025 05:33
Comment thread pulsar/producer_partition.go Resolved
Comment thread pulsar/internal/metrics.go Resolved

@crossoverJie crossoverJie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@RobertIndie
RobertIndie merged commit 67c3d53 into apache:master Jul 21, 2025
@RobertIndie
RobertIndie deleted the fix-buffer-race branch July 21, 2025 12:31
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]>
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Buffer data race

4 participants