Hi, I just produce the warning for clarity. sorry for the huge message
================== WARNING: DATA RACE Write at 0x00c0002d6480 by goroutine 48: runtime.mapdelete_faststr() /usr/local/go/src/runtime/map_faststr.go:297 +0x0 nfnrfapi/services/manag.UpdateNFInstance() /home/xxx/go/src/nfnrfapi/services/manag/api_nf_instance_id_document_Update.go:48 +0x46d net/http.HandlerFunc.ServeHTTP() /usr/local/go/src/net/http/server.go:2042 +0x51 nfnrfapi/common.RecoverHandler.func1() /home/xxx/go/src/nfnrfapi/common/middlewares.go:56 +0xe6 net/http.HandlerFunc.ServeHTTP() /usr/local/go/src/net/http/server.go:2042 +0x51 github.com/NYTimes/gziphandler.GzipHandlerWithOpts.func1.1() /home/xxx/go/pkg/mod/github.com/!n!y!times/gziphandler@v1.1.1/gzip.go:336 +0x45e net/http.HandlerFunc.ServeHTTP() /usr/local/go/src/net/http/server.go:2042 +0x51 github.com/gorilla/mux.(*Router).ServeHTTP() /home/xxx/go/pkg/mod/github.com/gorilla/mux@v1.8.0/mux.go:210 +0x132 nfnrfapi/common.Logging.func1.1() /home/xxx/go/src/nfnrfapi/common/middlewares.go:42 +0xcf net/http.HandlerFunc.ServeHTTP() /usr/local/go/src/net/http/server.go:2042 +0x51 nfnrfapi/common.Tracing.func1.1() /home/xxx/go/src/nfnrfapi/common/middlewares.go:26 +0x454 net/http.HandlerFunc.ServeHTTP() /usr/local/go/src/net/http/server.go:2042 +0x51 net/http.serverHandler.ServeHTTP() /usr/local/go/src/net/http/server.go:2843 +0xca net/http.initALPNRequest.ServeHTTP() /usr/local/go/src/net/http/server.go:3415 +0x104 net/http.(*initALPNRequest).ServeHTTP() <autogenerated>:1 +0xa6 net/http.Handler.ServeHTTP-fm() /usr/local/go/src/net/http/server.go:87 +0x64 golang.org/x/net/http2.(*serverConn).runHandler() /home/xxx/go/pkg/mod/golang.org/x/net@v0.0.0-20201031054903-ff519b6c9102/http2/server.go:2152 +0xc5 Previous read at 0x00c0002d6480 by goroutine 18: runtime.mapiterinit() /usr/local/go/src/runtime/map.go:797 +0x0 nfnrfapi/services/manag.PollingWorker() /home/xxx/go/src/nfnrfapi/services/manag/api_nf_instance_id_document_Update.go:111 +0x604 Goroutine 48 (running) created at: golang.org/x/net/http2.(*serverConn).processHeaders() /home/xxx/go/pkg/mod/golang.org/x/net@v0.0.0-20201031054903-ff519b6c9102/http2/server.go:1882 +0x924 golang.org/x/net/http2.(*serverConn).processFrame() /home/xxx/go/pkg/mod/golang.org/x/net@v0.0.0-20201031054903-ff519b6c9102/http2/server.go:1410 +0x41e golang.org/x/net/http2.(*serverConn).processFrameFromReader() /home/xxx/go/pkg/mod/golang.org/x/net@v0.0.0-20201031054903-ff519b6c9102/http2/server.go:1368 +0x7d9 golang.org/x/net/http2.(*serverConn).serve() /home/xxx/go/pkg/mod/golang.org/x/net@v0.0.0-20201031054903-ff519b6c9102/http2/server.go:869 +0x14fc golang.org/x/net/http2.(*Server).ServeConn() /home/xxx/go/pkg/mod/golang.org/x/net@v0.0.0-20201031054903-ff519b6c9102/http2/server.go:472 +0xdc4 golang.org/x/net/http2.ConfigureServer.func1() /home/xxx/go/pkg/mod/golang.org/x/net@v0.0.0-20201031054903-ff519b6c9102/http2/server.go:298 +0x11e net/http.(*conn).serve() /usr/local/go/src/net/http/server.go:1834 +0x1d5b Goroutine 18 (running) created at: main.main() /home/xxx/go/src/nfnrfapi/main.go:41 +0x2c6 ================== ================== WARNING: DATA RACE Write at 0x00c0000f7a88 by goroutine 48: nfnrfapi/services/manag.UpdateNFInstance() /home/xxx/go/src/nfnrfapi/services/manag/api_nf_instance_id_document_Update.go:50 +0x53c net/http.HandlerFunc.ServeHTTP() /usr/local/go/src/net/http/server.go:2042 +0x51 nfnrfapi/common.RecoverHandler.func1() /home/xxx/go/src/nfnrfapi/common/middlewares.go:56 +0xe6 net/http.HandlerFunc.ServeHTTP() /usr/local/go/src/net/http/server.go:2042 +0x51 github.com/NYTimes/gziphandler.GzipHandlerWithOpts.func1.1() /home/xxx/go/pkg/mod/github.com/!n!y!times/gziphandler@v1.1.1/gzip.go:336 +0x45e net/http.HandlerFunc.ServeHTTP() /usr/local/go/src/net/http/server.go:2042 +0x51 github.com/gorilla/mux.(*Router).ServeHTTP() /home/xxx/go/pkg/mod/github.com/gorilla/mux@v1.8.0/mux.go:210 +0x132 nfnrfapi/common.Logging.func1.1() /home/xxx/go/src/nfnrfapi/common/middlewares.go:42 +0xcf net/http.HandlerFunc.ServeHTTP() /usr/local/go/src/net/http/server.go:2042 +0x51 nfnrfapi/common.Tracing.func1.1() /home/xxx/go/src/nfnrfapi/common/middlewares.go:26 +0x454 net/http.HandlerFunc.ServeHTTP() /usr/local/go/src/net/http/server.go:2042 +0x51 net/http.serverHandler.ServeHTTP() /usr/local/go/src/net/http/server.go:2843 +0xca net/http.initALPNRequest.ServeHTTP() /usr/local/go/src/net/http/server.go:3415 +0x104 net/http.(*initALPNRequest).ServeHTTP() <autogenerated>:1 +0xa6 net/http.Handler.ServeHTTP-fm() /usr/local/go/src/net/http/server.go:87 +0x64 golang.org/x/net/http2.(*serverConn).runHandler() /home/xxx/go/pkg/mod/golang.org/x/net@v0.0.0-20201031054903-ff519b6c9102/http2/server.go:2152 +0xc5 Previous read at 0x00c0000f7a88 by goroutine 18: nfnrfapi/services/manag.PollingWorker() /home/xxx/go/src/nfnrfapi/services/manag/api_nf_instance_id_document_Update.go:112 +0x1c4 Goroutine 48 (running) created at: golang.org/x/net/http2.(*serverConn).processHeaders() /home/xxx/go/pkg/mod/golang.org/x/net@v0.0.0-20201031054903-ff519b6c9102/http2/server.go:1882 +0x924 golang.org/x/net/http2.(*serverConn).processFrame() /home/xxx/go/pkg/mod/golang.org/x/net@v0.0.0-20201031054903-ff519b6c9102/http2/server.go:1410 +0x41e golang.org/x/net/http2.(*serverConn).processFrameFromReader() /home/xxx/go/pkg/mod/golang.org/x/net@v0.0.0-20201031054903-ff519b6c9102/http2/server.go:1368 +0x7d9 golang.org/x/net/http2.(*serverConn).serve() /home/xxx/go/pkg/mod/golang.org/x/net@v0.0.0-20201031054903-ff519b6c9102/http2/server.go:869 +0x14fc golang.org/x/net/http2.(*Server).ServeConn() /home/xxx/go/pkg/mod/golang.org/x/net@v0.0.0-20201031054903-ff519b6c9102/http2/server.go:472 +0xdc4 golang.org/x/net/http2.ConfigureServer.func1() /home/xxx/go/pkg/mod/golang.org/x/net@v0.0.0-20201031054903-ff519b6c9102/http2/server.go:298 +0x11e net/http.(*conn).serve() /usr/local/go/src/net/http/server.go:1834 +0x1d5b Goroutine 18 (running) created at: main.main() /home/xxx/go/src/nfnrfapi/main.go:41 +0x2c6 On Friday, November 27, 2020 at 8:14:48 AM UTC+2 Afriyie Abraham Kwabena wrote: > Hi, > > Yes from the warning it prints the handler function below and after google > searching what i did was to mutex.Lock and Unlock the handler function and > no warning again. > However, my google search people mention of using channels instead of > mutex. What I would like to ask is, using mutex OK and if not the best way > of solving it, how can i use > channels in this case. > > Handler function below: > > func UpdateNFInstance(response http.ResponseWriter, request *http.Request) > { > > mu.Lock() > defer mu.Unlock() > > > var ( > localVarHTTPMethod = http.MethodPatch > ctx = context.Background() > > patchItems model.PatchItem > ) > > id := config.GetIdFromRequest(request) > > if request.Method == localVarHTTPMethod { > > if request.Header.Get("Content-Type") != > "application/json-patch+json" { > common.WriteError(response, > common.ErrStatusUnsupportedMediaTypePatch) > return > > } > > err := json.NewDecoder(request.Body).Decode(&patchItems) > if err != nil { > common.WriteError(response, common.ErrBadRequest) > return > } > > defer request.Body.Close() > > delete(idtime, id) // delete value in map if exit > > idtime[id] = time.Now().Unix() > > nfProfile, err := config.Conf.FindNFInstanceProfileById(ctx, id) > if err != nil { > common.WriteError(response, common.ErrNotFound) > return > } > > if patchItems.Path == "/nfStatus" { > > nfProfile.NfStatus = patchItems.Value > nfProfile.LoadTimeStamp = time.Now().String() > > success := config.Conf.UpdateNFInstanceHeartBeatNfStatus(ctx, > nfProfile, id) > if !success { > common.WriteError(response, common.ErrInternalServer) > return > } > > if request.URL.Scheme != "" { > scheme = request.URL.Scheme > } > response.Header().Set("Content-Location", > scheme+"://"+request.Host+request.URL.Path) > response.Header().Set("Response-Desc", "Success") > common.RespondWith3gppJSONPatchJSON(response, > http.StatusNoContent, nil) > > } else { > // patchItems.Path == "/load" information > v, err := strconv.Atoi(patchItems.Value) > if err != nil { > config.Logrus.Errorf("Failed to convert Patch Item Value > string to integer: %s", err.Error()) > } > > nfProfile.Load = int32(v) > nfProfile.LoadTimeStamp = time.Now().String() > > success := config.Conf.UpdateNFInstanceHeartBeatLoad(ctx, > nfProfile, id) > if !success { > common.WriteError(response, common.ErrInternalServer) > return > } > > if request.URL.Scheme != "" { > scheme = request.URL.Scheme > } > response.Header().Set("Content-Location", > scheme+"://"+request.Host+request.URL.Path) > response.Header().Set("Response-Desc", "Success") > common.RespondWith3gppJSONPatchJSON(response, > http.StatusNoContent, nil) > > } > } else { > common.WriteError(response, common.ErrMethodNotAllowed) > return > } > } > > > BR > Abraham > > On Friday, November 27, 2020 at 6:38:52 AM UTC+2 Shulhan wrote: > >> >> >> > On 27 Nov 2020, at 07.06, Afriyie Abraham Kwabena <afriyie...@gmail.com> >> wrote: >> > >> > Hi, >> > >> > Am experiencing data race warning with my pollingworker function below >> even though when i build with the -race flag i do not get any error. Any >> help? >> > >> >> Usually when you got data race warning it will print the line that cause >> read and write race condition. Pay attention to both lines and I think you >> will find the cause. >> >> It's kind of hard to figure it out only through a single function. It >> could be in config.Conf or in function that manipulate idtime outside the >> pollingworker. >> >> -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/2b9c5058-1f7c-47a1-8d6c-ea74bb565b4cn%40googlegroups.com.