Skip to content

Commit

Permalink
Fix writing to closed channel.
Browse files Browse the repository at this point in the history
  • Loading branch information
brocaar committed Aug 30, 2018
1 parent b2452dd commit 57074db
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions internal/gateway/semtech/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,16 @@ func NewBackend(bind string, onNew, onDelete func(lorawan.EUI64) error, configur
// Close closes the backend.
func (b *Backend) Close() error {
b.Lock()
b.closed = true

log.Info("gateway: closing gateway backend")
b.closed = true
close(b.udpSendChan)

if err := b.conn.Close(); err != nil {
return errors.Wrap(err, "close udp listener error")
}

log.Info("gateway: handling last packets")

close(b.udpSendChan)
b.Unlock()
b.wg.Wait()
return nil
Expand Down Expand Up @@ -311,6 +310,13 @@ func (b *Backend) sendPackets() error {
}

func (b *Backend) handlePacket(up udpPacket) error {
b.RLock()
defer b.RUnlock()

if b.closed {
return nil
}

pt, err := packets.GetPacketType(up.data)
if err != nil {
return err
Expand Down

0 comments on commit 57074db

Please sign in to comment.