Skip to content

Commit

Permalink
fix a bug that could cause "Helium.dispatch" to panic
Browse files Browse the repository at this point in the history
  • Loading branch information
DuodenumL committed Feb 25, 2022
1 parent 4aa9a48 commit 34bdfd0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions discovery/helium/helium.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ func (h *Helium) start(ctx context.Context) {

func (h *Helium) dispatch(status types.ServiceStatus) {
h.subs.Range(func(k, v interface{}) bool {
defer func() {
if err := recover(); err != nil {
log.Errorf(context.TODO(), "[dispatch] dispatch %s failed, err: %v", k, err)
}
}()
c, ok := v.(chan<- types.ServiceStatus)
if !ok {
log.Error("[WatchServiceStatus] failed to cast channel from map")
Expand Down
30 changes: 30 additions & 0 deletions discovery/helium/helium_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,33 @@ func TestHelium(t *testing.T) {
close(chAddr)
close(chStatus)
}

func TestPanic(t *testing.T) {
chAddr := make(chan []string)

store := &storemocks.Store{}
store.On("ServiceStatusStream", mock.Anything).Return(chAddr, nil)

grpcConfig := types.GRPCConfig{
ServiceDiscoveryPushInterval: time.Duration(1) * time.Second,
}
service := New(grpcConfig, store)

for i := 0; i < 1000; i++ {
go func() {
chStatus := make(chan types.ServiceStatus)
uuid := service.Subscribe(chStatus)
time.Sleep(time.Second)
service.Unsubscribe(uuid)
close(chStatus)
}()
}

go func() {
for i := 0; i < 1000; i++ {
chAddr <- []string{"hhh", "hhh2"}
}
}()

time.Sleep(5 * time.Second)
}

0 comments on commit 34bdfd0

Please sign in to comment.