Skip to content

jedis subscription not listening to thread interrupted signal #3725

@SoilChang

Description

@SoilChang

Issue Description

jedisCluster.ssubscribe(pubSub, channel) this line blocks forever.

 jedisCluster.ssubscribe(
         pubSub,
         "channel".getBytes()
 );

because, it runs in a loop keeps pulling message afterwards. see JedisShardedPubSubBase::process(). Here is a simplified snippet

do {
      Object reply = client.getUnflushedObject();

       // process the message 
} while (isSubscribed());

As a result, this line jedisCluster.ssubscribe(..) is usually invoked in a separate thread, for example a ThreadPool, to avoid blocking the main thread.

During termination(shutdown), people will usually to shutdown the threadPool, which internally will interrupt the threads.

But the process loop does not check for interrupted flag. Hence it is unaware that the thread that is running the process() call has been signalled to stop.

Therefore, the process loop keeps running while it should have exited upon interruption.

Expected behavior

JedisShardedPubSubBase::process should exit the "do while" loop upon thread interrupt.

Actual behavior

JedisShardedPubSubBase::process keeps running despite thread interrupt

Steps to reproduce:

  1. start a JedisCluster
  2. call JedisCluster::ssubscribe(...) on a different thread. can be in a thread pool or otherwise.
  3. interrupt the thread that's running JedisCluster::ssubscribe(...)
  4. You should expect to see consumption continues still, which means the thread will still be stuck at this line: JedisCluster::ssubscribe(...)

Redis / Jedis Configuration

This is not related to configuration.

Jedis version:

5.0.0

Redis version:

latest

Java version:

openjdk 17

Suggested Fix:

do {
      Object reply = client.getUnflushedObject();

       // process the message 
} while (!Thread.currentThread.isInterrupted() && isSubscribed());

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions