Skip to content

Commit

Permalink
Merge pull request networkservicemesh#342 from Bolodya1997/qfix/gorou…
Browse files Browse the repository at this point in the history
…tines-leak

[qfix] Fix goroutine leak in ipaddress
  • Loading branch information
denis-tingaikin authored Oct 5, 2021
2 parents 2c2b60a + 97967f1 commit ff54f72
Showing 1 changed file with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,18 @@ func create(ctx context.Context, conn *networkservice.Connection, isClient bool)

ch := make(chan netlink.AddrUpdate)
done := make(chan struct{})
defer close(done)

if err := netlink.AddrSubscribeAt(targetNetNS, ch, done); err != nil {
return errors.Wrapf(err, "failed to subscribe for interface address updates")
}

defer func() {
close(done)
// `ch` should be fully read after the `done` close to prevent goroutine leak in `netlink.AddrSubscribeAt`
for range ch {
}
}()

for _, ipNet := range ipNets {
now := time.Now()
addr := &netlink.Addr{
Expand Down Expand Up @@ -131,7 +139,10 @@ func waitForIPNets(ctx context.Context, ch chan netlink.AddrUpdate, l netlink.Li
select {
case <-ctx.Done():
return errors.Wrapf(ctx.Err(), "timeout waiting for update to add ip addresses %s to %s (type: %s)", ipNets, l.Attrs().Name, l.Type())
case update := <-ch:
case update, ok := <-ch:
if !ok {
return errors.Errorf("failed to receive update to add ip addresses %s to %s (type: %s)", ipNets, l.Attrs().Name, l.Type())
}
if update.LinkIndex == l.Attrs().Index {
for i := range ipNets {
if update.LinkAddress.IP.Equal(ipNets[i].IP) && update.Flags&unix.IFA_F_TENTATIVE == 0 {
Expand Down

0 comments on commit ff54f72

Please sign in to comment.