Fix process leak in thumbnail (#257)

This commit is contained in:
Dominic Wong
2021-11-04 09:47:09 +00:00
committed by GitHub
parent 340daa09b4
commit a4594e4f6d

View File

@@ -5,6 +5,7 @@ import (
"encoding/base64"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"time"
@@ -31,6 +32,9 @@ func NewThumbnail(imageService iproto.ImageService) *Thumbnail {
func (e *Thumbnail) Screenshot(ctx context.Context, req *thumbnail.ScreenshotRequest, rsp *thumbnail.ScreenshotResponse) error {
imageName := uuid.New().String() + ".png"
imagePath := filepath.Join(screenshotPath, imageName)
defer func() {
os.Remove(imagePath)
}()
width := "800"
height := "600"
if req.Width != 0 {
@@ -39,8 +43,10 @@ func (e *Thumbnail) Screenshot(ctx context.Context, req *thumbnail.ScreenshotReq
if req.Height != 0 {
height = fmt.Sprintf("%v", req.Height)
}
outp, err := exec.Command("/usr/bin/chromium-browser", "--headless", "--window-size="+width+","+height, "--no-sandbox", "--screenshot="+imagePath, "--hide-scrollbars", req.Url).CombinedOutput()
cmd := exec.Command("/usr/bin/chromium-browser",
"--headless", "--window-size="+width+","+height, "--no-sandbox", "--screenshot="+imagePath,
"--hide-scrollbars", "--disable-setuid-sandbox", "--single-process", "--no-zygote", req.Url)
outp, err := cmd.CombinedOutput()
logger.Info(string(outp))
if err != nil {
logger.Error(string(outp) + err.Error())