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