Skip to content

Function deregisterBroker Delete a Wrong Broker Without Close Broker #199

Open
@byt3n33dl3

Description

@byt3n33dl3

Heyo, cool works, and awesome

is related to a potential memory management bug in the deregisterBroker function of a Kafka client implemented in Go. Let’s break it down step by step:

Here’s the function in question:

func (client *client) deregisterBroker(broker *Broker) {
	client.lock.Lock()
	defer client.lock.Unlock()

	_, ok := client.brokers[broker.ID()]
	if ok {
		Logger.Printf("client/brokers deregistered broker #%d at %s", broker.ID(), broker.Addr())
		delete(client.brokers, broker.ID())
		return
	}
	if len(client.seedBrokers) > 0 && broker == client.seedBrokers[0] {
		client.deadSeeds = append(client.deadSeeds, broker)
		client.seedBrokers = client.seedBrokers[1:]
	}
}

What’s the issue?

Problem Source: Using a Pointer (*Broker) as Parameter

  • The parameter broker *Broker is a pointer.
  • Even if two brokers have the same ID(), they may be different pointers in memory.
// Different pointers, same broker ID
b1 := &Broker{id: 1}
b2 := &Broker{id: 1} // same ID but different memory

The Bug:

  • client.brokers is a map from broker.ID() to *Broker. The function uses the ID to delete from the map:
    • delete(client.brokers, broker.ID())
  • This removes the key from the map, but not necessarily the correct pointer if two brokers have the same ID.
  • Also, there's no call to broker.Close() or similar cleanup so the memory (or any active connections/resources) held by the broker aren't released.

Summary of What’s wrong? Deregistering a broker by ID may remove the wrong broker (due to pointer mismatch), leaving the actual broker unclosed in memory. Cause Broker is passed as a pointer; broker with same ID might be a different object in memory.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions