Skip to content

fix(stagehand): Inject --no-sandbox into Stagehand's Chromium launch when sandbox is disabled#1906

Open
vdusek wants to merge 1 commit into
masterfrom
fix/stagehand-no-sandbox
Open

fix(stagehand): Inject --no-sandbox into Stagehand's Chromium launch when sandbox is disabled#1906
vdusek wants to merge 1 commit into
masterfrom
fix/stagehand-no-sandbox

Conversation

@vdusek
Copy link
Copy Markdown
Collaborator

@vdusek vdusek commented May 20, 2026

Summary

The scheduled e2e tests for the Stagehand template have failed for several days. After PR #1900 fixed the OPENAI_API_KEY propagation, the next layer of failure surfaced — every Stagehand variant in today's run failed identically on the Apify platform with:

stagehand.InternalServerError: Error code: 500 - {'success': False, 'message': 'connect ECONNREFUSED 127.0.0.1:<port>'}

Root cause

Reproduced locally inside the apify/actor-python-playwright:3.13 image: Stagehand's BrowserLaunchOptions accepts a chromium_sandbox field, but the field is not propagated to the underlying Chromium launch. When the actor runs as root (which it does on Apify), Chromium silently refuses to start because the setuid sandbox can't initialise — and Stagehand's local SEA server then hits ECONNREFUSED when it tries to connect to the missing Chromium CDP port. The regular PlaywrightCrawler is unaffected because Playwright's Python binding does translate chromium_sandbox=False into --no-sandbox.

Fix

In StagehandBrowserPlugin, when the sandbox is disabled (config.disable_browser_sandbox=True, which Apify auto-sets), append '--no-sandbox' to browser_launch_options['args'] as an explicit workaround. Verified end-to-end in the same actor base image — the crawler now completes requests successfully.

The apify push hang from the same scheduled run is a separate flake and is addressed in #1905.

…box is disabled

Stagehand's `BrowserLaunchOptions` accepts `chromium_sandbox` but does not propagate it to the underlying Chromium launch, so containers running as root (e.g. Apify actors with `disable_browser_sandbox=True`) hit `ECONNREFUSED` on the CDP port. Inject `--no-sandbox` explicitly in `StagehandBrowserPlugin` so the `StagehandCrawler` actually starts a session on the Apify platform.
@vdusek vdusek added t-tooling Issues with this label are in the ownership of the tooling team. adhoc Ad-hoc unplanned task added during the sprint. labels May 20, 2026
@vdusek vdusek self-assigned this May 20, 2026
@github-actions github-actions Bot added this to the 141st sprint - Tooling team milestone May 20, 2026
@github-actions github-actions Bot added the tested Temporary label used only programatically for some analytics. label May 20, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented May 20, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.88%. Comparing base (e929d38) to head (f12d032).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1906      +/-   ##
==========================================
- Coverage   92.95%   92.88%   -0.07%     
==========================================
  Files         167      167              
  Lines       11709    11714       +5     
==========================================
- Hits        10884    10881       -3     
- Misses        825      833       +8     
Flag Coverage Δ
unit 92.88% <100.00%> (-0.07%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ 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.

vdusek added a commit that referenced this pull request May 20, 2026
…1905)

## Summary

Today's [scheduled
run](https://github.com/apify/crawlee-python/actions/runs/26136278735)
had the `playwright_chrome+impit+pip` variant of
`test_static_crawler_actor_at_apify` hang on `apify push` for the full
30-minute `pytest-timeout` window ([job
log](https://github.com/apify/crawlee-python/actions/runs/26136278735/job/76872087287)).
Yesterday it was a different combination — these are transient
`apify-cli` / network flakes, not a code bug.

Two test-infrastructure fixes:

- **`@pytest.mark.flaky(reruns=3)`** on
`test_static_crawler_actor_at_apify`, plus **`timeout=120`** on every
`apify-cli` `subprocess.run` (`login`, `init`, `push`). A hung CLI now
fails fast instead of burning the whole pytest-timeout window; the
reruns absorb the transient.
- **Fix `rerun=` → `reruns=` kwarg typo** in five existing
`@pytest.mark.flaky` decorators. `pytest-rerunfailures` reads `reruns`
(plural) — the typo silently meant 1 retry instead of 3 for the affected
unit tests.

The Stagehand `ECONNREFUSED` failures from the same scheduled run are a
separate bug, fixed in #1906.
@vdusek vdusek changed the title fix: Inject --no-sandbox into Stagehand's Chromium launch when sandbox is disabled fix(stagehand): Inject --no-sandbox into Stagehand's Chromium launch when sandbox is disabled May 20, 2026
@vdusek vdusek requested a review from Mantisus May 20, 2026 10:18
Copy link
Copy Markdown
Collaborator

@Mantisus Mantisus left a comment

Choose a reason for hiding this comment

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

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

adhoc Ad-hoc unplanned task added during the sprint. t-tooling Issues with this label are in the ownership of the tooling team. tested Temporary label used only programatically for some analytics.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants