Skip to content

Windows11 x64下无法启动服务,报端口占用实际是自己占用,尝试0.3.0和0.4.0均如此,更低版本未试。 #20

Closed

Description

@YujiaCheng1996
nohup: ignoring input
2026-11-09T20:55:38.537+0800	INFO	cli/serve.go:37	starting sctl serve	{"version": "0.4.0"}
2026-11-09T20:55:38.564+0800	INFO	daemon/component.go:81	bridge daemon is listening	{"address": "127.0.0.1:8643", "jsonrpc": "2.0"}
2026-11-09T20:55:38.566+0800	INFO	cago@v0.0.0-20260715061808-cfde8cba47ae/registry.go:103	sctl is starting...
{"level":"error","ts":"2026-11-09T20:15:27.017+0800","caller":"daemon/component.go:77","msg":"failed to bind the websocket listener","address":"127.0.0.1:8643","error":"listen tcp 127.0.0.1:8643: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted."}
{"level":"error","ts":"2026-11-09T20:33:41.206+0800","caller":"daemon/component.go:77","msg":"failed to bind the websocket listener","address":"127.0.0.1:8643","error":"listen tcp 127.0.0.1:8643: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted."}
{"level":"error","ts":"2026-11-09T20:42:41.688+0800","caller":"daemon/component.go:77","msg":"failed to bind the websocket listener","address":"127.0.0.1:8643","error":"listen tcp 127.0.0.1:8643: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted."}
{"level":"error","ts":"2026-11-09T20:51:40.755+0800","caller":"daemon/component.go:77","msg":"failed to bind the websocket listener","address":"127.0.0.1:8643","error":"listen tcp 127.0.0.1:8643: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted."}

{"level":"info","ts":"2026-11-09T20:42:41.667+0800","caller":"cli/serve.go:37","msg":"starting sctl serve","version":"0.3.0"}
{"level":"error","ts":"2026-11-09T20:42:41.688+0800","caller":"daemon/component.go:77","msg":"failed to bind the websocket listener","address":"127.0.0.1:8643","error":"listen tcp 127.0.0.1:8643: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted."}
{"level":"info","ts":"2026-11-09T20:49:56.913+0800","caller":"cli/serve.go:37","msg":"starting sctl serve","version":"0.4.0"}
{"level":"info","ts":"2026-11-09T20:49:56.938+0800","caller":"daemon/component.go:81","msg":"bridge daemon is listening","address":"127.0.0.1:8643","jsonrpc":"2.0"}
{"level":"info","ts":"2026-11-09T20:49:56.939+0800","caller":"cago@v0.0.0-20260715061808-cfde8cba47ae/registry.go:103","msg":"sctl is starting..."}
{"level":"info","ts":"2026-11-09T20:51:40.732+0800","caller":"cli/serve.go:37","msg":"starting sctl serve","version":"0.4.0"}
{"level":"error","ts":"2026-11-09T20:51:40.755+0800","caller":"daemon/component.go:77","msg":"failed to bind the websocket listener","address":"127.0.0.1:8643","error":"listen tcp 127.0.0.1:8643: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted."}
{"level":"info","ts":"2026-11-09T20:55:38.537+0800","caller":"cli/serve.go:37","msg":"starting sctl serve","version":"0.4.0"}
{"level":"info","ts":"2026-11-09T20:55:38.564+0800","caller":"daemon/component.go:81","msg":"bridge daemon is listening","address":"127.0.0.1:8643","jsonrpc":"2.0"}
{"level":"info","ts":"2026-11-09T20:55:38.566+0800","caller":"cago@v0.0.0-20260715061808-cfde8cba47ae/registry.go:103","msg":"sctl is starting..."}
2026-11-10T17:13:40.992+0800    INFO    cli/serve.go:37 starting sctl serve     {"version": "0.4.0"}
2026-11-10T17:13:41.011+0800    ERROR   daemon/component.go:77  failed to bind the websocket listener   {"address": "127.0.0.1:8643", "error": "listen tcp 127.0.0.1:8643: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted."}
panic: start component error: *daemon.daemonComponent listen tcp 127.0.0.1:8643: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted.

goroutine 1 [running]:
github.com/cago-frame/cago.(*Cago).RegistryCancel(0x3399549e1ba8, {0x7ff6fdb6a6b0, 0x3399546103c0})
        github.com/cago-frame/cago@v0.0.0-20260715061808-cfde8cba47ae/registry.go:59 +0x22e
github.com/scriptscat/sctl/internal/cli.newServeCmd.func1(0x3399548d6908?, {0x7ff6fe0b04a0?, 0x0?, 0x7ff6fdaf7d51?})
        github.com/scriptscat/sctl/internal/cli/serve.go:39 +0x2c5
github.com/spf13/cobra.(*Command).execute(0x3399548d6908, {0x7ff6fe0b04a0, 0x0, 0x0})
        github.com/spf13/cobra@v1.10.2/command.go:1015 +0xb14
github.com/spf13/cobra.(*Command).ExecuteC(0x3399548d6608)
        github.com/spf13/cobra@v1.10.2/command.go:1148 +0x465
github.com/spf13/cobra.(*Command).Execute(0x3399545e9ef8?)
        github.com/spf13/cobra@v1.10.2/command.go:1071 +0x13
main.main()
        github.com/scriptscat/sctl/cmd/sctl/main.go:15 +0x1c

serve 和 service install 均报错,使用connect提示未启动,检测不到。

error: cannot connect to the sctl daemon; start it with `sctl serve`

查询占用,只有 sctl.exe 占用端口,结束后即释放,但启动后又提示占用。

Activity

  1. JsonSong89 commented on Nov 11, 2026

    @JsonSong89

    补一份 100% 可复现的同类场景 + 根因定位(Windows 10、sctl 0.4.0)。

    复现步骤

    1. sctl service install → 注册 Task Scheduler 用户级任务 \sctl,后台 daemon 起来(ParentProcessId 是任务计划程序宿主)。
    2. 此时再在终端里执行 sctl serve:
    2026-11-11T13:43:05.027+0800  INFO   cli/serve.go:37  starting sctl serve  {"version": "0.4.0"}
    2026-11-11T13:43:05.036+0800  ERROR  daemon/component.go:77  failed to bind the websocket listener  {"address": "127.0.0.1:8643", "error": "listen tcp 127.0.0.1:8643: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted."}
    panic: start component error: *daemon.daemonComponent listen tcp 127.0.0.1:8643: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted.
    
    goroutine 1 [running]:
    github.com/cago-frame/cago.(*Cago).RegistryCancel(...)
            github.com/cago-frame/cago@v0.0.0-20260715061808-cfde8cba47ae/registry.go:59 +0x22e
    github.com/scriptscat/sctl/internal/cli.newServeCmd.func1(...)
            github.com/scriptscat/sctl/internal/cli/serve.go:39 +0x2c5
    main.main()
            github.com/scriptscat/sctl/cmd/sctl/main.go:15 +0x1c
    

    根因

    端口被自己的另一个实例占着,不是残留进程也不是别的软件:

    Get-NetTCPConnection -LocalPort 8643 -State Listen
    # → OwningProcess = <sctl.exe>
    Get-CimInstance Win32_Process -Filter "Name='sctl.exe'"
    # → "C:\Users\<user>\AppData\Local\sctl\bin\sctl.exe" --listen-address 127.0.0.1:8643 serve --service

    也就是 sctl service install 启动的服务实例。此时 sctl service status / sctl status 都是正常的(daemon 0.4.0 running、浏览器在线),只有第二个 serve 抢不到端口。

    这条路径特别容易被触发:装了服务之后,用户按 sctl connect 的提示(start it with sctl serve)或按旧习惯手动起 daemon,就会直接吃一坨 goroutine 栈。

    建议

    1. serve 绑定失败时不要 panic:先用 control API 探测该地址——能拿到 sctl 的应答就打印
      daemon already running at 127.0.0.1:8643 (service \sctl) 并提示 sctl service status|restart,正常退出或返回明确的非零码;
      端口被非 sctl 进程占用时给出 address already in use (pid N, name),exit 1。
    2. sctl service install 时若已有 daemon 在跑,先接管/重启,而不是留下「服务已装、连不上」的中间态。
    3. 结构上:panic 发生在 cago.New(...).Start() 内部,newServeCmd 的 RunE(serve.go:38-40)根本接不到,建议把 bind 失败转成 RunE 返回的 error,main 统一按错误码退出。

    环境:Windows 10 Pro / sctl 0.4.0(commit cba61950, go1.26.3 windows/amd64)/ Chrome 152 + sctl Browser 扩展 0.2.0。

  2. CodFrm commented on Nov 12, 2026

    @CodFrm
    Member

    感谢反馈,我看看

  3. CodFrm commented on Nov 12, 2026

    @CodFrm
    Member

    感谢两位的反馈和定位,修复在 #21。

    根因:sctl serve 绑定端口这一步原来放在 cago 组件的启动阶段,cago 遇到组件启动失败会直接 panic。所以只要端口被占,不管占着的是 sctl service 起的 daemon 还是别的程序,都会打印一屏 goroutine 栈,退出码 2。这个问题跟平台无关,macOS、Windows、Linux 上都能稳定复现。

    修复后:serve 先绑定端口,绑定失败时用健康检查判断是谁占着:

    • 另一个 sctl daemon(比如服务起的那个)占着时:
      error: an sctl daemon is already running at 127.0.0.1:8643, so there is nothing to start; sctl statusshows it, andsctl service status tells whether it is the background service
    • 别的程序占着时:
      error: cannot listen on 127.0.0.1:8643 (free it or pick another address with --listen-address): listen tcp 127.0.0.1:8643: bind: …

    两种情况退出码都是 3,原因同时写进日志文件。保持非零退出码是有意的:服务管理器会继续重试,等前台的 daemon 退出后,服务能自动接上。已经在 Windows 11(Task Scheduler 服务占着 8643 时手动 sctl serve)、macOS 和 Linux(systemd 用户服务)上实机验证过。

    关于建议里的第 2 点:现有逻辑已经处理了。服务还没装、但已经有 daemon 在跑时,sctl service install 会直接拒绝;服务已经装过时,会先停掉旧实例再重新启动。

    修复会在下个版本发布,PR 合并后这个 issue 会自动关闭。原帖提到的「端口被 sctl.exe 占着,但 sctl connect 提示 daemon 没启动」这一点我没能复现。如果升级后还遇到,麻烦附上 sctl status 和 sctl service status 的输出重新打开 issue,我们再看。

  4. YujiaCheng1996 commented on Nov 13, 2026

    @YujiaCheng1996
    Author

    如果仅仅是重复启动服务报端口号占用不是问题,只是根本无法启动成功,0.4.0在macos下测试没有任何问题,仅在windows11下无法成功启动。
    尝试一个终端中启动sctl

    sctl serve
    2026-11-13T15:12:15.189+0800    INFO    cli/serve.go:37 starting sctl serve     {"version": "0.4.0"}
    2026-11-13T15:12:15.211+0800    INFO    daemon/component.go:81  bridge daemon is listening      {"address": "127.0.0.1:8643", "jsonrpc": "2.0"}
    2026-11-13T15:12:15.213+0800    INFO    cago@v0.0.0-20260715061808-cfde8cba47ae/registry.go:103 sctl is starting...
    

    立即在另一个终端中查询端口占用和状态

    netstat -ano | grep 8643
      TCP    127.0.0.1:8643         0.0.0.0:0              LISTENING       5092
    sctl status
    daemon is not running
    

    再次查询端口已经不再占用,但任务管理器中5092的进程仍在运行,第一个终端中仍然卡在starting,没有报错和退出

    netstat -ano | grep 8643
    tasklist | grep 5092
    sctl.exe                      5092 Console                    5     16,584 K
    

    sctl.log内容如下,没有其他log

    {"level":"info","ts":"2026-11-13T15:12:15.189+0800","caller":"cli/serve.go:37","msg":"starting sctl serve","version":"0.4.0"}
    {"level":"info","ts":"2026-11-13T15:12:15.211+0800","caller":"daemon/component.go:81","msg":"bridge daemon is listening","address":"127.0.0.1:8643","jsonrpc":"2.0"}
    {"level":"info","ts":"2026-11-13T15:12:15.213+0800","caller":"cago@v0.0.0-20260715061808-cfde8cba47ae/registry.go:103","msg":"sctl is starting..."}
    
    

    最后多次测试,是被adguard影响,将sctl设置为不通过adguard路由即可,macos的adguard/little snitch不影响sctl工作。
    抱歉,这个issue可以关闭了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions