Skip to content
This repository has been archived by the owner on Nov 10, 2020. It is now read-only.

race-conditions in service.go and session.go #33

Open
shamork opened this issue Mar 17, 2016 · 0 comments
Open

race-conditions in service.go and session.go #33

shamork opened this issue Mar 17, 2016 · 0 comments

Comments

@shamork
Copy link

shamork commented Mar 17, 2016

every operation of *Session.Cmsg in session.go is like this

this.mu.Lock()
defer this.mu.Unlock()
blabla~~~

but in service.go line 237-241

// Publish will message if WillFlag is set. Server side only.
if !this.client && this.sess.Cmsg.WillFlag() {
glog.Infof("(%s) service/stop: connection unexpectedly closed. Sending Will.", this.cid())
this.onPublish(this.sess.Will)
}

there is no lock when operating this.sess.Cmsg

My opinion is:

  1. make Session.Cmsg private
  2. adding the lines below to session.go, and using the functions instead of this.sess.Cmsg
  3. if possible, change this.mu from sync.Mutex to sync.RWMutex

func (this *Session) WillFlag() bool {
this.mu.Lock()
defer this.mu.Unlock()
return this.cmsg.WillFlag()
}

func (this *Session) SetWillFlag(v bool) {
this.mu.Lock()
defer this.mu.Unlock()
this.cmsg.SetWillFlag(v)
}

func (this *Session) CleanSession() bool {
this.mu.Lock()
defer this.mu.Unlock()
return this.cmsg.CleanSession()
}

Another place of this problem in memtopics.go line 128-130

func (this *memTopics) Close() error {
this.sroot = nil
this.rroot = nil
return nil
}

shoud change to

func (this *memTopics) Close() error {
this.smu.Lock()
this.sroot = nil
this.smu.Unlock()
this.rmu.Lock()
this.rroot = nil
this.rmu.Unlock()
}

I'll Pull Request later when I complete my coding

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant