Skip to content

Commit

Permalink
handle data race
Browse files Browse the repository at this point in the history
  • Loading branch information
ykadowak committed Aug 1, 2023
1 parent 7436411 commit 004fb04
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion pkg/agent/core/ngt/handler/grpc/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package grpc
import (
"context"
"fmt"
"sync"

"github.com/vdaas/vald/apis/grpc/v1/payload"
"github.com/vdaas/vald/apis/grpc/v1/vald"
Expand Down Expand Up @@ -208,22 +209,33 @@ func (s *server) StreamListObject(_ *payload.Object_List_Request, stream vald.Ob
}

Check warning on line 209 in pkg/agent/core/ngt/handler/grpc/object.go

View check run for this annotation

Codecov / codecov/patch

pkg/agent/core/ngt/handler/grpc/object.go#L208-L209

Added lines #L208 - L209 were not covered by tests
}()

errs := make([]error, 0, s.ngt.Len())
var (
mu sync.Mutex
emu sync.Mutex
errs = make([]error, 0, s.ngt.Len())
)
s.ngt.ListObjectFunc(ctx, func(uuid string, _ uint32, _ int64) bool {
vec, ts, err := s.ngt.GetObject(uuid)
if err != nil {
st := status.CreateWithNotFound(fmt.Sprintf("failed to get object with uuid: %s", uuid), err)

mu.Lock()
err := stream.Send(&payload.Object_List_Response{
Payload: &payload.Object_List_Response_Status{
Status: st.Proto(),
},
})
mu.Unlock()

if err != nil {
emu.Lock()
errs = append(errs, err)
emu.Unlock()
}
return true

Check warning on line 235 in pkg/agent/core/ngt/handler/grpc/object.go

View check run for this annotation

Codecov / codecov/patch

pkg/agent/core/ngt/handler/grpc/object.go#L220-L235

Added lines #L220 - L235 were not covered by tests
}

mu.Lock()
err = stream.Send(&payload.Object_List_Response{
Payload: &payload.Object_List_Response_Vector{
Vector: &payload.Object_Vector{
Expand All @@ -233,8 +245,12 @@ func (s *server) StreamListObject(_ *payload.Object_List_Request, stream vald.Ob
},
},
})
mu.Unlock()

if err != nil {
emu.Lock()
errs = append(errs, err)
emu.Unlock()
}
return true
})
Expand Down

0 comments on commit 004fb04

Please sign in to comment.