Skip to content

fix: the access to consumer.consumers might not be thread safe - #1494

Merged
nodece merged 8 commits into
apache:masterfrom
BewareMyPower:bewaremypower/fix-npe-ack
May 14, 2026
Merged

nodece merged 8 commits into
apache:masterfrom
BewareMyPower:bewaremypower/fix-npe-ack

Conversation

@BewareMyPower

@BewareMyPower BewareMyPower commented May 12, 2026 •

Copy link
Copy Markdown
Contributor

c.consumers can be updated by internalTopicSubscribeToPartitions, so we must access c.consumers when holding a lock. However, many code places don't follow the implicit requirement, so nil pointer dereference could happen.

This PR replaces the combination of c.checkMsgIDPartition(msgID) and c.consumers[msgID] with a wrapped method findPartitionConsumer, which retrieves the consumer with lock held. Specifically, for Seek, add an unsafe version (i.e. no lock acquired) because the lock has been acquired to check if len(c.consumers) > 1 before.

For other places that access c.consumers (all are range for loops), ensure lock is acquired. The exceptional case is the hasNext method, we have to iterate over a snapshot, otherwise, c.Unlock() must be called after wg.Wait(), which is impossible

@BewareMyPower BewareMyPower self-assigned this May 12, 2026
@BewareMyPower BewareMyPower changed the title fix: the access to ConsumerImpl.consumers might not be thread safe fix: the access to consumer.consumers might not be thread safe May 13, 2026
Comment thread pulsar/consumer_impl.go Resolved
@crossoverJie

Copy link
Copy Markdown
Member

I still noticed a few other direct accesses to c.consumers, for example:

Was the intention here to only fix the msgID-based paths, or is there a reason not to update these remaining access sites in the same PR as well?

@BewareMyPower

Copy link
Copy Markdown
Contributor Author

@crossoverJie Good finding! Let me try a better approach, maybe we need to wrap a thread-safe list

@BewareMyPower
BewareMyPower marked this pull request as draft May 13, 2026 07:47
@BewareMyPower
BewareMyPower force-pushed the bewaremypower/fix-npe-ack branch from 8f96f83 to ba0903e Compare May 13, 2026 08:00
@BewareMyPower
BewareMyPower marked this pull request as ready for review May 13, 2026 08:14
@BewareMyPower

Copy link
Copy Markdown
Contributor Author

@crossoverJie Now I replaced the trivial slice with an atomic.Value, PTAL again

Comment thread pulsar/consumer_impl.go Resolved
@BewareMyPower

Copy link
Copy Markdown
Contributor Author

I've reverted to the previous lock-based design and added comments for the issue here. PTAL again @mattisonchao

@BewareMyPower

Copy link
Copy Markdown
Contributor Author

The failed blue-green migration is not caused by this PR.


Based on my analysis, the test failure is NOT caused by the commits since master. Here's why:

Changes since master:
All 8 commits only modify pulsar/consumer_impl.go — a pure refactoring of the consumer struct's consumers slice access for thread safety (replacing direct indexing with findPartitionConsumer/unsafeFindPartitionConsumer, adding a snapshot in hasNext()). The sync.Mutex was already embedded before these changes.

Test failure root cause:
The failure occurs in consumer_partition.go (unchanged by these commits). The proxyConnection sub-test times out because during topic migration reconnection:

  1. grabConn sends a Subscribe request after getting a connection from the pool
  2. The request times out (line 81: request timed out at consumer_partition.go:2244)
  3. The timeout handler tries to close the consumer via RequestOnCnx on the same connection (consumer_partition.go:2251-2256), but this also likely fails silently
  4. On subsequent retries, GetConnection returns the same pooled connection where the broker still has consumerID=1 registered from the timed-out subscribe, resulting in: UnknownError: Consumer that failed is already present on the connection

This is a pre-existing race condition in the reconnection logic — the consumer doesn't properly handle the case where a Subscribe request reaches the broker but the response doesn't reach the client.

@nodece
nodece merged commit 0129b2d into apache:master May 14, 2026
11 of 12 checks passed
@nodece nodece added this to the 0.20.0 milestone May 14, 2026
@BewareMyPower
BewareMyPower deleted the bewaremypower/fix-npe-ack branch May 19, 2026 03:25
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.

4 participants