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

Cron expression scheduling might be triggered 1 millisecond earlier #30666

Closed
yfei-z opened this issue Jun 14, 2023 · 1 comment
Closed

Cron expression scheduling might be triggered 1 millisecond earlier #30666

yfei-z opened this issue Jun 14, 2023 · 1 comment
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: regression A bug that is also a regression
Milestone

Comments

@yfei-z
Copy link

yfei-z commented Jun 14, 2023

Affects: 6.0.8


The issue just like the title described, after I look into the code, I found the calculation of the delay time in ReschedulingRunnable might cause the issue.

Duration initialDelay = Duration.between(this.triggerContext.getClock().instant(), this.scheduledExecutionTime);
this.currentFuture = this.executor.schedule(this, initialDelay.toMillis(), TimeUnit.MILLISECONDS);

Above calculation of initialDelay keeps the nanosecond part, and truncate them at last in initialDelay.toMillis(). The next execution time of cron expression accurate to seconds, and current time has nanoseconds, that will make the initialDelay.toMillis() 1 millisecond less. If the execution fast enough the current time that is obtained in task will earlier than next execution time of cron expression.

Compare to 5.3.27

long initialDelay = this.scheduledExecutionTime.getTime() - this.triggerContext.getClock().millis();
this.currentFuture = this.executor.schedule(this, initialDelay, TimeUnit.MILLISECONDS);

The nanoseconds has been truncated before the calculation of duration, that will make sure you can't got a unexpected current time.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jun 14, 2023
@jhoeller
Copy link
Contributor

jhoeller commented Jun 14, 2023

I suppose calling schedule with initialDelay.toNanos() and TimeUnit.NANOSECONDS would fix this. We could do that for all our schedule calls in ConcurrentTaskScheduler and ThreadPoolTaskScheduler as well, generally using nanosecond precision then.

@jhoeller jhoeller added type: regression A bug that is also a regression in: core Issues in core modules (aop, beans, core, context, expression) and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jun 14, 2023
@jhoeller jhoeller self-assigned this Jun 14, 2023
@jhoeller jhoeller added this to the 6.0.10 milestone Jun 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: regression A bug that is also a regression
Projects
None yet
Development

No branches or pull requests

3 participants