Skip to content

Function deregisterBroker Delete a Wrong Broker Without Close Broker #199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
byt3n33dl3 opened this issue Apr 29, 2025 · 0 comments
Open

Comments

@byt3n33dl3
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant