Skip to content
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

internal/queue/kafka/hub.go에서 panic 발생 #2

Open
cg10036 opened this issue Jun 28, 2024 · 0 comments
Open

internal/queue/kafka/hub.go에서 panic 발생 #2

cg10036 opened this issue Jun 28, 2024 · 0 comments

Comments

@cg10036
Copy link

cg10036 commented Jun 28, 2024

Hub Shutdown과 동시에 해당 함수가 실행된다면 panic이 발생합니다.

func (h *Hub) GetConsumer(consumerId int) *Consumer {
mutex := sync.Mutex{}
mutex.Lock()
defer mutex.Unlock()
return h.consumers[consumerId]
}

func (h *Hub) GetRandomConsumer() *Consumer {
mutex := sync.Mutex{}
mutex.Lock()
consumer := h.consumers[0]
mutex.Unlock()

func (h *Hub) MakeNewConsumer(ctx *context.Context) *Consumer {
mutex := sync.Mutex{}
defer mutex.Unlock()
consumer := NewConsumer(h.availID, h.messageSerializer, h.publishOnly, h.config)
mutex.Lock()

func (h *Hub) SendMessage(
item queueitem.Universal,
topic string,
consumerId int,
) error {
if !h.isRunning {
return queueerror.ErrQueueNotPrepared(topic)
}
mutex := sync.Mutex{}
defer mutex.Unlock()
mutex.Lock()



func (h *Hub) Shutdown() {
if !h.isRunning {
return
}
// Shutdown waits until all consumers are stopped.
mutex := sync.Mutex{}
shutdownWaitGroup := sync.WaitGroup{}
copyedConsumers := make(map[int]*Consumer, h.consumersMax) // To prevent deadlock while iterating over consumers
for consumerId, consumer := range h.consumers {
copyedConsumers[consumerId] = consumer
}
shutdownWaitGroup.Add(1)
go func() {
defer shutdownWaitGroup.Done()
for consumerId, consumer := range copyedConsumers {
consumer.Pause()
mutex.Lock()
delete(h.consumers, consumerId)
mutex.Unlock()
}
}()

Hub Shutdown은 함수 내부에서 뮤텍스를 생성해 사용하기 때문에 위에 언급된 함수가 사용하는 mutex와 다릅니다. 따라서 Shutdown 도중 위에 언급된 함수가 실행되면 map에서 동시 Read/Write를 하게 되고 panic이 발생합니다. 만약 동시 실행이 절대 불가능한 환경이라면 mutex를 제거하는 것이 좋아보입니다.

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