Skip to content

Commit b6dd6e5

Browse files
authored
fix: add --no-sandbox flag to browser launch options (#6686)
1 parent e02ac08 commit b6dd6e5

File tree

2 files changed

+79
-24
lines changed

2 files changed

+79
-24
lines changed

src/services/browser/UrlContentFetcher.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,20 @@ export class UrlContentFetcher {
5050
return
5151
}
5252
const stats = await this.ensureChromiumExists()
53+
const args = [
54+
"--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36",
55+
"--disable-dev-shm-usage",
56+
"--disable-accelerated-2d-canvas",
57+
"--no-first-run",
58+
"--disable-gpu",
59+
"--disable-features=VizDisplayCompositor",
60+
]
61+
if (process.platform === "linux") {
62+
// Fixes network errors on Linux hosts (see https://github.com/puppeteer/puppeteer/issues/8246)
63+
args.push("--no-sandbox")
64+
}
5365
this.browser = await stats.puppeteer.launch({
54-
args: [
55-
"--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36",
56-
"--disable-dev-shm-usage",
57-
"--disable-accelerated-2d-canvas",
58-
"--no-first-run",
59-
"--disable-gpu",
60-
"--disable-features=VizDisplayCompositor",
61-
],
66+
args,
6267
executablePath: stats.executablePath,
6368
})
6469
// (latest version of puppeteer does not add headless to user agent)

src/services/browser/__tests__/UrlContentFetcher.spec.ts

Lines changed: 66 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -125,25 +125,75 @@ describe("UrlContentFetcher", () => {
125125
})
126126

127127
describe("launchBrowser", () => {
128-
it("should launch browser with correct arguments", async () => {
129-
await urlContentFetcher.launchBrowser()
130-
131-
expect(vi.mocked(PCR)).toHaveBeenCalledWith({
132-
downloadPath: path.join("/test/storage", "puppeteer"),
128+
it("should launch browser with correct arguments on non-Linux platforms", async () => {
129+
// Ensure we're not on Linux for this test
130+
const originalPlatform = process.platform
131+
Object.defineProperty(process, 'platform', {
132+
value: 'darwin' // macOS
133133
})
134134

135-
const stats = await vi.mocked(PCR).mock.results[0].value
136-
expect(stats.puppeteer.launch).toHaveBeenCalledWith({
137-
args: [
138-
"--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36",
139-
"--disable-dev-shm-usage",
140-
"--disable-accelerated-2d-canvas",
141-
"--no-first-run",
142-
"--disable-gpu",
143-
"--disable-features=VizDisplayCompositor",
144-
],
145-
executablePath: "/path/to/chromium",
135+
try {
136+
await urlContentFetcher.launchBrowser()
137+
138+
expect(vi.mocked(PCR)).toHaveBeenCalledWith({
139+
downloadPath: path.join("/test/storage", "puppeteer"),
140+
})
141+
142+
const stats = await vi.mocked(PCR).mock.results[0].value
143+
expect(stats.puppeteer.launch).toHaveBeenCalledWith({
144+
args: [
145+
"--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36",
146+
"--disable-dev-shm-usage",
147+
"--disable-accelerated-2d-canvas",
148+
"--no-first-run",
149+
"--disable-gpu",
150+
"--disable-features=VizDisplayCompositor",
151+
],
152+
executablePath: "/path/to/chromium",
153+
})
154+
} finally {
155+
// Restore original platform
156+
Object.defineProperty(process, 'platform', {
157+
value: originalPlatform
158+
})
159+
}
160+
})
161+
162+
it("should launch browser with Linux-specific arguments", async () => {
163+
// Mock process.platform to be linux
164+
const originalPlatform = process.platform
165+
Object.defineProperty(process, 'platform', {
166+
value: 'linux'
146167
})
168+
169+
try {
170+
// Create a new instance to ensure fresh state
171+
const linuxFetcher = new UrlContentFetcher(mockContext)
172+
await linuxFetcher.launchBrowser()
173+
174+
expect(vi.mocked(PCR)).toHaveBeenCalledWith({
175+
downloadPath: path.join("/test/storage", "puppeteer"),
176+
})
177+
178+
const stats = await vi.mocked(PCR).mock.results[vi.mocked(PCR).mock.results.length - 1].value
179+
expect(stats.puppeteer.launch).toHaveBeenCalledWith({
180+
args: [
181+
"--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36",
182+
"--disable-dev-shm-usage",
183+
"--disable-accelerated-2d-canvas",
184+
"--no-first-run",
185+
"--disable-gpu",
186+
"--disable-features=VizDisplayCompositor",
187+
"--no-sandbox", // Linux-specific argument
188+
],
189+
executablePath: "/path/to/chromium",
190+
})
191+
} finally {
192+
// Restore original platform
193+
Object.defineProperty(process, 'platform', {
194+
value: originalPlatform
195+
})
196+
}
147197
})
148198

149199
it("should set viewport and headers after launching", async () => {

0 commit comments

Comments
 (0)