~shulhan/karajo

6129e3dd2af80f3fa8198ef25683a9547cd1a514 — Shulhan 1 year, 2 months ago fa47770
all: fix possible data race on HTTP API Environment

This is the fix for 14b1aa3f5fef.
2 files changed, 26 insertions(+), 0 deletions(-)

M environment.go
M karajo.go
M environment.go => environment.go +23 -0
@@ 510,3 510,26 @@ func (env *Environment) loadJobHttpd() (err error) {
	}
	return nil
}

func (env *Environment) lockAllJob() {
	var job *Job
	for _, job = range env.Jobs {
		job.Lock()
	}

	var jobHttp *JobHttp
	for _, jobHttp = range env.HttpJobs {
		jobHttp.Lock()
	}
}
func (env *Environment) unlockAllJob() {
	var job *Job
	for _, job = range env.Jobs {
		job.Unlock()
	}

	var jobHttp *JobHttp
	for _, jobHttp = range env.HttpJobs {
		jobHttp.Unlock()
	}
}

M karajo.go => karajo.go +3 -0
@@ 329,7 329,10 @@ func (k *Karajo) apiEnvironment(epr *libhttp.EndpointRequest) (resbody []byte, e
	res.Code = http.StatusOK
	res.Data = k.env

	k.env.lockAllJob()
	resbody, err = json.Marshal(res)
	k.env.unlockAllJob()

	if err != nil {
		return nil, fmt.Errorf(`%s: %w`, logp, err)
	}