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: data race for get jobs map #558

Merged
merged 1 commit into from
Sep 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,13 @@ func (s *Scheduler) Jobs() []*Job {

// JobsMap returns a map of job uuid to job
func (s *Scheduler) JobsMap() map[uuid.UUID]*Job {
return s.jobsMap()
s.jobsMutex.RLock()
defer s.jobsMutex.RUnlock()
jobs := make(map[uuid.UUID]*Job, len(s.jobs))
for id, job := range s.jobs {
jobs[id] = job
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the data race on the map object or the jobs inside the map?

Copy link
Author

Choose a reason for hiding this comment

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

😁 A coroutine calls the Jobs api to obtain jobs, and then reads them. Another coroutine modifies the jobs dictionary? In this way, golang will cause error messages for concurrent map reading and writing.

}
return jobs
}

func (s *Scheduler) jobsMap() map[uuid.UUID]*Job {
Expand Down
Loading