{"record":{"id":"3702725fedfa60f6","repo":"jeessy2/ddns-go","slug":"s-370272","errorCode":null,"errorMessage":"监听端口发生异常, 请检查端口是否被占用! %s","messagePattern":"监听端口发生异常, 请检查端口是否被占用! (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"main.go","lineNumber":211,"sourceCode":"\t// 启动静态文件服务\n\thttp.HandleFunc(\"/static/\", web.AuthAssert(staticFsFunc))\n\thttp.HandleFunc(\"/favicon.ico\", web.AuthAssert(faviconFsFunc))\n\thttp.HandleFunc(\"/login\", web.AuthAssert(web.Login))\n\thttp.HandleFunc(\"/loginFunc\", web.AuthAssert(web.LoginFunc))\n\n\thttp.HandleFunc(\"/\", web.Auth(web.Writing))\n\thttp.HandleFunc(\"/save\", web.Auth(web.Save))\n\thttp.HandleFunc(\"/setLang\", web.Auth(web.SetLang))\n\thttp.HandleFunc(\"/logs\", web.Auth(web.Logs))\n\thttp.HandleFunc(\"/clearLog\", web.Auth(web.ClearLog))\n\thttp.HandleFunc(\"/webhookTest\", web.Auth(web.WebhookTest))\n\thttp.HandleFunc(\"/logout\", web.Auth(web.Logout))\n\n\tutil.Log(\"监听 %s\", *listen)\n\n\tl, err := net.Listen(\"tcp\", *listen)\n\tif err != nil {\n\t\treturn errors.New(util.LogStr(\"监听端口发生异常, 请检查端口是否被占用! %s\", err))\n\t}\n\n\treturn http.Serve(l, nil)\n}\n\n// 以守护/分离进程方式运行（Unix 使用 setsid，Windows 使用 DETACHED_PROCESS）\nfunc runAsDaemon() error {\n\texe, err := os.Executable()\n\tif err != nil {\n\t\treturn err\n\t}\n\n\t// 过滤掉 -d 参数\n\targs := make([]string, 0, len(os.Args))\n\targs = append(args, exe)\n\tfor i := 1; i < len(os.Args); i++ {\n\t\tif os.Args[i] == \"-d\" {\n\t\t\tcontinue","sourceCodeStart":193,"sourceCodeEnd":229,"githubUrl":"https://github.com/jeessy2/ddns-go/blob/5874c2e6667c69357ab95079421131104bd3fcae/main.go#L193-L229","documentation":"runWebServer binds the HTTP listener with net.Listen(\"tcp\", *listen). If binding fails (port already in use, permission denied, invalid address), it returns this error telling the user to check whether the port is occupied. The underlying OS error is appended via %s.","triggerScenarios":"Starting the app with -listen pointing to a port already bound by another process; binding to a privileged port (<1024) without root; malformed listen address; IPv6 address not available on the host.","commonSituations":"Two instances of the app running simultaneously; another service (nginx, previous un-killed process) occupies the port; Docker/container lacks port binding permission; typo in the listen flag.","solutions":["Find and stop the process occupying the port: lsof -i :PORT or ss -tlnp, then kill it or change -listen to a free port","Run with elevated privileges if binding a privileged port, or use a port >= 1024","Check the -listen value for typos/invalid address syntax (e.g. ':8080' or '0.0.0.0:8080')","Read the wrapped OS error after the '!' for the precise cause (address in use vs permission denied)"],"exampleFix":"// before\n./app -listen :8080   # fails: port in use\n// after\nss -tlnp | grep 8080  # find offender\n./app -listen :8081   # or kill the offending process","handlingStrategy":"validation","validationCode":"// pre-flight check before starting the server\nfunc portFree(addr string) bool {\n  l, err := net.Listen(\"tcp\", addr)\n  if err != nil {\n    return false\n  }\n  l.Close()\n  return true\n}","typeGuard":"null","tryCatchPattern":"l, err := net.Listen(\"tcp\", *listen)\nif err != nil {\n  return errors.New(util.LogStr(\"监听端口发生异常, 请检查端口是否被占用! %s\", err))\n}\n// caller:\nif err := runWebServer(); err != nil {\n  if strings.Contains(err.Error(), \"监听端口发生异常\") {\n    // instruct user to free the port or change -listen\n  }\n}","preventionTips":["Before startup, run ss -tlnp / lsof -i :PORT to confirm the port is free","Use ports >= 1024 to avoid permission problems","Make the listen address configurable and document valid formats","Avoid running duplicate instances; use a PID file or systemd unit to enforce single instance"],"tags":["port-in-use","go","network","startup"],"backgroundTag":"address-already-in-use","analyzedSha":"5874c2e6667c69357ab95079421131104bd3fcae","analyzedAt":"2026-09-03T15:41:16.799Z","contentChangedAt":"2026-09-03T15:41:16.799Z","schemaVersion":2},"datasetVersion":"2026-09-11T00:17:11.886Z"}