Skip to content

Commit

Permalink
nsqd: SUB: fix ephemeral channel race condition with reconnecting cli…
Browse files Browse the repository at this point in the history
…ent.

It's possible for a client reconnecting quickly and subscribed to an ephemeral channel to race with nsqd's cleanup of said ephemeral channel, as documented in nsqio/go-nsq#206.

Fixes nsqio#883
  • Loading branch information
lilinzhe committed May 2, 2017
1 parent 71734e9 commit 5bcb654
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions nsqd/protocol_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,9 +615,20 @@ func (p *protocolV2) SUB(client *clientV2, params [][]byte) ([]byte, error) {
return nil, err
}

topic := p.ctx.nsqd.GetTopic(topicName)
channel := topic.GetChannel(channelName)
channel.AddClient(client.ID, client)
for {
topic := p.ctx.nsqd.GetTopic(topicName)
if topic.Exiting() {
time.Sleep(1 * time.Microsecond)
continue
}
channel := topic.GetChannel(channelName)
if channel.Exiting() {
time.Sleep(1 * time.Microsecond)
continue
}
channel.AddClient(client.ID, client)
break
}

atomic.StoreInt32(&client.State, stateSubscribed)
client.Channel = channel
Expand Down

0 comments on commit 5bcb654

Please sign in to comment.