{"record":{"id":"de1b1e66972a073f","repo":"micro/go-micro","slug":"missing-service-port","errorCode":null,"errorMessage":"missing service port","messagePattern":"missing service port","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/util/mdns/zone.go","lineNumber":73,"sourceCode":"//\n// If domain, hostName, or ips is set to the zero value, then a default value\n// will be inferred from the operating system.\n//\n// TODO(reddaly): This interface may need to change to account for \"unique\n// record\" conflict rules of the mDNS protocol.  Upon startup, the server should\n// check to ensure that the instance name does not conflict with other instance\n// names, and, if required, select a new name.  There may also be conflicting\n// hostName A/AAAA records.\nfunc NewMDNSService(instance, service, domain, hostName string, port int, ips []net.IP, txt []string) (*MDNSService, error) {\n\t// Sanity check inputs\n\tif instance == \"\" {\n\t\treturn nil, fmt.Errorf(\"missing service instance name\")\n\t}\n\tif service == \"\" {\n\t\treturn nil, fmt.Errorf(\"missing service name\")\n\t}\n\tif port == 0 {\n\t\treturn nil, fmt.Errorf(\"missing service port\")\n\t}\n\n\t// Set default domain\n\tif domain == \"\" {\n\t\tdomain = \"local.\"\n\t}\n\tif err := validateFQDN(domain); err != nil {\n\t\treturn nil, fmt.Errorf(\"domain %q is not a fully-qualified domain name: %v\", domain, err)\n\t}\n\n\t// Get host information if no host is specified.\n\tif hostName == \"\" {\n\t\tvar err error\n\t\thostName, err = os.Hostname()\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"could not determine host: %v\", err)\n\t\t}\n\t\thostName = fmt.Sprintf(\"%s.\", hostName)","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/internal/util/mdns/zone.go#L55-L91","documentation":"NewMDNSService requires a non-zero port because an mDNS SRV record pointing at port 0 is meaningless for service discovery. The sanity check rejects port == 0 before any records are built.","triggerScenarios":"Calling NewMDNSService(instance, service, domain, hostName, 0, ips, txt) — e.g. the listener's port was not yet known (server not started) or an uninitialized int variable was passed.","commonSituations":"Registering the service before starting the HTTP/TCP listener so the resolved port is still 0; a config parser leaving port unset; pointer dereference or map lookup returning the zero value.","solutions":["Start your listener first, then register with the actual bound port (e.g. from listener.Addr().(*net.TCPAddr).Port)","Validate that the port is in 1–65535 before calling NewMDNSService","Fix config parsing so the port field is required and populated"],"exampleFix":"// before\nln, _ := net.Listen(\"tcp\", \":0\")\nsvc, err := NewMDNSService(\"myapp\", \"_http._tcp\", \"\", \"\", 0, ips, nil) // forgot to use ln port\n// after\nport := ln.Addr().(*net.TCPAddr).Port\nsvc, err := NewMDNSService(\"myapp\", \"_http._tcp\", \"\", \"\", port, ips, nil)","handlingStrategy":"validation","validationCode":"if port <= 0 || port > 65535 {\n\treturn errors.New(\"service port must be a positive number in 1-65535; start the listener before registering\")\n}","typeGuard":"func hasValidPort(port int) bool { return port > 0 && port <= 65535 }","tryCatchPattern":"svc, err := NewMDNSService(instance, service, domain, hostName, port, ips, txt)\nif err != nil {\n\tif strings.Contains(err.Error(), \"missing service port\") {\n\t\treturn fmt.Errorf(\"register the service after the listener is bound to a real port: %w\", err)\n\t}\n\treturn err\n}","preventionTips":["Always start your listener first and read the bound port from listener.Addr()","Never pass a raw zero-value int; load the port from config with explicit defaults","Use ephemeral ports (\":0\") only in combination with reading back the actual assigned port"],"tags":["validation","mdns","port","missing-argument"],"backgroundTag":"missing-required-argument","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}