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() { func (channel *Channel) Destroy() {
if channel.destroyed { if channel.IsDestroyed() {
return return
} }
channel.withMutex(func() {
channel.destroyed = true channel.destroyed = true
channel.members = make(ClientSet) channel.members = make(ClientSet)
})
channel.server.channels.Remove(channel) channel.server.channels.Remove(channel)
} }
@ -70,7 +72,7 @@ func (channel *Channel) Reply(reply Reply) {
func (channel *Channel) receiveCommands(commands <-chan ChannelCommand) { func (channel *Channel) receiveCommands(commands <-chan ChannelCommand) {
for command := range commands { for command := range commands {
if channel.destroyed { if channel.IsDestroyed() {
if DEBUG_CHANNEL { if DEBUG_CHANNEL {
log.Printf("%s → %s %s dropped", command.Source(), channel, command) log.Printf("%s → %s %s dropped", command.Source(), channel, command)
} }
@ -92,9 +94,15 @@ func IsPrivMsg(reply Reply) bool {
return strReply.code == "PRIVMSG" 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) { func (channel *Channel) receiveReplies(replies <-chan Reply) {
for reply := range replies { for reply := range replies {
if channel.destroyed { if channel.IsDestroyed() {
if DEBUG_CHANNEL { if DEBUG_CHANNEL {
log.Printf("%s ← %s %s dropped", channel, reply.Source(), reply) 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 { for _, chname := range m.channels {
channel := s.channels[chname] server.mutex.Lock()
channel := server.channels[chname]
server.mutex.Unlock()
if channel == nil { if channel == nil {
m.Client().Reply(ErrNoSuchChannel(s, channel.name)) m.Client().Reply(ErrNoSuchChannel(server, channel.name))
continue continue
} }