Skip to content

fix: close browser after export#4373

Open
howenyap wants to merge 3 commits into
nusmodifications:masterfrom
howenyap:howen/fix-unclosed-browser-in-export-service
Open

fix: close browser after export#4373
howenyap wants to merge 3 commits into
nusmodifications:masterfrom
howenyap:howen/fix-unclosed-browser-in-export-service

Conversation

@howenyap
Copy link
Copy Markdown

Context

The serverless export service opens a browser but only returns the page handler

Then, cleanup only closes the page, leaving the browser handle open.

Implementation

  • Return both the browser and page handle in the open function
  • Close the browser after the export completes, this closes the page as well
image

Other Information

Disclaimer:
I found this leak with Codex Security and told @ravern about it!

@vercel
Copy link
Copy Markdown

vercel Bot commented Mar 24, 2026

@howenyap is attempting to deploy a commit to the modsbot's projects Team on Vercel.

A member of the Team first needs to authorize it.

@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 24, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 56.39%. Comparing base (988c6fd) to head (a8c3d96).
⚠️ Report is 227 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4373      +/-   ##
==========================================
+ Coverage   54.52%   56.39%   +1.86%     
==========================================
  Files         274      317      +43     
  Lines        6076     6962     +886     
  Branches     1455     1679     +224     
==========================================
+ Hits         3313     3926     +613     
- Misses       2763     3036     +273     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ravern
Copy link
Copy Markdown
Member

ravern commented Mar 24, 2026

@greptile

@howenyap
Copy link
Copy Markdown
Author

@greptile

first greptile scan kinda nervous

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Mar 24, 2026

Confidence Score: 5/5

Safe to merge — the change is a targeted cleanup of a genuine resource leak with correct finally-block placement and no regressions to the happy path.

Both changed files are small and self-contained. The fix correctly closes the browser via a try/finally in the handler, and defensively closes it inside open() if page navigation fails. browser.close() implicitly closes all associated pages, so the old explicit page.close() is safely replaced. Control-flow analysis confirms browser is always assigned before the finally block can execute.

No files require special attention.

Important Files Changed

Filename Overview
export/src/render-serverless.ts Explicit return type added to open; now returns { browser, page } instead of just page; browser is closed in a try/catch if page initialisation fails
export/src/handler.ts Destructures browser from render.open(); wraps performExport in try/finally so browser.close() is always called, replacing the old page.close() call

Sequence Diagram

sequenceDiagram
    participant H as handler.ts
    participant R as render-serverless.ts
    participant B as Browser
    participant P as Page

    H->>R: open(url)
    R->>B: puppeteer.launch()
    B-->>R: browser
    R->>P: browser.newPage()
    P-->>R: page
    R->>P: page.goto(url)
    R->>P: setViewport(page)
    R-->>H: "{ browser, page }"

    H->>H: performExport(response, page, data)
    note over H: try/finally
    H->>B: browser.close()
    B->>P: (implicitly closes page)

    note over R: If page init fails:
    R->>B: browser.close()
    R-->>H: throws error
Loading

Reviews (2): Last reviewed commit: "Merge branch 'master' into howen/fix-unc..." | Re-trigger Greptile

Comment thread export/src/handler.ts Outdated
@howenyap howenyap force-pushed the howen/fix-unclosed-browser-in-export-service branch from b79003c to 2bf0914 Compare March 24, 2026 09:08
@jloh02
Copy link
Copy Markdown
Member

jloh02 commented May 18, 2026

@greptile

@jloh02 jloh02 self-assigned this May 18, 2026
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.

3 participants