Getting bind: address already in use even after closing the connection in golang

Issue

I am building a master/slave type system in golang. When the slave(called racer) is up, it pings master telling that it is up and ready to receive data and then it starts listening on a port. The port used for pinging and listening are same but I make sure I am closing the connection after pinging. My use case is to use same port for pinging and listening but I get bind: address already in use error when I use the same port to listen. Sometimes it works sometimes it doesn’t. What I am doing wrong?

Complete Code

main.go

package main

import (
    "flag"
    "log"
    "strconv"
    "time"

    "github.com/goku321/line-racer/master"
    "github.com/goku321/line-racer/model"
    "github.com/goku321/line-racer/racer"
)

func main() {
    log.SetFlags(log.LstdFlags | log.Lshortfile)
    nodeType := flag.String("nodeType", "master", "type of node: master/racer")
    masterIP := flag.String("masterIP", "127.0.0.1", "ip address of master process")
    racers := flag.Int("racers", 2, "number of racers")
    racerID := flag.Int("racerID", 0, "unique racer id (0 <= id < number of racers")
    laps := flag.Int("laps", 10, "number of lap")
    ip := flag.String("ip", "127.0.0.1", "ip address of the node")
    port := flag.String("port", "3000", "port to use")
    flag.Parse()

_, err := strconv.ParseInt(*port, 10, 64)
if err != nil {
    log.Fatalf("error parsing port number: %s", *port)
}

if *nodeType == "master" {
    m := master.New(*ip, *port, *racers, *laps)
    m.GenerateLaps()
    go m.Listen()
    m.WaitForRacers()
    m.StartRace()
    m.PrintLaps()
} else {
    r := racer.New(*racerID, *ip, *port, *masterIP)
    r.SignalMaster(&model.Message{Source: r.IPAddr + ":" + r.Port})
    time.Sleep(time.Second*60)
    r.ListenForNewLap()
}

}

racer.go(slave)

package racer

import (
    "encoding/json"
    "log"
    "net"
    "os"
    "strconv"
    "time"

    "github.com/goku321/line-racer/model"
)

// Racer represents a racer
type Racer struct {
    ID     int
    IPAddr string
    Port   string
    Master string
    Laps   [][]model.Point
    Status string
}

// SignalMaster sends a signal to master process
// with its coordinates
func (r *Racer) SignalMaster(m *model.Message) {
    laddr, err := net.ResolveTCPAddr("tcp", r.IPAddr+":"+r.Port)
    if err != nil {
        log.Fatalf("error resolving tcp address: %s, reason: %v", r.IPAddr+":"+r.Port, err)
    }

    raddr, err := net.ResolveTCPAddr("tcp", r.Master+":3000")
    if err != nil {
        log.Fatalf("error resolving tcp address: %v", err)
    }

    for {
        conn, err := net.DialTCP("tcp", laddr, raddr)
        if err != nil {
            log.Printf("connecting to master, %v", err)
            time.Sleep(time.Second * 5)
        } else {
            m.Type = "ready"
            m.Source = strconv.Itoa(r.ID)
            m.Dest = r.Master + ":3000"
            err := json.NewEncoder(conn).Encode(&m)
            if err != nil {
                log.Fatalf("error communicating to master: %v", err)
            }
            if err = conn.Close(); err != nil {
                log.Fatal("unable to close connection")
            }
            break
        }
    }
}

// ListenForNewLap waits for master to get new coordinates
func (r *Racer) ListenForNewLap() {
    ln, err := net.Listen("tcp", ":"+r.Port)
    if err != nil {
        log.Fatalf("racer %d: %v - %v", r.ID, err, time.Now())
    }

    log.Printf("racer %d: listening on %s:%s", r.ID, r.IPAddr, r.Port)

    for {
        conn, err := ln.Accept()
        if err != nil {
            log.Fatal(err)
        }

        go handleConnection(conn, r)
    }
}

func handleConnection(conn net.Conn, r *Racer) {
    log.Printf("racer %d: new lap from master", r.ID)

    var msg model.Message
    err := json.NewDecoder(conn).Decode(&msg)
    if err != nil {
        log.Printf("racer %d: %v", r.ID, err)
    }

    // close connection here as message has already been received
    conn.Close()

    if msg.Type == "race" {
        r.Laps = append(r.Laps, msg.Coordinates)
        r.race(msg.Coordinates)
    } else if msg.Type == "kill" {
        log.Printf("racer %d: kill signal received. racer will terminate", r.ID)
        r.printLaps()
        os.Exit(0)
    }
}

Solution

Some details vary from one implementation to another, but in general, after closing a TCP connection, the underlying system (host OS, usually) has to keep it around for a little while in case of stray duplicate packets.

This connection, which is in TIME_WAIT state, can block further use of the port, making it impossible to create a new listener, unless you give the right underlying settings to the host OS. Since Go 1.5 or so, Go automatically does this on Linux: see issue 9929. What host OS are you using?

Answered By – torek

Answer Checked By – Mildred Charles (GoLangFix Admin)

Leave a Reply

Your email address will not be published.