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

ArithmeticException: long overflow on @Scheduled(fixedDelay = Long.MAX_VALUE) #30754

Closed
ipalbeniz opened this issue Jun 26, 2023 · 4 comments
Closed
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: regression A bug that is also a regression
Milestone

Comments

@ipalbeniz
Copy link

Affects: 6.0.10


I was scheduling a task using @Scheduled(fixedDelay = Long.MAX_VALUE) in Spring version 6.0.9. The intention was to schedule the task only once. However, after upgrading to version 6.0.10, the application fails to start with the following exception:

java.lang.ArithmeticException: long overflow
	at java.base/java.lang.Math.multiplyExact(Math.java:1004)
	at java.base/java.time.Duration.toNanos(Duration.java:1250)
	at org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler.scheduleWithFixedDelay(ThreadPoolTaskScheduler.java:420)

It seems that the method ThreadPoolTaskScheduler.scheduleWithFixedDelay() was changed in commit 3415b04, which is referenced by issue #30666. This change modifies the delay time unit from milliseconds to nanoseconds. I suspect that this alteration may be the cause of the exception.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jun 26, 2023
@sbrannen sbrannen added type: regression A bug that is also a regression in: core Issues in core modules (aop, beans, core, context, expression) labels Jun 26, 2023
@jhoeller jhoeller removed the status: waiting-for-triage An issue we've not yet triaged or decided on label Jun 26, 2023
@jhoeller jhoeller self-assigned this Jun 26, 2023
@jhoeller jhoeller added this to the 6.0.11 milestone Jun 26, 2023
@jhoeller
Copy link
Contributor

Technically, there was always a risk of an overflow there but previously you could only trigger that via fixedDelay = Long.MAX_VALUE, timeUnit = TimeUnit.SECONDS or the like, whereas now it can be triggered with the default milliseconds unit. We'll see what we can do about this, falling back to millisecond or even second precision when necessary.

@jhoeller
Copy link
Contributor

There is a more idiomatic solution: We can use JDK 11's long TimeUnit.convert(Duration) method which prevents arithmetic overflows, turning them into Long.MAX_VALUE instead. After all, such large values just mean arbitrary long out anyway, precision does not really matter there. Whereas for shorter delays and periods, nano precision is certainly beneficial.

@ipalbeniz
Copy link
Author

Thank you so much @jhoeller !

@cmuchinsky
Copy link

@jhoeller I'm seeing a similar overflow exception coming from https://github.com/spring-projects/spring-framework/blob/main/spring-context/src/main/java/org/springframework/scheduling/concurrent/ReschedulingRunnable.java#L83, should the same fix be applied to that as well?

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

5 participants