修复 Windows 上 auth login Ctrl+C 后终端无法输入的问题 #246

Open
chluknight wants to merge 1 commits from chluknight/gitlink-cli:fix/powershell-auth into master
First-time contributor

##问题描述
在 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

##问题描述 在 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
chluknight force-pushed fix/powershell-auth from 9d7fadc857 to cca2989e14 2026-06-14 19:45:29 +08:00 Compare
chluknight force-pushed fix/powershell-auth from cca2989e14 to 1602593a24 2026-06-14 20:14:38 +08:00 Compare
This pull request has changes conflicting with the target branch.
  • cmd/auth/auth.go
You can also view command line instructions.

Step 1:

From your project repository, check out a new branch and test the changes.
git checkout -b chluknight-fix/powershell-auth master
git pull fix/powershell-auth

Step 2:

Merge the changes and update on Gitea.
git checkout master
git merge --no-ff chluknight-fix/powershell-auth
git push origin master
Sign in to join this conversation.
No reviewers
No Label
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: Gitlink/gitlink-cli#246
No description provided.