3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-12-22 18:52:41 +01:00

fix a race

This commit is contained in:
Jeremy Latt 2014-02-15 20:20:37 -08:00
parent 213afc0481
commit 0f6ee63e6e
2 changed files with 18 additions and 8 deletions

View File

@ -51,12 +51,14 @@ func NewChannel(s *Server, name string) *Channel {
}
func (channel *Channel) Destroy() {
if channel.destroyed {
if channel.IsDestroyed() {
return
}
channel.withMutex(func() {
channel.destroyed = true
channel.members = make(ClientSet)
})
channel.server.channels.Remove(channel)
}
@ -70,7 +72,7 @@ func (channel *Channel) Reply(reply Reply) {
func (channel *Channel) receiveCommands(commands <-chan ChannelCommand) {
for command := range commands {
if channel.destroyed {
if channel.IsDestroyed() {
if DEBUG_CHANNEL {
log.Printf("%s → %s %s dropped", command.Source(), channel, command)
}
@ -92,9 +94,15 @@ func IsPrivMsg(reply Reply) bool {
return strReply.code == "PRIVMSG"
}
func (channel *Channel) IsDestroyed() bool {
channel.mutex.Lock()
defer channel.mutex.Unlock()
return channel.destroyed
}
func (channel *Channel) receiveReplies(replies <-chan Reply) {
for reply := range replies {
if channel.destroyed {
if channel.IsDestroyed() {
if DEBUG_CHANNEL {
log.Printf("%s ← %s %s dropped", channel, reply.Source(), reply)
}

View File

@ -364,12 +364,14 @@ func (m *JoinCommand) HandleServer(s *Server) {
}
}
func (m *PartCommand) HandleServer(s *Server) {
func (m *PartCommand) HandleServer(server *Server) {
for _, chname := range m.channels {
channel := s.channels[chname]
server.mutex.Lock()
channel := server.channels[chname]
server.mutex.Unlock()
if channel == nil {
m.Client().Reply(ErrNoSuchChannel(s, channel.name))
m.Client().Reply(ErrNoSuchChannel(server, channel.name))
continue
}