Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

程序里监听退出信号想一旦监听退出就关闭浏览器 导致奔溃 #550

Closed
ghost opened this issue Mar 4, 2022 · 7 comments
Labels
question Questions related to rod

Comments

@ghost
Copy link

ghost commented Mar 4, 2022

Rod Version: v0.102.1

The code to demonstrate your question

package main

import (
	"crypto/md5"
	"math/rand"
	"fmt"
	"strings"
	"time"
	"log"
	"syscall"
	"os"
	"os/signal"
	"strconv"
	"github.com/go-rod/rod"
	"github.com/go-rod/rod/lib/launcher"
	_"github.com/shirou/gopsutil/process"
	"github.com/gookit/goutil/dump"
	"github.com/go-rod/stealth"
)

func main() {
	sigc := make(chan os.Signal, 1)
	signal.Notify(sigc,
		syscall.SIGHUP,
		syscall.SIGINT,
		syscall.SIGTERM,
		syscall.SIGQUIT)

	u := launcher.New().
	Proxy("127.0.0.1:10809").
	Delete("use-mock-keychain").
	UserDataDir("path").
	Headless(false).
	MustLaunch()
	browser := rod.New().ControlURL(u).MustConnect()
	defer browser.MustClose()
	fmt.Printf("js: %x\n\n", md5.Sum([]byte(stealth.JS)))
	page := stealth.MustPage(browser)

	go func() {
		_ = <-sigc
		fmt.Println("ctrl+c pressed")
		page.Close()
		os.Exit(0)
	}()

	page.MustNavigate("https://ifconfig.me/forwarded")
	page.MustWaitLoad()
	dump.P(page.MustElement("body").MustText())

	for { 
		time.Sleep(5 * time.Second)
	} 
	page.Close()
}

func other(){
	os.Exit(0)
	log.Println("a")
	dump.P(strings.Contains("a", "c"),strconv.Itoa(90),rand.Intn(90))
}

What have you tried to solve the question

reinstall gorod but failed

@ghost ghost added the question Questions related to rod label Mar 4, 2022
@rod-robot
Copy link

Please add a valid **Rod Version:** v0.0.0 to your issue. Current version is v0.103.0
generated by check-issue

@ghost
Copy link
Author

ghost commented Mar 4, 2022

error:

panic: [launcher] Failed to get the debug url: �������е��������Ự�д򿪡�


goroutine 1 [running]:
github.com/go-rod/rod/lib/utils.glob..func2(0x35a7a0, 0x11a86000)
        C:/Program Files/go/gopath/pkg/mod/github.com/go-rod/[email protected]/lib/utils/utils.go:57 +0x29
github.com/go-rod/rod/lib/utils.E(...)
        C:/Program Files/go/gopath/pkg/mod/github.com/go-rod/[email protected]/lib/utils/utils.go:63
github.com/go-rod/rod/lib/launcher.(*Launcher).MustLaunch(0x11966d00, 0x3d8900, 0x11966d00)
        C:/Program Files/go/gopath/pkg/mod/github.com/go-rod/[email protected]/lib/launcher/launcher.go:329 +0xa1
main.main()
        C:/Program Files/go/gopath/src/ge/gs.go:34 +0x188
exit status 2

@ghost
Copy link
Author

ghost commented Mar 4, 2022

go mod file:

module ge

go 1.16

require (
	github.com/go-rod/rod v0.102.1
	github.com/go-rod/stealth v0.4.4
	github.com/gookit/goutil v0.4.4
	github.com/shirou/gopsutil v2.21.11+incompatible
	github.com/tklauser/go-sysconf v0.3.9 // indirect
	github.com/yusufpapurcu/wmi v1.2.2 // indirect
)

@ghost
Copy link
Author

ghost commented Mar 4, 2022

#526 #345 #293

@ghost ghost changed the title 在里监听退出信号想一旦监听退出就关闭浏览器 导致奔溃 程序里监听退出信号想一旦监听退出就关闭浏览器 导致奔溃 Mar 4, 2022
@ysmood
Copy link
Member

ysmood commented Mar 4, 2022

能简化下代码并描述下操作步骤吗?

@ghost
Copy link
Author

ghost commented Mar 5, 2022

能简化下代码并描述下操作步骤吗?

想实现这个功能,如果程序退出,不管是执行完毕退出退出还是异常退出,也把chromium退出干净。

我简化了一下,监听退出信号,如果监听到就执行 page.Close() 然后就退出,其余是正常的rod命令

@ysmood
Copy link
Member

ysmood commented Mar 11, 2022

当 golang 进程退出时 rod 会自动 kill 浏览器进程,这个可以看看 leakless 部分的文档说明。你可以删除所有 signal 的处理代码,也无需执行 page.Close()browser.MustClose()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Questions related to rod
Projects
None yet
Development

No branches or pull requests

2 participants