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

[BUG] - Stop func might not wait for job to finish #391

Closed
hiroebe opened this issue Oct 26, 2022 · 0 comments · Fixed by #392
Closed

[BUG] - Stop func might not wait for job to finish #391

hiroebe opened this issue Oct 26, 2022 · 0 comments · Fixed by #392
Labels
bug Something isn't working

Comments

@hiroebe
Copy link

hiroebe commented Oct 26, 2022

Describe the bug

(*Scheduler).Stop func should wait for all jobs to finish due to its doc, but it possibly returns before all jobs finished.

To Reproduce

(This is a concurrent issue and a bit hard to reproduce)

  1. Add time.Sleep(time.Second) after the lines below (to raise the concurrent issue on purpose)

    gocron/executor.go

    Lines 47 to 48 in e315112

    case f := <-e.jobFunctions:
    runningJobsWg.Add(1)

  2. Write some code like this:

func main() {
	s := gocron.NewScheduler(time.UTC)
	s.Every(1).Second().Do(func() {
		fmt.Println("Job started")
		time.Sleep(5 * time.Second)
		fmt.Println("Job finished")
	})
	go func() {
		time.Sleep(3 * time.Second)
		fmt.Println("Trying to stop")
		s.Stop()
		fmt.Println("Stopped")
	}()
	s.StartAsync()

	time.Sleep(10 * time.Second)
}

The output on my machine is:

Job started
Job started
Trying to stop
Stopped
Job started
Job finished
Job finished
Job finished

As we can see, Stop() func returns before the jobs finished. Expected behavior is that Stopped log is shown at the end.

Version

latest main: e315112

Expected behavior

Stop() func always waits for the jobs to finish.

Additional context

N/A

@hiroebe hiroebe added the bug Something isn't working label Oct 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant