Skip to content
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

jedis subscription not listening to thread interrupted signal #3725

Closed
SoilChang opened this issue Feb 19, 2024 · 3 comments · Fixed by #3726
Closed

jedis subscription not listening to thread interrupted signal #3725

SoilChang opened this issue Feb 19, 2024 · 3 comments · Fixed by #3726
Labels

Comments

@SoilChang
Copy link
Contributor

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());
@sazzad16 sazzad16 added the bug label Feb 19, 2024
@sazzad16
Copy link
Collaborator

@SoilChang As you have already suggested a fix, would you like to craft a PR?

@SoilChang
Copy link
Contributor Author

glad to submit a PR. coming up.

@SoilChang
Copy link
Contributor Author

#3726 PR is here

sazzad16 added a commit that referenced this issue Feb 19, 2024
- adding thread interrupted check in `JedisShardedPubSubBase::process`
- also in `JedisPubSubBase::process`

Fixes #3725 

---------

Co-authored-by: charles.chang <[email protected]>
Co-authored-by: M Sazzadul Hoque <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants