From bc551fc3f1273103e1eec6a25ef7085378f72a6e Mon Sep 17 00:00:00 2001 From: Chris Grieger Date: Wed, 14 May 2025 16:13:44 +0200 Subject: [PATCH] fix: improve headless engine startup and shutdown Fixes #6221 Instead of enumerating all chrome processes to determine which ones need to be killed on shutdown, use the launcher.Kill() method to terminate the process that was launched for this browser instance. --- pkg/protocols/headless/engine/engine.go | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/pkg/protocols/headless/engine/engine.go b/pkg/protocols/headless/engine/engine.go index 20942c2618..0a83eef983 100644 --- a/pkg/protocols/headless/engine/engine.go +++ b/pkg/protocols/headless/engine/engine.go @@ -15,16 +15,15 @@ import ( "github.com/projectdiscovery/nuclei/v3/pkg/types" fileutil "github.com/projectdiscovery/utils/file" osutils "github.com/projectdiscovery/utils/os" - processutil "github.com/projectdiscovery/utils/process" ) // Browser is a browser structure for nuclei headless module type Browser struct { - customAgent string - tempDir string - previousPIDs map[int32]struct{} // track already running PIDs - engine *rod.Browser - options *types.Options + customAgent string + tempDir string + engine *rod.Browser + options *types.Options + launcher *launcher.Launcher // use getHTTPClient to get the http client httpClient *http.Client httpClientOnce *sync.Once @@ -36,7 +35,6 @@ func New(options *types.Options) (*Browser, error) { if err != nil { return nil, errors.Wrap(err, "could not create temporary directory") } - previousPIDs := processutil.FindProcesses(processutil.IsChromeProcess) chromeLauncher := launcher.New(). Leakless(false). @@ -110,8 +108,8 @@ func New(options *types.Options) (*Browser, error) { engine: browser, options: options, httpClientOnce: &sync.Once{}, + launcher: chromeLauncher, } - engine.previousPIDs = previousPIDs return engine, nil } @@ -143,6 +141,6 @@ func (b *Browser) getHTTPClient() (*http.Client, error) { // Close closes the browser engine func (b *Browser) Close() { b.engine.Close() + b.launcher.Kill() os.RemoveAll(b.tempDir) - processutil.CloseProcesses(processutil.IsChromeProcess, b.previousPIDs) }