Aias00 opened a new pull request, #992:
URL: https://github.com/apache/dubbo-go-pixiu/pull/992

   ## Summary
   
   Fixes nil map panic in KafkaConsumerFacade and adds concurrent access 
protection.
   
   ## Problem
   
   1. **Nil map panic**: `KafkaConsumerFacade.Subscribe()` panics on first use 
because `consumerManager` map is never initialized. Writing to a nil map in Go 
causes an immediate runtime panic.
   
   2. **Concurrent access without synchronization**: `consumerManager` is 
read/written from multiple goroutines (`Subscribe` and `checkConsumerIsAlive`) 
without any synchronization, causing potential data races.
   
   3. **Goroutine leak**: In `checkConsumerIsAlive`, the `<-f.done` and 
implicit context cancellation cases did not properly return, causing goroutines 
to continue looping after shutdown signals.
   
   ## Changes
   
   ### kafka_facade.go
   - Initialize `consumerManager` with `make(map[string]func())` in 
`NewKafkaConsumerFacade`
   - Add `sync.RWMutex` to protect all `consumerManager` read/write operations
   - Add `return` in `<-f.done` case of `checkConsumerIsAlive` to prevent 
goroutine leak
   - Add `<-ctx.Done()` case with proper cleanup to handle context cancellation
   - Use double-check pattern (`if cancel, ok := f.consumerManager[key]; ok`) 
before calling cancel and deleting from map
   
   ### kafka_facade_test.go (new file)
   - `TestKafkaConsumerFacadeConsumerManagerInitialized`: verifies map 
initialization prevents nil map panic
   - `TestKafkaConsumerFacadeConcurrentAccess`: verifies mutex protection for 
concurrent read/write
   - `TestGetConsumerManagerKey`: verifies key generation function determinism
   - `TestNewKafkaConsumerFacadeConstructorInitializesMap`: verifies 
constructor properly initializes consumerManager
   
   ## Testing
   
   All tests pass with `-race` detector enabled:
   ```
   === RUN   TestKafkaConsumerFacadeConsumerManagerInitialized
   --- PASS
   === RUN   TestKafkaConsumerFacadeConcurrentAccess
   --- PASS
   === RUN   TestGetConsumerManagerKey
   --- PASS
   === RUN   TestNewKafkaConsumerFacadeConstructorInitializesMap
   --- PASS
   PASS
   ok  github.com/apache/dubbo-go-pixiu/pkg/client/mq  2.328s
   ```
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to