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

Fix flannel hang if lease expired #1334

Merged
merged 1 commit into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions backend/extension/extension_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,12 @@ func (n *network) Run(ctx context.Context) {

for {
select {
case evtBatch := <-evts:
case evtBatch, ok := <-evts:
if !ok {
log.Infof("evts chan closed")
return
}
n.handleSubnetEvents(evtBatch)

case <-ctx.Done():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we keep this, there is a chance this for loop ends before subnet.WatchLeases goroutine terminates, then no one will read events from evts chan, subnet.WatchLeases goroutine may happen to be writting to it. And it results in subnet.WatchLeases hangs forever on receiver <- batch.

return
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions backend/ipsec/ipsec_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,13 @@ func (n *network) Run(ctx context.Context) {

for {
select {
case evtsBatch := <-evts:
case evtsBatch, ok := <-evts:
if !ok {
log.Infof("evts chan closed")
return
}
log.Info("Handling event")
n.handleSubnetEvents(evtsBatch)
case <-ctx.Done():
log.Info("Received DONE")
return
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions backend/route_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,12 @@ func (n *RouteNetwork) Run(ctx context.Context) {

for {
select {
case evtBatch := <-evts:
case evtBatch, ok := <-evts:
if !ok {
log.Infof("evts chan closed")
return
}
n.handleSubnetEvents(evtBatch)

case <-ctx.Done():
return
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions backend/route_network_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,12 @@ func (n *RouteNetwork) Run(ctx context.Context) {

for {
select {
case evtBatch := <-evts:
case evtBatch, ok := <-evts:
if !ok {
log.Infof("evts chan closed")
return
}
n.handleSubnetEvents(evtBatch)

case <-ctx.Done():
return
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions backend/udp/udp_network_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,13 @@ func (n *network) Run(ctx context.Context) {

for {
select {
case evtBatch := <-evts:
case evtBatch, ok := <-evts:
if !ok {
log.Infof("evts chan closed")
stopProxy(n.ctl)
return
}
n.processSubnetEvents(evtBatch)

case <-ctx.Done():
stopProxy(n.ctl)
return
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions backend/vxlan/vxlan_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,12 @@ func (nw *network) Run(ctx context.Context) {

for {
select {
case evtBatch := <-events:
case evtBatch, ok := <-events:
if !ok {
log.Infof("evts chan closed")
return
}
nw.handleSubnetEvents(evtBatch)

case <-ctx.Done():
return
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions backend/vxlan/vxlan_network_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,12 @@ func (nw *network) Run(ctx context.Context) {

for {
select {
case evtBatch := <-events:
case evtBatch, ok := <-events:
if !ok {
log.Infof("evts chan closed")
return
}
nw.handleSubnetEvents(evtBatch)

case <-ctx.Done():
return
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,11 @@ func MonitorLease(ctx context.Context, sm subnet.Manager, bn backend.Network, wg
log.Info("Lease renewed, new expiration: ", bn.Lease().Expiration)
dur = bn.Lease().Expiration.Sub(time.Now()) - renewMargin

case e := <-evts:
case e, ok := <-evts:
if !ok {
log.Infof("Stopped monitoring lease")
return errCanceled
}
switch e.Type {
case subnet.EventAdded:
bn.Lease().Expiration = e.Lease.Expiration
Expand All @@ -437,10 +441,6 @@ func MonitorLease(ctx context.Context, sm subnet.Manager, bn backend.Network, wg
log.Error("Lease has been revoked. Shutting down daemon.")
return errInterrupted
}

case <-ctx.Done():
log.Infof("Stopped monitoring lease")
return errCanceled
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion subnet/etcdv2/mock_etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (me *mockEtcd) sendEvent(resp *etcd.Response) {
me.events = append(me.events, resp)

// and notify watchers
for w, _ := range me.watchers {
for w := range me.watchers {
w.notifyEvent(resp)
}
}
Expand Down
4 changes: 4 additions & 0 deletions subnet/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ func WatchLeases(ctx context.Context, sm Manager, ownLease *Lease, receiver chan
res, err := sm.WatchLeases(ctx, cursor)
if err != nil {
if err == context.Canceled || err == context.DeadlineExceeded {
log.Infof("%v, close receiver chan", err)
close(receiver)
return
}

Expand Down Expand Up @@ -169,6 +171,8 @@ func WatchLease(ctx context.Context, sm Manager, sn ip.IP4Net, receiver chan Eve
wr, err := sm.WatchLease(ctx, sn, cursor)
if err != nil {
if err == context.Canceled || err == context.DeadlineExceeded {
log.Infof("%v, close receiver chan", err)
close(receiver)
return
}

Expand Down