From 000e780649070b4d3bfae6846c9d4cd8d8c3dd94 Mon Sep 17 00:00:00 2001 From: Omar Abdulaziz Date: Wed, 14 Feb 2024 11:57:56 +0200 Subject: [PATCH] add mutex for a safe accessing on memory between goroutines: --- pkg/diagnostics/diagnostics.go | 11 +++++++++-- pkg/perf/networkhealth/networkhealth.go | 5 +++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/pkg/diagnostics/diagnostics.go b/pkg/diagnostics/diagnostics.go index 392df8989..46e602b59 100644 --- a/pkg/diagnostics/diagnostics.go +++ b/pkg/diagnostics/diagnostics.go @@ -75,16 +75,22 @@ func (m *DiagnosticsManager) GetSystemDiagnostics(ctx context.Context) (Diagnost } var wg sync.WaitGroup - for _, module := range Modules { + var mut sync.Mutex + var hasError bool + for _, module := range Modules { wg.Add(1) go func(module string) { defer wg.Done() report := m.getModuleStatus(ctx, module) + + mut.Lock() + defer mut.Unlock() + results.ZosModules[module] = report if report.Err != nil { - results.SystemStatusOk = false + hasError = true } }(module) @@ -92,6 +98,7 @@ func (m *DiagnosticsManager) GetSystemDiagnostics(ctx context.Context) (Diagnost wg.Wait() + results.SystemStatusOk = !hasError results.Online = m.isOnline(ctx) return results, nil diff --git a/pkg/perf/networkhealth/networkhealth.go b/pkg/perf/networkhealth/networkhealth.go index 12e91a521..7b8e45e47 100644 --- a/pkg/perf/networkhealth/networkhealth.go +++ b/pkg/perf/networkhealth/networkhealth.go @@ -54,11 +54,16 @@ func (t *NetworkHealthTask) Run(ctx context.Context) (interface{}, error) { reports := []ServiceStatus{} var wg sync.WaitGroup + var mut sync.Mutex for _, serviceUrl := range servicesUrl { wg.Add(1) go func(serviceUrl string) { defer wg.Done() report := getNetworkReport(ctx, serviceUrl) + + mut.Lock() + defer mut.Unlock() + reports = append(reports, report) }(serviceUrl) }