-
-
Notifications
You must be signed in to change notification settings - Fork 515
Disable more browser features & ensure temp file cleanup #591
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -89,22 +89,19 @@ def compute_image_hash(image): | |||||||||
| return hashlib.sha256(img_bytes).hexdigest() | ||||||||||
|
|
||||||||||
| def take_screenshot_html(html_str, dimensions, timeout_ms=None): | ||||||||||
| image = None | ||||||||||
| try: | ||||||||||
| # Create a temporary HTML file | ||||||||||
| with tempfile.NamedTemporaryFile(suffix=".html", delete=False) as html_file: | ||||||||||
| with tempfile.NamedTemporaryFile(suffix=".html") as html_file: | ||||||||||
| html_file.write(html_str.encode("utf-8")) | ||||||||||
| html_file.flush() | ||||||||||
| html_file_path = html_file.name | ||||||||||
|
|
||||||||||
| image = take_screenshot(html_file_path, dimensions, timeout_ms) | ||||||||||
|
|
||||||||||
| # Remove html file | ||||||||||
| os.remove(html_file_path) | ||||||||||
| return take_screenshot(html_file_path, dimensions, timeout_ms) | ||||||||||
|
|
||||||||||
| except Exception as e: | ||||||||||
| logger.error(f"Failed to take screenshot: {str(e)}") | ||||||||||
|
Comment on lines
101
to
102
|
||||||||||
| except Exception as e: | |
| logger.error(f"Failed to take screenshot: {str(e)}") | |
| except Exception: | |
| logger.exception("Failed to take screenshot") |
Copilot
AI
Feb 12, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PNG output temp file is also opened and held by Python while Chromium is expected to write to the same path. On Windows this frequently fails due to file locking, and even on Unix it can be brittle. Prefer creating the output path without holding an open handle (e.g., NamedTemporaryFile(delete=False) then close immediately, or mkstemp() + close), and then remove the file in a finally after loading it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NamedTemporaryFiledefaults todelete=Trueand keeps the file handle open during thewithblock. Callingtake_screenshot(...)while the file is still open can fail on Windows because the browser process may be unable to open/read the temp HTML file. A robust pattern is to create the temp file withdelete=False, close it before invoking Chromium, and ensure deletion in afinally(or use aTemporaryDirectoryand create a normal file inside it).