From e49b4af6c6f932f979151ab41ec82194c39e6590 Mon Sep 17 00:00:00 2001 From: "huanyang@alauda.io" Date: Wed, 1 Apr 2026 10:31:41 +0800 Subject: [PATCH] fix: close idle HTTP connections to prevent process hanging on exit The GitLab SDK's underlying go-retryablehttp maintains idle HTTP connections. Without explicitly closing them, the process hangs after command completion waiting for connection timeouts. Added CloseIdleConnections() to GitLabClient and defer-called it in all command functions to ensure immediate cleanup on exit. Co-Authored-By: Claude Opus 4.6 --- internal/cli/cmd.go | 5 +++++ pkg/client/client.go | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/internal/cli/cmd.go b/internal/cli/cmd.go index d0ee716..f864151 100644 --- a/internal/cli/cmd.go +++ b/internal/cli/cmd.go @@ -135,6 +135,7 @@ func runUserCreate(cfg *config.CLIConfig) error { if err != nil { return err } + defer gitlabClient.CloseIdleConnections() userConfig, err := config.Load(cfg.ConfigFile) if err != nil { @@ -210,6 +211,7 @@ func runUserCleanup(cfg *config.CLIConfig) error { if err != nil { return err } + defer gitlabClient.CloseIdleConnections() userConfig, err := config.Load(cfg.ConfigFile) if err != nil { @@ -258,6 +260,7 @@ func runUserDelete(cfg *config.CLIConfig, usernames string) error { if err != nil { return err } + defer gitlabClient.CloseIdleConnections() // 解析用户名列表(以逗号分隔) usernameList := strings.Split(usernames, ",") @@ -354,6 +357,7 @@ func runUserList(cfg *config.CLIConfig, searchPrefix string) error { if err != nil { return err } + defer gitlabClient.CloseIdleConnections() log.Printf("\n正在获取用户列表...\n") if searchPrefix != "" { @@ -382,6 +386,7 @@ func runUserDeleteByPrefix(cfg *config.CLIConfig, prefix string, dryRun bool) er if err != nil { return err } + defer gitlabClient.CloseIdleConnections() log.Printf("\n正在搜索用户名以 '%s' 开头的用户...\n", prefix) diff --git a/pkg/client/client.go b/pkg/client/client.go index 5a7891a..a4c8c4a 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -24,6 +24,11 @@ func NewGitLabClient(host, token string) (*GitLabClient, error) { }, nil } +// CloseIdleConnections 关闭空闲连接,防止程序退出时卡住 +func (c *GitLabClient) CloseIdleConnections() { + c.client.HTTPClient().CloseIdleConnections() +} + // CheckAuth 检查认证和管理员权限 func (c *GitLabClient) CheckAuth() error { user, _, err := c.client.Users.CurrentUser()