fix(eventbridge): stop a scheduled rule's timer when its rule is gone (#5462)

* fix(eventbridge): stop a scheduled rule's timer when its rule is gone

* fix(eventbridge): stop only the timer whose tick found its rule gone
This commit is contained in:
Niels Kootstra authored and GitHub committed 2026-10-11 14:45:14 -04:00
1 parent 2e4cfe7ac5
commit d6f93abb18
2 files changed
+65 -6

No files matched your search

@@ -100,9 +100,9 @@ public class RuleScheduler implements Resettable {
Supplier<ScheduleData> dataSupplier) {
long intervalMs = ScheduleExpressionParser.parseRateToMillis(scheduleExpr);
tick(dataSupplier);
long timerId = vertx.setPeriodic(intervalMs, id -> tick(dataSupplier));
long timerId = vertx.setPeriodic(intervalMs, id -> tick(ruleArn, id, dataSupplier));
scheduleContexts.put(ruleArn, new ScheduleContext(timerId, scheduleExpr));
tick(ruleArn, timerId, dataSupplier);
LOG.debugv("Started rate scheduler for rule {0} with interval {1}ms", ruleArn, intervalMs);
}
@@ -124,7 +124,7 @@ public class RuleScheduler implements Resettable {
}
long timerId = vertx.setTimer(delayMs, id -> {
tick(dataSupplier);
tick(ruleArn, id, dataSupplier);
ScheduleContext current = scheduleContexts.get(ruleArn);
if (current != null && current.timerId() == id) {
scheduleCronFire(ruleArn, scheduleExpr, dataSupplier, current);
@@ -150,12 +150,14 @@ public class RuleScheduler implements Resettable {
}
}
private void tick(Supplier<ScheduleData> dataSupplier) {
private void tick(String ruleArn, long timerId, Supplier<ScheduleData> dataSupplier) {
ScheduleData data = dataSupplier.get();
if (data == null || data.rule == null) {
LOG.debugv("Rule no longer exists, stopping scheduler");
stopScheduler(data != null && data.rule != null ? data.rule.getArn() : null);
LOG.debugv("Rule {0} no longer exists, stopping its timer", ruleArn);
vertx.cancelTimer(timerId);
// A rule recreated under the same ARN may already have its own timer; leave that one.
scheduleContexts.computeIfPresent(ruleArn, (arn, ctx) -> ctx.timerId() == timerId ? null : ctx);
return;
}
@@ -19,6 +19,7 @@ import java.util.concurrent.atomic.AtomicReference;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.mock;
@@ -113,6 +114,62 @@ class RuleSchedulerTest {
assertFalse(scheduler.isRunning(RULE_ARN), "a stop racing the re-arm must not leave the new timer recorded");
}
@Test
void ruleGoneWithoutDeleteRuleStopsItsTimerOnTheNextTick() {
Vertx vertx = mock(Vertx.class);
ArgumentCaptor<Handler<Long>> fire = timerHandlerCaptor();
when(vertx.setPeriodic(anyLong(), fire.capture())).thenReturn(1L);
// A storage reset or restore removes the rule without going through DeleteRule, so the
// scheduler only learns about it when its timer next fires.
AtomicReference<Rule> currentRule = new AtomicReference<>(enabledRule());
RuleScheduler scheduler = newScheduler(vertx, new StoppingInvoker(() -> { }));
scheduler.startScheduler(RULE_ARN, "rate(1 minute)", () -> toScheduleData(currentRule.get()));
currentRule.set(null);
fire.getValue().handle(1L);
verify(vertx).cancelTimer(1L);
assertFalse(scheduler.isRunning(RULE_ARN), "a rule that no longer exists must not keep its timer");
}
@Test
void ruleAlreadyGoneWhenItsRateSchedulerStartsLeavesNoTimer() {
Vertx vertx = mock(Vertx.class);
when(vertx.setPeriodic(anyLong(), any())).thenReturn(1L);
RuleScheduler scheduler = newScheduler(vertx, new StoppingInvoker(() -> { }));
// DeleteRule landed between the rule being stored and its scheduler starting.
scheduler.startScheduler(RULE_ARN, "rate(1 minute)", () -> toScheduleData(null));
assertFalse(scheduler.isRunning(RULE_ARN), "a rule that no longer exists must not get a timer");
}
@Test
void oldTickFindingItsRuleGoneLeavesTheRecreatedRulesTimerRunning() {
Vertx vertx = mock(Vertx.class);
ArgumentCaptor<Handler<Long>> fire = timerHandlerCaptor();
when(vertx.setPeriodic(anyLong(), fire.capture())).thenReturn(1L, 2L);
RuleScheduler scheduler = newScheduler(vertx, new StoppingInvoker(() -> { }));
AtomicReference<Rule> oldRule = new AtomicReference<>(enabledRule());
// The old timer looks its rule up and finds it deleted; before it acts on that, PutRule
// recreates a rule with the same ARN, which stops the old scheduler and starts a new one.
scheduler.startScheduler(RULE_ARN, "rate(1 minute)", () -> {
Rule current = oldRule.get();
if (current == null) {
scheduler.stopScheduler(RULE_ARN);
scheduler.startScheduler(RULE_ARN, "rate(1 minute)", () -> toScheduleData(enabledRule()));
}
return toScheduleData(current);
});
oldRule.set(null);
fire.getAllValues().get(0).handle(1L);
assertTrue(scheduler.isRunning(RULE_ARN), "the recreated rule must keep its timer");
}
@SuppressWarnings("unchecked")
private static ArgumentCaptor<Handler<Long>> timerHandlerCaptor() {
return ArgumentCaptor.forClass(Handler.class);