Skip to content

Calling a method on an ConsumerRecords before performing a null check #3810

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ghgh415263 opened this issue Mar 19, 2025 · 2 comments · Fixed by #3811
Closed

Calling a method on an ConsumerRecords before performing a null check #3810

ghgh415263 opened this issue Mar 19, 2025 · 2 comments · Fixed by #3811

Comments

@ghgh415263
Copy link
Contributor

ghgh415263 commented Mar 19, 2025

In what version(s) of Spring for Apache Kafka are you seeing this issue?

current main branch

Describe the bug

KafkaTestUtils.java

If received can be null, a NullPointerException may occur in the logger received.count(). (line 371)
But, as far as I know, 'consumer.poll method' does not return null.

public static <K, V> ConsumerRecords<K, V> getRecords(Consumer<K, V> consumer, long timeout, int minRecords) {
		logger.debug("Polling...");
		Map<TopicPartition, List<ConsumerRecord<K, V>>> records = new HashMap<>();
		long remaining = timeout;
		int count = 0;
		do {
			long t1 = System.currentTimeMillis();
			ConsumerRecords<K, V> received = consumer.poll(Duration.ofMillis(remaining));
			logger.debug(() -> "Received: " + received.count() + ", "
					+ received.partitions().stream()
					.flatMap(p -> received.records(p).stream())
					// map to same format as send metadata toString()
					.map(r -> r.topic() + "-" + r.partition() + "@" + r.offset())
					.collect(Collectors.toList()));
			if (received == null) {
				throw new IllegalStateException("null received from consumer.poll()");
			}
			if (minRecords < 0) {
				return received;
			}
			else {
				count += received.count();
				received.partitions().forEach(tp -> {
					List<ConsumerRecord<K, V>> recs = records.computeIfAbsent(tp, part -> new ArrayList<>());
					recs.addAll(received.records(tp));
				});
				remaining -= System.currentTimeMillis() - t1;
			}
		}
		while (count < minRecords && remaining > 0);
		return new ConsumerRecords<>(records);
	}

How about moving it right below line 370 or removing it?

if (received == null) {
	throw new IllegalStateException("null received from consumer.poll()");
}

I appreciate you taking the time to read this

To Reproduce

Steps to reproduce the behavior.

Expected behavior

A clear and concise description of what you expected to happen.

Sample

A link to a GitHub repository with a minimal, reproducible, sample.

Reports that include a sample will take priority over reports that do not.
At times, we may require a sample, so it is good to try and include a sample up front.

@ghgh415263 ghgh415263 changed the title Calling a method on an object before performing a null check Calling a method on an ConsumerRecords before performing a null check Mar 19, 2025
@sobychacko
Copy link
Contributor

I think it's ok to move that logging after the conditional statement. Feel free to send a PR. Thanks!

ghgh415263 added a commit to ghgh415263/spring-kafka that referenced this issue Mar 19, 2025
Move the logging statement after the conditional statement.

Signed-off-by: kjy1994 <[email protected]>
@ghgh415263
Copy link
Contributor Author

Thank you for the review

I just sent the PR

I think it's ok to move that logging after the conditional statement. Feel free to send a PR. Thanks!

sobychacko pushed a commit that referenced this issue Mar 19, 2025
…n KafkaTestUtils (#3811)

Fixes: 3810

Issue link: #3810

Signed-off-by: kjy1994 <[email protected]>

**Auto-cherry-pick to `3.3.x` & `3.2.x`**
spring-builds pushed a commit that referenced this issue Mar 19, 2025
…n KafkaTestUtils (#3811)

Fixes: 3810

Issue link: #3810

Signed-off-by: kjy1994 <[email protected]>

(cherry picked from commit e85c88c)
spring-builds pushed a commit that referenced this issue Mar 19, 2025
…n KafkaTestUtils (#3811)

Fixes: 3810

Issue link: #3810

Signed-off-by: kjy1994 <[email protected]>

(cherry picked from commit e85c88c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants