From fd265956a84b1c16ce6c3148009f5f87bf590113 Mon Sep 17 00:00:00 2001 From: "fengyun.rui" Date: Tue, 5 Sep 2023 03:18:05 +0800 Subject: [PATCH] fix: data race for get jobs map (#558) --- scheduler.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/scheduler.go b/scheduler.go index d011f61b..a8ddc295 100644 --- a/scheduler.go +++ b/scheduler.go @@ -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 + } + return jobs } func (s *Scheduler) jobsMap() map[uuid.UUID]*Job {