Skip to content

Commit

Permalink
Fix resource leak
Browse files Browse the repository at this point in the history
  • Loading branch information
pdf committed Oct 3, 2017
1 parent ce317cc commit 3691517
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions protocol/v2/device/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -775,19 +775,26 @@ func (d *Device) handler() {
}
select {
case res.ch <- pktResponse:
case <-res.done:
d.delSeq(seq)
res.wg.Done()
case <-timeout:
res.wg.Done()
common.Log.Warnf("Timeout returning seq %d to caller on device %d", seq, d.id)
case <-res.done:
res.wg.Done()
select {
case <-d.quitChan:
default:
d.delSeq(seq)
}
}
res.wg.Done()
}()
}
}
}

func (d *Device) addSeq() (seq uint8, res *response) {
d.Lock()
defer d.Unlock()
d.sequence++
if d.sequence == 0 {
d.sequence++
Expand All @@ -798,7 +805,6 @@ func (d *Device) addSeq() (seq uint8, res *response) {
done: make(doneChan),
}
d.responseMap[seq] = res
d.Unlock()

return seq, res
}
Expand All @@ -816,11 +822,10 @@ func (d *Device) delSeq(seq uint8) {
if !ok {
return
}
res.wg.Wait()
d.Lock()
defer d.Unlock()
close(res.ch)
delete(d.responseMap, seq)
d.Unlock()
}

func New(addr *net.UDPAddr, requestSocket *net.UDPConn, timeout *time.Duration, retryInterval *time.Duration, reliable bool, pkt *packet.Packet) (*Device, error) {
Expand Down

0 comments on commit 3691517

Please sign in to comment.