修复 Windows 上 auth login Ctrl+C 后终端无法输入的问题 #246
Loading…
Reference in New Issue
No description provided.
Delete Branch "chluknight/gitlink-cli:fix/powershell-auth"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
##问题描述
在 Windows PowerShell 中运行 gitlink-cli auth login,输入用户名后在密码输入阶段按 Ctrl+C 退出,此后再次运行任何命令(包括 gitlink-cli auth login)都将无法正常输入。
##问题复现步骤
PS C:\Windows\system32> gitlink-cli auth login
Username/Email/Phone: 1
Password:
密码输错,ctrl+c退出
PS C:\Windows\system32> gitlink-cli auth login
Username/Email/Phone: 无法输入
…
##根本原因
term.ReadPassword(fd) 会将终端设为 raw 模式(关闭 ENABLE_ECHO_INPUT、ENABLE_LINE_INPUT、ENABLE_PROCESSED_INPUT),并在函数返回时通过 defer 恢复。但在 Windows 上,Ctrl+C 触发 ExitProcess() 立即终止进程,不执行任何 defer 清理,导致终端被永久留在 raw 模式。由于 PowerShell 控制台在命令之间不重建,后续进程会继承这个损坏的终端状态。
##修复方案
在 cmd/auth/auth.go 的 readPassword 函数中:
进入 term.ReadPassword 前,用 term.GetState(fd) 保存终端原始状态
注册 os.Interrupt 信号处理器,监听到 Ctrl+C 时先调用 term.Restore(fd, oldState) 恢复终端再 os.Exit(1)
正常返回时通过 done channel 通知信号 goroutine 退出,并 signal.Stop 清理
##修复后
PS C:\Windows\system32> gitlink-cli auth login
Username/Email/Phone: 1
Password:
密码输错,ctrl+c退出
PS C:\Windows\system32> gitlink-cli auth login
Username/Email/Phone: 123
9d7fadc857tocca2989e14cca2989e14to1602593a24Step 1:
From your project repository, check out a new branch and test the changes.Step 2:
Merge the changes and update on Gitea.