Repository navigation
fix(jshttp): Chromium exits on Windows with --single-process ("unexpected page type") - #29
Vinicius-Montanari wants to merge 2 commits into
Conversation
When the primary page could not be reused and both recreatePage and recreateContext failed, acquirePage returned the result of recreateBrowser as its error. A successful browser recreation therefore produced (nil, nil), and Fetch reported the misleading "unexpected page type" instead of retrying on the fresh browser. Fall through to primaryPage after a successful browser recreation and only return early when recreateBrowser itself fails.
With the Chromium build used by playwright-go v0.6100.0 (Playwright 1.61.1, Chromium 149), passing --single-process on Windows makes the browser exit on the first navigation with "Target page, context or browser has been closed". Every JS job fails, and through acquirePage this surfaced as "unexpected page type" (gosom/google-maps-scraper#322, #309). Move the launch flags into launchArgs and only add --single-process on non-Windows platforms, keeping the current behavior there.
|
I hit this on Windows 11 with v1.18.1 and traced it to scrapemate. It launches Chromium Fix: #29. With it, the same queries that returned nothing on v1.18.1 |
|
Same failure on macOS, same cause. Keeping Environment: macOS 26.6 arm64, playwright-go v0.6100.0 (driver 1.61.1), chrome-headless-shell 1228 (Chromium 149.0.7827.55), google-maps-scraper v1.18.1 with scrapemate v1.4.0. Playwright only, using the driver playwright-go downloads with the flags, UA and viewport from
google-maps-scraper v1.18.1, one query,
That's gosom/google-maps-scraper#338 on macOS. gosom/google-maps-scraper#137 looks like the Windows side. Why keep it on Linux: the flag came in with 8466b4b ("browser arguments opt", v0.8.2). The same day, google-maps-scraper added its AWS Lambda runtime on top of v0.8.2 (gosom/google-maps-scraper#96). I couldn't test Lambda, so Linux stays as it is. On top of this branch, the change is the condition, plus moving if goos == "linux" {
args = append(args, `--single-process`)
}Workaround on macOS for google-maps-scraper users who build from sourcegit clone https://github.com/gosom/google-maps-scraper && cd google-maps-scraper
git checkout v1.18.1
go mod vendor
sed -i '' '/--single-process/d' vendor/github.com/gosom/scrapemate/adapters/fetchers/jshttp/jshttp.go
go build -mod=vendor -o google_maps_scraper . |
|
Opened #30 for the inverted isClosed() noted above. Kept separate so this one stays focused. |
Problem
On Windows every JS job fails with
unexpected page type(gosom/google-maps-scraper#322, gosom/google-maps-scraper#309). Two things combine:
newBrowserlaunches Chromium with--single-process. With the Chromium build usedby playwright-go v0.6100.0 (Playwright 1.61.1, Chromium 149), this makes the browser
exit on the first navigation on Windows:
page.goto: Target page, context or browser has been closed.Reproduced with Playwright alone: the same flags fail on the first
goto, and thesame flags without
--single-processwork.recreatePageandrecreateContextfail,acquirePagereturnsnil, s.runtime.recreateBrowser(). If the browser is recreated successfully that is(nil, nil), soFetchfails its type assertion withunexpected page typeand thereal error is lost.
Changes
launchArgs(goos, disableImages)builds the flag list and only adds--single-processon non-Windows platforms. Linux and macOS keep the current flags.
acquirePagereturns early only whenrecreateBrowserfails; after a successfulrecreation it falls through to
primaryPage().TestLaunchArgs*,TestSessionSlotReturnsPageAfterBrowserRecreation(fails onmain) andTestSessionSlotReturnsErrorWhenBrowserRecreationFails.Testing
go test ./adapters/fetchers/jshttp/...passes.TestSessionSlotReturnsPageAfterBrowserRecreationfails on
mainwith "expected a page after browser recreation, got nil".go vet ./...,GOOS=linux go build ./...and golangci-lint with the repo config(
GOOS=linux): 0 issues.main(d0b51bc), 4 queries,-depth 1 -email -c 4:unexpected page type;replacepointing to this branch: 75 places in about 2 minutes and nounexpected page type. The only failed jobs were email jobs hitting company websitesthat are down or block the request.
acquirePagechange applies there too and is only covered by the unit tests above. Also not run with
-race(no cgo toolchain on the test machine). On Windows the root package tests do not build becausescrapemate_test.gousessyscall.Kill; that is pre-existing and unrelated.Noticed, not changed here
playwrightPage.isClosed()returns!p.p.IsClosed(), so it is inverted. Because of itacquirePagenever reuses%2