Skip to content

[fix][client] Avoid NPE in Consumer.hasMessageAvailable() before calling receive() method#35

Open
oneby-wang wants to merge 2 commits into
masterfrom
fix_has_message_available_npe_fix
Open

[fix][client] Avoid NPE in Consumer.hasMessageAvailable() before calling receive() method#35
oneby-wang wants to merge 2 commits into
masterfrom
fix_has_message_available_npe_fix

Conversation

@oneby-wang
Copy link
Copy Markdown
Owner

@oneby-wang oneby-wang commented May 23, 2026

Fixes: apache#25837

Motivation

Calling Consumer#hasMessageAvailable() before receiving any messages can hit a NullPointerException for a normal consumer because startMessageId may be null. In that state, hasMessageAvailable() should rely on the broker response and compare the mark-delete position instead of passing a null message id into hasMoreMessages.

Modifications

  • Treat a null startMessageId as a subscription-position based consumer and fetch the latest message id response before deciding availability.
  • Keep the inclusive seek behavior limited to consumers that actually start from MessageId.latest.
  • Add coverage for hasMessageAvailable() before receive with default, earliest, and latest initial positions.

Verifying this change

This change added tests and can be verified as follows:

  • ./gradlew :pulsar-client-original:compileJava
  • ./gradlew :pulsar-broker:test --tests org.apache.pulsar.client.api.ConsumerCreationTest

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

  • Dependencies (add or upgrade a dependency)
  • The public API
  • The schema
  • The default values of configurations
  • The threading model
  • The binary protocol
  • The REST endpoints
  • The admin CLI options
  • The metrics
  • Anything that affects deployment

@oneby-wang oneby-wang changed the title [fix][client] Avoid NPE in hasMessageAvailable before receive [fix][client] Avoid NPE in Consumer.hasMessageAvailable() before calling receive() method May 23, 2026
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.

[Bug][Client] hasMessageAvailable() throws UnsupportedOperationException when consumer created via newConsumer().subscribe() before any message is read

1 participant