Skip to content

fix: improve headless engine startup and shutdown#6222

Merged
dogancanbakir merged 1 commit intoprojectdiscovery:devfrom
fourcube:fix/slow-headless-start-and-shutdown
May 19, 2025
Merged

fix: improve headless engine startup and shutdown#6222
dogancanbakir merged 1 commit intoprojectdiscovery:devfrom
fourcube:fix/slow-headless-start-and-shutdown

Conversation

@fourcube
Copy link
Contributor

@fourcube fourcube commented May 14, 2025

Fixes #6221

Proposed changes

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.

Checklist

  • Pull request is created against the dev branch
  • All checks passed (lint, unit/integration/regression tests etc.) with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

Summary by CodeRabbit

  • Bug Fixes

    • Improved reliability of browser session cleanup, ensuring all launched browser processes are properly terminated when closing.
  • Chores

    • Updated internal process management for browser sessions to enhance stability.

Fixes projectdiscovery#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.
@auto-assign auto-assign bot requested a review from dogancanbakir May 14, 2025 14:15
@coderabbitai
Copy link
Contributor

coderabbitai bot commented May 14, 2025

Walkthrough

The changes update the browser process management in the headless engine by removing manual tracking and cleanup of Chrome process IDs. Instead, the code now uses the Chrome launcher's built-in kill functionality to manage the lifecycle of the browser process, simplifying the logic and removing unnecessary imports.

Changes

File(s) Change Summary
pkg/protocols/headless/engine/engine.go Removed manual PID tracking and cleanup; added launcher field to Browser; use launcher.Kill() to terminate browser; removed processutil import.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Browser (engine.go)
    participant Launcher

    User->>Browser (engine.go): New()
    Browser (engine.go)->>Launcher: Initialize Chrome launcher
    User->>Browser (engine.go): Close()
    Browser (engine.go)->>Launcher: Kill()
    Launcher-->>Browser (engine.go): Chrome process terminated
Loading

Assessment against linked issues

Objective Addressed Explanation
Improve headless engine startup and shutdown speed by removing enumeration of all Chrome processes (#6221)
Ensure browser lifecycle is managed without scanning unrelated Chrome processes (#6221)

Poem

A launcher now leads Chrome to start,
No more PID hunts, we’re smart!
With a single kill, we say goodbye,
Browsers close fast—oh my!
No more waiting, no more dread,
The rabbit hops ahead!
🐇✨

Tip

⚡️ Faster reviews with caching
  • CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure Review - Disable Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting under your Organization Settings.

Enjoy the performance boost—your workflow just got faster.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f52ffad and bc551fc.

📒 Files selected for processing (1)
  • pkg/protocols/headless/engine/engine.go (3 hunks)
🔇 Additional comments (4)
pkg/protocols/headless/engine/engine.go (4)

26-26: Good addition of a launcher field

Adding the launcher field to the Browser struct provides a direct reference to the Chrome launcher instance, enabling cleaner process management and shutdown.


111-111: Properly storing the launcher reference

Storing the launcher instance in the Browser struct is a good approach, ensuring the same launcher that started the browser can be used later for proper cleanup.


144-144: Improved browser shutdown mechanism

Using b.launcher.Kill() is more precise and efficient than the previous approach of manually tracking and terminating Chrome processes by PID. This change aligns with the PR objective to improve the headless engine shutdown process.


1-146: Overall implementation aligns with the PR objectives

The changes effectively improve the headless browser engine startup and shutdown by:

  1. Removing manual tracking of Chrome processes
  2. Leveraging the launcher's built-in kill functionality
  3. Simplifying the process management logic
  4. Removing unnecessary dependencies

This implementation makes the shutdown process more precise and efficient as intended.

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Member

@dogancanbakir dogancanbakir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

@dogancanbakir
Copy link
Member

ignoring test failure, fixed in #6230

@dogancanbakir dogancanbakir merged commit 160eab9 into projectdiscovery:dev May 19, 2025
18 of 19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] headless launch takes a lot of time

2 participants