fix(mod/lock): Use dedicated channel to shutdown goroutine properly

If closeChan is available and stopWatchChan was closed by defer
select selects a single channel randomly. This causes a panic sending to closed
channel.
This commit is contained in:
TANABE Ken-ichi 2014-01-22 19:42:07 +09:00
parent a417782151
commit 4c2942f9f9

View File

@ -146,14 +146,17 @@ func (h *handler) ttlKeepAlive(k string, value string, ttl int, stopChan chan bo
func (h *handler) watch(keypath string, index int, closeChan <- chan bool) error { func (h *handler) watch(keypath string, index int, closeChan <- chan bool) error {
// Wrap close chan so we can pass it to Client.Watch(). // Wrap close chan so we can pass it to Client.Watch().
stopWatchChan := make(chan bool) stopWatchChan := make(chan bool)
stopWrapChan := make(chan bool)
go func() { go func() {
select { select {
case <- closeChan: case <- closeChan:
stopWatchChan <- true stopWatchChan <- true
case <- stopWrapChan:
stopWatchChan <- true
case <- stopWatchChan: case <- stopWatchChan:
} }
}() }()
defer close(stopWatchChan) defer close(stopWrapChan)
for { for {
// Read all nodes for the lock. // Read all nodes for the lock.