#4752·jedis

JedisSentinelPool hands out connections to the demoted master after failover

Author: insaf021Created Sep 26, 2026Updated Sep 26, 2026

JedisSentinelPool.getResource() guards against handing out a connection that still points at the demoted master by comparing currentHostMaster with jedis.getClient().getHostAndPort() (JedisSentinelPool.java:315-323), and JedisFactory.validateObject makes the same comparison for testOnBorrow/testWhileIdle. Both checks are dead. Connection.getHostAndPort() returns ((DefaultJedisSocketFactory) socketFactory).getHostAndPort(), every pooled Jedis shares the factory's single DefaultJedisSocketFactory, and initMaster re-points that one instance through factory.setHostAndPort(currentHostMaster) on +switch-master. After the switch every connection, including one whose socket was opened against the old master, reports the new master, so master.equals(connection) is always true. The guard was written when each Jedis carried its own host and port; with the shared factory it compares the factory with itself.

Scenario

  1. A Jedis is borrowed from a JedisSentinelPool and is still checked out while Sentinel fails over. initMaster only clears idle instances, as its own comment notes.
  2. The holder calls close(). The instance goes back to the idle pool with its socket still connected to the demoted node.
  3. The next getResource() returns that instance. Writes on it get READONLY once the old node has been reconfigured, or, while it is partitioned and still writable, land on a node Sentinel will resync and wipe; reads return stale data. READONLY is a JedisDataException, so the connection is not marked broken and keeps being reused.

Any JedisSentinelPool user with a connection in flight during a failover is exposed, including Spring Data Redis, which builds a JedisSentinelPool for its Sentinel configuration.

Reproduction against the repo's sentinel-failover docker env (mymasterfailover, nodes 127.0.0.1:6384/6385, sentinel :26381):

java
Set<String> sentinels = Collections.singleton("localhost:26381");
GenericObjectPoolConfig<Jedis> config = new GenericObjectPoolConfig<>();
config.setMaxTotal(1);
try (JedisSentinelPool pool = new JedisSentinelPool("mymasterfailover", sentinels, config, 1000,
    "foobared", 0)) {
  HostAndPort oldMaster = pool.getCurrentHostMaster();            // 127.0.0.1:6384
  Jedis borrowed = pool.getResource();                            // socket -> :6384

  try (Jedis s1 = new Jedis("localhost", 26381); Jedis s2 = new Jedis("localhost", 26381)) {
    JedisSentinelTestUtil.waitForNewPromotedMaster("mymasterfailover", s1, s2);
  }
  while (oldMaster.equals(pool.getCurrentHostMaster())) {         // listener handled +switch-master
    Thread.sleep(50);
  }
  HostAndPort newMaster = pool.getCurrentHostMaster();            // 127.0.0.1:6385

  borrowed.close();                                               // back in the pool, socket still on :6384
  try (Jedis reborrowed = pool.getResource()) {
    System.out.println(reborrowed.getClient().getHostAndPort());  // 127.0.0.1:6385
    System.out.println(reborrowed.info("server"));                // ... tcp_port:6384 ...
  }
}

Output on master (b75c784, JDK 21): getHostAndPort() prints 127.0.0.1:6385, INFO server prints tcp_port:6384. The pool log shows Created JedisSentinelPool to master at 127.0.0.1:6385 before the re-borrow, so the listener did its part; getResource() simply cannot tell the stale socket apart. With testOnBorrow enabled the outcome is the same, because validateObject reads both addresses from the same factory and then gets PONG from the old node.

The fix I would propose is for Connection to record the factory's target when it opens the socket and to return that from getHostAndPort(). That makes both the getResource() guard and validateObject effective again without touching the pool, and it is what the method's javadoc already promises. Happy to open the PR.