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

fix: panic: runtime error: integer divide by zero #504

Closed
wants to merge 2 commits into from
Closed

fix: panic: runtime error: integer divide by zero #504

wants to merge 2 commits into from

Conversation

BranislavLazic
Copy link

"Do" function triggers "doCommon" function that sets "inScheduleChain" to false. In "getCurrentJob", if "inScheduleChain" is false, then a new job will be added to a slice and the current job will be the last element of the slice. Therefore, after "Do" function runs, "StartAt" will try to obtain the current job. Since "inScheduleChain" is false, a new job with interval 0 will be added and returned as the current job in "StartAt" function. After this interval 0 will produce "duration" to be 0 and "panic: runtime error: integer divide by zero".

What does this do?

Which issue(s) does this PR fix/relate to?

Resolves #486

List any changes that modify/break current functionality

Have you included tests for your changes?

Yes

Did you document any new/modified functionality?

  • Updated example_test.go
  • Updated README.md

Notes

"Do" function triggers "doCommon" function that sets "inScheduleChain" to false. In "getCurrentJob", if "inScheduleChain" is false, then a new job will be added to a slice and the current job will be the last element of the slice. Therefore, after "Do" function runs, "StartAt" will try to obtain the current job. Since "inScheduleChain" is false, a new job with interval 0 will be added and returned as the current job in "StartAt" function. After this interval 0 will produce "duration" to be 0 and "panic: runtime error: integer divide by zero".
Comment on lines +761 to +762
_, _ = s.Every(5).Seconds().Do(task)
s.StartAt(time.Now().Add(10 * time.Second))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @BranislavLazic thanks for taking a stab at this. The solution you have here actually changes / breaks the design of the scheduling chain. The inScheduleChain bool is to prevent this type of activity as it causes more pain confusion to users not knowing which job exactly is being changed.

The chain is started anytime a method is called on the scheduler and terminated with Do is called.

The actual issue where a divide by zero can happen is when startAt is called and the time is in the past. I think just wrapping the potential divide by zero case in a check if the denominator is zero first will be the way to solve it. I'll open a PR

@JohnRoesler JohnRoesler closed this Jun 1, 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 this pull request may close these issues.

[BUG] - panic: runtime error: integer divide by zero
2 participants