Skip to content

Commit c9faf3c

Browse files
artembilangaryrussell
authored andcommitted
Fix LockRegLeaderIn for interrupted Thread.sleep (#2455)
* Fix LockRegLeaderIn for interrupted Thread.sleep https://build.spring.io/browse/INT-FATS5IC-517 If current thread is interrupted, the `Thread.sleep()` interrupts immediately. In the catch block of the main loop in the `LockRegistryLeaderInitiator` we have such a dangerous `sleep()` and don't restart election in this candidate any more * Move `Thread.sleep()` to else after checking the current thread for interrupted state * Remove `LongRunningIntegrationTest` rule from the `RedisLockRegistryLeaderInitiatorTests` since it now works much faster after proper `busy-wait` handling **Cherry-pick to master** * Ignore interruption on the sleep i catch and move on with loop
1 parent 7ab024d commit c9faf3c

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

spring-integration-core/src/main/java/org/springframework/integration/support/leader/LockRegistryLeaderInitiator.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -397,10 +397,6 @@ else if (acquired) {
397397
}
398398
// The lock was broken and we are no longer leader
399399
handleRevoked();
400-
if (isRunning()) {
401-
// Give it a chance to elect some other leader.
402-
Thread.sleep(LockRegistryLeaderInitiator.this.busyWaitMillis);
403-
}
404400
}
405401

406402
if (e instanceof InterruptedException || Thread.currentThread().isInterrupted()) {
@@ -417,9 +413,21 @@ else if (acquired) {
417413
}
418414
return null;
419415
}
420-
else if (logger.isDebugEnabled()) {
421-
logger.debug("Error acquiring the lock for " + this.context +
422-
". " + (isRunning() ? "Retrying..." : ""), e);
416+
else {
417+
if (isRunning()) {
418+
// Give it a chance to elect some other leader.
419+
try {
420+
Thread.sleep(LockRegistryLeaderInitiator.this.busyWaitMillis);
421+
}
422+
catch (InterruptedException e1) {
423+
// Ignore interruption and let it to be caught on the next cycle.
424+
Thread.currentThread().interrupt();
425+
}
426+
}
427+
if (logger.isDebugEnabled()) {
428+
logger.debug("Error acquiring the lock for " + this.context +
429+
". " + (isRunning() ? "Retrying..." : ""), e);
430+
}
423431
}
424432
}
425433
}

spring-integration-redis/src/test/java/org/springframework/integration/redis/leader/RedisLockRegistryLeaderInitiatorTests.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import org.springframework.integration.redis.util.RedisLockRegistry;
3838
import org.springframework.integration.support.leader.LockRegistryLeaderInitiator;
3939
import org.springframework.integration.test.rule.Log4j2LevelAdjuster;
40-
import org.springframework.integration.test.support.LongRunningIntegrationTest;
4140
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;
4241

4342
/**
@@ -49,9 +48,6 @@
4948
*/
5049
public class RedisLockRegistryLeaderInitiatorTests extends RedisAvailableTests {
5150

52-
@Rule
53-
public LongRunningIntegrationTest longTests = new LongRunningIntegrationTest();
54-
5551
@Rule
5652
public Log4j2LevelAdjuster adjuster =
5753
Log4j2LevelAdjuster.trace()

0 commit comments

Comments
 (0)