routing: wait for topology clients to fully exit before closing ntfn chan
This commit fixes a send on closed channel panic by adding additional synchronization when cancelling the notifications for a particular topology client. We now ensure that all goroutines belonging to a particular topology client exit fully before we close the notification channel in order to avoid a panic.
This commit is contained in:
parent
286026fbb9
commit
5c45d52ab6
@ -3,6 +3,7 @@ package routing
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
@ -101,6 +102,8 @@ type topologyClient struct {
|
|||||||
// exit is a channel that is used internally by the channel router to
|
// exit is a channel that is used internally by the channel router to
|
||||||
// cancel any active un-consumed goroutine notifications.
|
// cancel any active un-consumed goroutine notifications.
|
||||||
exit chan struct{}
|
exit chan struct{}
|
||||||
|
|
||||||
|
wg sync.WaitGroup
|
||||||
}
|
}
|
||||||
|
|
||||||
// notifyTopologyChange notifies all registered clients of a new change in
|
// notifyTopologyChange notifies all registered clients of a new change in
|
||||||
@ -116,7 +119,11 @@ func (r *ChannelRouter) notifyTopologyChange(topologyDiff *TopologyChange) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, client := range r.topologyClients {
|
for _, client := range r.topologyClients {
|
||||||
|
client.wg.Add(1)
|
||||||
|
|
||||||
go func(c topologyClient) {
|
go func(c topologyClient) {
|
||||||
|
defer c.wg.Done()
|
||||||
|
|
||||||
select {
|
select {
|
||||||
|
|
||||||
// In this case we'll try to send the notification
|
// In this case we'll try to send the notification
|
||||||
|
@ -489,7 +489,10 @@ func (r *ChannelRouter) networkHandler() {
|
|||||||
if ntfnUpdate.cancel {
|
if ntfnUpdate.cancel {
|
||||||
if client, ok := r.topologyClients[ntfnUpdate.clientID]; ok {
|
if client, ok := r.topologyClients[ntfnUpdate.clientID]; ok {
|
||||||
delete(r.topologyClients, clientID)
|
delete(r.topologyClients, clientID)
|
||||||
|
|
||||||
close(client.exit)
|
close(client.exit)
|
||||||
|
client.wg.Wait()
|
||||||
|
|
||||||
close(client.ntfnChan)
|
close(client.ntfnChan)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user