Skip to content
代码片段 群组 项目
未验证 提交 c9e7fbb9 编辑于 作者: Stan Hu's avatar Stan Hu
浏览文件

Ensure Workhorse log writers are closed to avoid Goroutine leaks

We've seen in production the number of Goroutines in Workhorse exceed
4000. The pprof output suggests that many of these came from the
logrus Goroutines lingering.

When Workhorse forks `gitlab-zip-cat` and `gitlab-zip-metadata` it
attempts to log all stderr via a logrus Writer. logrus launches a
Goroutine to listen from a pipe. We should ensure this Writer is
closed so that the Goroutines shut down as soon as possible.

Changelog: fixed
上级 6ffc9a77
加载中
...@@ -71,12 +71,19 @@ func unpackFileFromZip(ctx context.Context, archivePath, encodedFilename string, ...@@ -71,12 +71,19 @@ func unpackFileFromZip(ctx context.Context, archivePath, encodedFilename string,
return err return err
} }
logWriter := log.ContextLogger(ctx).Writer()
defer func() {
if closeErr := logWriter.Close(); closeErr != nil {
log.ContextLogger(ctx).WithError(closeErr).Error("failed to close gitlab-zip-cat log writer")
}
}()
catFile := exec.Command("gitlab-zip-cat") catFile := exec.Command("gitlab-zip-cat")
catFile.Env = append(os.Environ(), catFile.Env = append(os.Environ(),
"ARCHIVE_PATH="+archivePath, "ARCHIVE_PATH="+archivePath,
"ENCODED_FILE_NAME="+encodedFilename, "ENCODED_FILE_NAME="+encodedFilename,
) )
catFile.Stderr = log.ContextLogger(ctx).Writer() catFile.Stderr = logWriter
catFile.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} catFile.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
stdout, err := catFile.StdoutPipe() stdout, err := catFile.StdoutPipe()
if err != nil { if err != nil {
......
...@@ -72,8 +72,15 @@ func (a *artifactsUploadProcessor) generateMetadataFromZip(ctx context.Context, ...@@ -72,8 +72,15 @@ func (a *artifactsUploadProcessor) generateMetadataFromZip(ctx context.Context,
fileName = file.RemoteURL fileName = file.RemoteURL
} }
logWriter := log.ContextLogger(ctx).Writer()
defer func() {
if closeErr := logWriter.Close(); closeErr != nil {
log.ContextLogger(ctx).WithError(closeErr).Error("failed to close gitlab-zip-metadata log writer")
}
}()
zipMd := exec.CommandContext(ctx, "gitlab-zip-metadata", "-zip-reader-limit", strconv.FormatInt(readerLimit, 10), fileName) zipMd := exec.CommandContext(ctx, "gitlab-zip-metadata", "-zip-reader-limit", strconv.FormatInt(readerLimit, 10), fileName)
zipMd.Stderr = log.ContextLogger(ctx).Writer() zipMd.Stderr = logWriter
zipMd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} zipMd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
zipMdOut, err := zipMd.StdoutPipe() zipMdOut, err := zipMd.StdoutPipe()
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册