Skip to content

Comments

Disable more browser features & ensure temp file cleanup#591

Open
boidolr wants to merge 3 commits intofatihak:mainfrom
boidolr:screenshots
Open

Disable more browser features & ensure temp file cleanup#591
boidolr wants to merge 3 commits intofatihak:mainfrom
boidolr:screenshots

Conversation

@boidolr
Copy link
Contributor

@boidolr boidolr commented Feb 12, 2026

No description provided.

Copilot AI review requested due to automatic review settings February 12, 2026 17:58
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR aims to further harden the headless Chromium invocation by disabling more browser features and to simplify temporary file handling/cleanup.

Changes:

  • Switch temporary HTML/PNG handling to use NamedTemporaryFile defaults instead of manual delete=False + os.remove.
  • Add additional Chromium disablement flags to the screenshot command.
  • Simplify screenshot functions to return early and return None on error.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +94 to +99
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)
Copy link

Copilot AI Feb 12, 2026

Choose a reason for hiding this comment

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

NamedTemporaryFile defaults to delete=True and keeps the file handle open during the with block. Calling take_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 with delete=False, close it before invoking Chromium, and ensure deletion in a finally (or use a TemporaryDirectory and create a normal file inside it).

Copilot uses AI. Check for mistakes.
Comment on lines +125 to 127
with tempfile.NamedTemporaryFile(suffix=".png") as img_file:
img_file_path = img_file.name

Copy link

Copilot AI Feb 12, 2026

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.

Copilot uses AI. Check for mistakes.
Comment on lines 101 to 102
except Exception as e:
logger.error(f"Failed to take screenshot: {str(e)}")
Copy link

Copilot AI Feb 12, 2026

Choose a reason for hiding this comment

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

Consider using logger.exception("Failed to take screenshot") here so the traceback is captured in logs. This makes debugging subprocess/file-handling failures much easier than logging only str(e).

Suggested change
except Exception as e:
logger.error(f"Failed to take screenshot: {str(e)}")
except Exception:
logger.exception("Failed to take screenshot")

Copilot uses AI. Check for mistakes.
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.

1 participant