Skip to content

Commit 0ef1e0f

Browse files
authored
chore: Disable browser sandbox when running on Apify platform (#441)
Disable browser sandbox when running on Apify platform. Fixes apify/crawlee-python#1110 Integration tests will be added in the crawlee-python repository.
1 parent eb4c8e4 commit 0ef1e0f

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/apify/_configuration.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
from logging import getLogger
66
from typing import Annotated, Any
77

8-
from pydantic import AliasChoices, BeforeValidator, Field
9-
from typing_extensions import deprecated
8+
from pydantic import AliasChoices, BeforeValidator, Field, model_validator
9+
from typing_extensions import Self, deprecated
1010

1111
from crawlee._utils.models import timedelta_ms
1212
from crawlee._utils.urls import validate_http_url
@@ -365,6 +365,19 @@ class Configuration(CrawleeConfiguration):
365365
),
366366
] = None
367367

368+
@model_validator(mode='after')
369+
def disable_browser_sandbox_on_platform(self) -> Self:
370+
"""Disable the browser sandbox mode when running on the Apify platform.
371+
372+
Running in environment where `is_at_home` is True does not benefit from browser sandbox as it is already running
373+
in a container. It can be on the contrary undesired as the process in the container might be running as root and
374+
this will crash chromium that was started with browser sandbox mode.
375+
"""
376+
if self.is_at_home and not self.disable_browser_sandbox:
377+
self.disable_browser_sandbox = True
378+
logger.warning('Actor is running on the Apify platform, `disable_browser_sandbox` was changed to True.')
379+
return self
380+
368381
@classmethod
369382
def get_global_configuration(cls) -> Configuration:
370383
"""Retrieve the global instance of the configuration.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import pytest
2+
3+
from apify import Configuration
4+
5+
6+
@pytest.mark.parametrize(
7+
('is_at_home', 'disable_browser_sandbox_in', 'disable_browser_sandbox_out'),
8+
[
9+
(False, False, False),
10+
(False, True, True),
11+
(True, False, True),
12+
(True, True, True),
13+
],
14+
)
15+
def test_disable_browser_sandbox(
16+
*, is_at_home: bool, disable_browser_sandbox_in: bool, disable_browser_sandbox_out: bool
17+
) -> None:
18+
assert (
19+
Configuration(is_at_home=is_at_home, disable_browser_sandbox=disable_browser_sandbox_in).disable_browser_sandbox
20+
== disable_browser_sandbox_out
21+
)

0 commit comments

Comments
 (0)