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

CronTrigger.getNextRunTime throws ArrayIndexOutOfBoundsException for daily schedule #274

Closed
lonsuder opened this issue Apr 22, 2023 · 1 comment · Fixed by #275
Closed

Comments

@lonsuder
Copy link

lonsuder commented Apr 22, 2023

Scheduling a job to run at the same time every day with CronTrigger (eg: "0 12 * * *") results in getNextRunTime throwing ArrayIndexOutOfBoundsException when taskScheduledTime is before the target time:

java.lang.ArrayIndexOutOfBoundsException: Array index out of range: -1
    at jakarta.enterprise.concurrent.CronTrigger.nextHour(CronTrigger.java:504)
    at jakarta.enterprise.concurrent.CronTrigger.next(CronTrigger.java:462)
    at jakarta.enterprise.concurrent.CronTrigger.getNextRunTime(CronTrigger.java:219)

The issue can be reproduced with the following code:

public static void main(String[] args) {
    var trigger = new CronTrigger("0 12 * * *", ZoneOffset.UTC);
    for (var i = 0; i < 24; i++) {
        try {
            var taskScheduledTime = ZonedDateTime.of(2023, 4, 22, i, 0, 0, 0, trigger.getZoneId());
            var next = trigger.getNextRunTime(null, taskScheduledTime);
            System.err.printf("%02d: PASS (%s)\n", i, next);
        } catch (Exception ex) {
            System.err.printf("%02d: FAIL (%s)\n", i, ex.getMessage());
        }
    }
}

This produces the following output:

00: FAIL (Array index out of range: -1)
01: FAIL (Array index out of range: -1)
02: FAIL (Array index out of range: -1)
03: FAIL (Array index out of range: -1)
04: FAIL (Array index out of range: -1)
05: FAIL (Array index out of range: -1)
06: FAIL (Array index out of range: -1)
07: FAIL (Array index out of range: -1)
08: FAIL (Array index out of range: -1)
09: FAIL (Array index out of range: -1)
10: FAIL (Array index out of range: -1)
11: FAIL (Array index out of range: -1)
12: PASS (2023-04-22T12:00Z)
13: PASS (2023-04-23T12:00Z)
14: PASS (2023-04-23T12:00Z)
15: PASS (2023-04-23T12:00Z)
16: PASS (2023-04-23T12:00Z)
17: PASS (2023-04-23T12:00Z)
18: PASS (2023-04-23T12:00Z)
19: PASS (2023-04-23T12:00Z)
20: PASS (2023-04-23T12:00Z)
21: PASS (2023-04-23T12:00Z)
22: PASS (2023-04-23T12:00Z)
23: PASS (2023-04-23T12:00Z)
njr-11 added a commit to njr-11/concurrency-api that referenced this issue Apr 24, 2023
njr-11 added a commit to njr-11/concurrency-api that referenced this issue Apr 24, 2023
@njr-11
Copy link
Contributor

njr-11 commented Apr 24, 2023

Thanks for spotting this error and reporting it with the test case to reproduce. It looks like this was caused by some code that was added for detecting Daylight Saving Time which was trying to run regardless of whether the hour matched the cron expression, leading to the exception. Pull #275 adds a fix along with a test case that is based on the one provided.

KyleAure pushed a commit to KyleAure/concurrency-api that referenced this issue Aug 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants