{"record":{"id":"d5c2b6a92b37ca03","repo":"micro/go-micro","slug":"already-listening-on-addr","errorCode":null,"errorMessage":"already listening on ${addr}","messagePattern":"already listening on (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"transport/memory.go","lineNumber":254,"sourceCode":"\t\treturn nil, err\n\t}\n\n\taddr, err = maddr.Extract(host)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\t// if zero port then randomly assign one\n\tif len(port) > 0 && port == \"0\" {\n\t\ti := rand.Intn(20000)\n\t\tport = fmt.Sprintf(\"%d\", 10000+i)\n\t}\n\n\t// set addr with port\n\taddr = mnet.HostPort(addr, port)\n\n\tif _, ok := m.listeners[addr]; ok {\n\t\treturn nil, errors.New(\"already listening on \" + addr)\n\t}\n\n\tlistener := &memoryListener{\n\t\tlopts: options,\n\t\ttopts: m.opts,\n\t\taddr:  addr,\n\t\tconn:  make(chan *memorySocket),\n\t\texit:  make(chan bool),\n\t\tctx:   m.opts.Context,\n\t}\n\n\tm.listeners[addr] = listener\n\n\treturn listener, nil\n}\n\nfunc (m *memoryTransport) Init(opts ...Option) error {\n\tfor _, o := range opts {","sourceCodeStart":236,"sourceCodeEnd":272,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/transport/memory.go#L236-L272","documentation":"Listen refuses to bind an address that already has a memory listener registered in the same process, returning 'already listening on <addr>'. The in-memory transport keys listeners by the exact normalized host:port, so two Listen calls with the same (normalized) address collide.","triggerScenarios":"Calling Listen twice on the same addr string; two components configured with the same fixed port; Listen on 'host:0' resolving to the same port as an existing listener (rare but possible).","commonSituations":"Starting a server twice due to duplicate init code or a test suite running setup twice; multiple services in one binary configured with the same port; forgetting a previous Close when reusing addresses in tests.","solutions":["Close the existing listener before Listen on the same address, or pick a distinct port (or \":0\" for an ephemeral port)","Track created listeners (map keyed by addr) so setup code never calls Listen twice","Check the error — it names the colliding address — and correct the config so each server has a unique host:port"],"exampleFix":"// before\nm.Listen(\"127.0.0.1:8080\")\nm.Listen(\"127.0.0.1:8080\") // already listening on 127.0.0.1:8080\n// after\nl, err := m.Listen(\"127.0.0.1:8080\")\nif err == nil { defer l.Close() }\nl2, err := m.Listen(\"127.0.0.1:8081\") // distinct addr","handlingStrategy":"validation","validationCode":"if _, ok := listeners[addr]; ok {\n\treturn fmt.Errorf(\"already listening on %s\", addr)\n}","typeGuard":null,"tryCatchPattern":"l, err := m.Listen(addr)\nif err != nil && strings.HasPrefix(err.Error(), \"already listening on \") {\n\t// reuse existing listener or choose another port\n}","preventionTips":["Track your listeners and Close before re-Listen","Use \":0\" ephemeral ports in tests","Keep a registry of ports per test to avoid cross-test collisions"],"tags":["transport","memory","listen","port-conflict"],"backgroundTag":"address-already-in-use","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}