Skip to content

Commit

Permalink
fixed race conditions and nil panic on calling registerBroker after c…
Browse files Browse the repository at this point in the history
…lose
  • Loading branch information
roblaszczak committed Nov 2, 2019
1 parent a5a3dd2 commit 93f4001
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
8 changes: 8 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,9 @@ func (client *client) Close() error {
}

func (client *client) Closed() bool {
client.lock.RLock()
defer client.lock.RUnlock()

return client.brokers == nil
}

Expand Down Expand Up @@ -529,6 +532,11 @@ func (client *client) RefreshCoordinator(consumerGroup string) error {
// in the brokers map. It returns the broker that is registered, which may be the provided broker,
// or a previously registered Broker instance. You must hold the write lock before calling this function.
func (client *client) registerBroker(broker *Broker) {
if client.brokers == nil {
Logger.Printf("cannot register broker #%d at %s, client already closed", broker.ID(), broker.Addr())
return
}

if client.brokers[broker.ID()] == nil {
client.brokers[broker.ID()] = broker
Logger.Printf("client/brokers registered new broker #%d at %s", broker.ID(), broker.Addr())
Expand Down
28 changes: 16 additions & 12 deletions consumer_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,12 +417,6 @@ func (c *consumerGroup) leave() error {
}

func (c *consumerGroup) handleError(err error, topic string, partition int32) {
select {
case <-c.closed:
return
default:
}

if _, ok := err.(*ConsumerError); !ok && topic != "" && partition > -1 {
err = &ConsumerError{
Topic: topic,
Expand All @@ -431,13 +425,23 @@ func (c *consumerGroup) handleError(err error, topic string, partition int32) {
}
}

if c.config.Consumer.Return.Errors {
select {
case c.errors <- err:
default:
}
} else {
if !c.config.Consumer.Return.Errors {
Logger.Println(err)
return
}

c.lock.Lock()
defer c.lock.Unlock()

select {
case <-c.closed:
return
default:
}

select {
case c.errors <- err:
default:
}
}

Expand Down

0 comments on commit 93f4001

Please sign in to comment.