Drain callback channel when try to deregister it
callback() is blocked on sending into channel otherwise.
This commit is contained in:
		
							
								
								
									
										16
									
								
								rpc.go
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								rpc.go
									
									
									
									
									
								
							| @@ -191,10 +191,10 @@ func (l *Libvirt) listen() { | ||||
| func (l *Libvirt) callback(id uint32, res response) { | ||||
| 	l.cm.Lock() | ||||
| 	c, ok := l.callbacks[id] | ||||
| 	l.cm.Unlock() | ||||
| 	if ok { | ||||
| 		c <- res | ||||
| 	} | ||||
| 	l.cm.Unlock() | ||||
| } | ||||
|  | ||||
| // route sends incoming packets to their listeners. | ||||
| @@ -281,7 +281,19 @@ func (l *Libvirt) register(id uint32, c chan response) { | ||||
|  | ||||
| // deregister destroys a method response callback | ||||
| func (l *Libvirt) deregister(id uint32) { | ||||
| 	l.cm.Lock() | ||||
| 	lockChan := make(chan bool) | ||||
| 	go func() { | ||||
| 		l.cm.Lock() | ||||
| 		lockChan <- true | ||||
| 	}() | ||||
| Loop: | ||||
| 	for { | ||||
| 		select { | ||||
| 		case _ = <-l.callbacks[id]: | ||||
| 		case _ = <-lockChan: | ||||
| 			break Loop | ||||
| 		} | ||||
| 	} | ||||
| 	close(l.callbacks[id]) | ||||
| 	delete(l.callbacks, id) | ||||
| 	l.cm.Unlock() | ||||
|   | ||||
		Reference in New Issue
	
	Block a user