Skip to content

Cron scheduled methods may be executed a fraction of a second too early #29735

Closed as not planned
@thuri

Description

@thuri

Affects: 6.0.3


Hi Spring Team,

i was just updating a project of mine from spring-boot 2.7.6 to spring-boot 3.0.0 and may have found an issue with cron scheduled beans. I'm quite sure that this has something to do with spring-context and not Spring Boot itself, so i decided to create this issue here.

When the application ran with 2.7.6 the cron expression "0 */1 * * * *" executed the annotated method always some faction of a second AFTER second 0 of each minute.

With 3.0.1 the job may be executed "around" the second 0 of each minute. That means, that a job may be executed at second 59 + some fraction of a second.

See the following log output:

2022-12-23T14:05:59.999+01:00 ERROR 5262 --- [   scheduling-1] n.l.spring6.scheduled.cron.CronJob       : now=2022-12-23T13:05:59.999691839Z now.truncated=2022-12-23T13:05:59Z

In most cases that may not be a problem but in my project i use the current time (java.time.Instant.now) to calculate the next iteration with CronExpression#nextwhich will result in the timestamp of the next minute where the second is 0. This means that the result of next is actually the timestamp on which the current execution should have been started.

I did create a small demo project to show you what I mean and to reproduce the problem if possible: https://github.com/thuri/spring6-scheduled-cron

I had a look at CronExpression source code but didn't find a change that could be the cause of this. Maybe I'll have some time to have a deeper look into this.

Regards,
Michael

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: declinedA suggestion or change that we don't feel we should currently apply

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions