Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DeregisterInstance() data race #180

Closed
undom opened this issue Jan 14, 2021 · 1 comment
Closed

DeregisterInstance() data race #180

undom opened this issue Jan 14, 2021 · 1 comment

Comments

@undom
Copy link
Contributor

undom commented Jan 14, 2021

Using nacos-sdk-go v1.0.3

Data race log:

WARNING: DATA RACE
Read at 0x00c0004a4718 by goroutine 80:
  github.com/nacos-group/nacos-sdk-go/clients/naming_client.(*BeatReactor).sendInstanceBeat()
    /github.com/nacos-group/nacos-sdk-go/clients/naming_client/beat_reactor.go:82 +0xcb

Previous write at 0x00c0004a4718 by goroutine 112:
  [failed to restore the stack]

Goroutine 80 (running) created at:
  github.com/nacos-group/nacos-sdk-go/clients/naming_client.(*BeatReactor).AddBeatInfo()
      /github.com/nacos-group/nacos-sdk-go/clients/naming_client/beat_reactor.go:64 +0x39d
  github.com/nacos-group/nacos-sdk-go/clients/naming_client.(*NamingClient).RegisterInstance()
/github.com/nacos-group/nacos-sdk-go/clients/naming_client/naming_client.go:122 +0x4bf
  gitlab..../nacos.RegisterService()
     /nacos/service.go:26 +0x195
  gitlab.../internal/common/server.Register.func1()
    /internal/common/server/server.go:30 +0x83
  gitlab.../internal/util.GoroutineWithRecoverNotContext.func1()
     /internal/util/recover.go:34 +0x5a

Source code

func (br *BeatReactor) RemoveBeatInfo(serviceName string, ip string, port uint64) {
	logger.Infof("remove beat: %s@%s:%d from beat map", serviceName, ip, port)
	k := buildKey(serviceName, ip, port)
	data, exist := br.beatMap.Get(k)
	if exist {
		beatInfo := data.(*model.BeatInfo)
		beatInfo.Stopped = true // write there
	}
	br.beatMap.Remove(k)
}

func (br *BeatReactor) sendInstanceBeat(k string, beatInfo *model.BeatInfo) {
	for {
		br.beatThreadSemaphore.Acquire()
		//如果当前实例注销,则进行停止心跳
		if beatInfo.Stopped { // read there
			logger.Infof("instance[%s] stop heartBeating", k)
			br.beatThreadSemaphore.Release()
			return
		}

		//进行心跳通信
		beatInterval, err := br.serviceProxy.SendBeat(*beatInfo)
		if err != nil {
			logger.Errorf("beat to server return error:%+v", err)
			br.beatThreadSemaphore.Release()
			t := time.NewTimer(beatInfo.Period)
			<-t.C
			continue
		}
		if beatInterval > 0 {
			beatInfo.Period = time.Duration(time.Millisecond.Nanoseconds() * beatInterval)
		}

		br.beatRecordMap.Set(k, util.CurrentMillis())
		br.beatThreadSemaphore.Release()

		t := time.NewTimer(beatInfo.Period)
		<-t.C
	}
}

It's possible DeregisterInstance() write Stopped and sendInstanceBeat read Stopped at the same time.
Maybe use chan or atomic replace the bool Stopped.

@binbin0325
Copy link
Member

correlation pr:#181

@binbin0325 binbin0325 reopened this Jan 18, 2021
lzp0412 added a commit that referenced this issue Feb 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants