Skip to content

JedisCluster throws NullPointerException when maxAttempts is set to 0 #4185

@shivincible

Description

@shivincible

When maxAttempts is set to 0 in JedisCluster, it leads to an unhandled NullPointerException during command execution. The cause is that lastException remains null, and Throwable.addSuppressed(null) is called, which is not allowed.

This could be avoided with a guard clause to validate the value of maxAttempts or by checking for null before calling addSuppressed.

Reproduction

JedisCluster jedis = new JedisCluster(
    new HostAndPort("localhost", 6379),
    2000,       // connection timeout
    2000,       // soTimeout
    0,          // maxAttempts
    null,       // password
    new JedisPoolConfig()
);
jedis.set("key", "value");  // NPE thrown

Stacktrace
java.lang.NullPointerException: Cannot suppress a null exception. at java.util.Objects.requireNonNull(Objects.java:235) at java.lang.Throwable.addSuppressed(Throwable.java:1074) at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:158)

Cause
In RetryableCommandExecutor.java, the code attempts to call lastException.addSuppressed(ex) without checking whether lastException is null.

e.addSuppressed(lastException); // lastException is null when maxAttempts = 0

This was tried with Jedis 3.9.0, but the same issue is also present in the latest master branch.
Relevant line in the current code:
https://github.com/redis/jedis/blob/master/src/main/java/redis/clients/jedis/executors/RetryableCommandExecutor.java#L71

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions