go-rod / rod

A Chrome DevTools Protocol driver for web automation and scraping.
https://go-rod.github.io
MIT License
5.25k stars 345 forks source link

当浏览器关闭时,chrome_crashpad_handler和Chromium Helper两个进程没有被关闭。 #1006

Open DC3x6 opened 8 months ago

DC3x6 commented 8 months ago

Rod Version: v0.114.5

在多协程的情况下,每个协程开始时初始化一个浏览器,浏览器关闭时,chrome_crashpad_handler和Chromium Helper两个进程没有被关闭,导致内存占满。

  func main() {
    var swg = sizedwaitgroup.New(10)
    for i := 0; i < 100; i++ {
        swg.Add()
        go func(num int) {
            defer swg.Done()
            println(num)
            browser := testrod.NBrowser()
            defer browser.MustClose()
            page := browser.Timeout(20 * time.Second).MustPage()
            navigation := page.MustWaitNavigation()
            page.MustNavigate("https://www.google.com/")
            navigation()
            time.Sleep(5 * time.Second)
        }(i)
    }
    swg.Wait()

}

headless

    launch := launcher.New().
        //Leakless(true).
        Set("disable-gpu", "true").
        Set("ignore-certificate-errors", "true").
        Set("ignore-certificate-errors", "1").
        Set("disable-crash-reporter", "true").
        Set("hide-scrollbars", "true").
        Set("mute-audio", "true").
        Set("incognito", "true").
        Headless(true).
Bin(brpath).
        MustLaunch()

browser := rod.New().NoDefaultDevice().ControlURL(launch).MustConnect().MustIgnoreCertErrors(true)
image image
github-actions[bot] commented 8 months ago

Please fix the format of your markdown:

3:91 MD026/no-trailing-punctuation Trailing punctuation in heading [Punctuation: '。']
29 MD031/blanks-around-fences Fenced code blocks should be surrounded by blank lines [Context: "```"]
29 MD040/fenced-code-language Fenced code blocks should have a language specified [Context: "```"]
44 MD031/blanks-around-fences Fenced code blocks should be surrounded by blank lines [Context: "```"]
45:1 MD033/no-inline-html Inline HTML [Element: img]
46:1 MD033/no-inline-html Inline HTML [Element: img]

generated by check-issue

ysmood commented 8 months ago

如果 leakless 都不行,那么 chrome 就非常像病毒了。你只能自己处理他的进程了,这是拿到 stdlib 的 cmd 对象的例子:

https://github.com/go-rod/rod/blob/f8e5736d35cfd3207a6a4e5cc4de32a9150837bf/lib/launcher/example_test.go#L26-L46

你使用 exec 库的话就和 rod 无关了,这部分实在难以调试和复现。

hktalent commented 5 months ago

@ysmood

util "github.com/GhostTroops/go-utils"

var fE = func() error {
        browser.MustClose()
        chromeLauncher.Cleanup()
        chromeLauncher.Kill()
        return nil
    }
    defer func() {
        if err := recover(); nil != err {
            log.Println(err)
            fE()
        }
    }()
    util.NewExit().RegClose(fE)