diff --git a/src/apify/_configuration.py b/src/apify/_configuration.py index e3dec442..4e12304c 100644 --- a/src/apify/_configuration.py +++ b/src/apify/_configuration.py @@ -5,8 +5,8 @@ from logging import getLogger from typing import Annotated, Any -from pydantic import AliasChoices, BeforeValidator, Field -from typing_extensions import deprecated +from pydantic import AliasChoices, BeforeValidator, Field, model_validator +from typing_extensions import Self, deprecated from crawlee._utils.models import timedelta_ms from crawlee._utils.urls import validate_http_url @@ -365,6 +365,19 @@ class Configuration(CrawleeConfiguration): ), ] = None + @model_validator(mode='after') + def disable_browser_sandbox_on_platform(self) -> Self: + """Disable the browser sandbox mode when running on the Apify platform. + + Running in environment where `is_at_home` is True does not benefit from browser sandbox as it is already running + in a container. It can be on the contrary undesired as the process in the container might be running as root and + this will crash chromium that was started with browser sandbox mode. + """ + if self.is_at_home and not self.disable_browser_sandbox: + self.disable_browser_sandbox = True + logger.warning('Actor is running on the Apify platform, `disable_browser_sandbox` was changed to True.') + return self + @classmethod def get_global_configuration(cls) -> Configuration: """Retrieve the global instance of the configuration. diff --git a/tests/unit/actor/test_configuration.py b/tests/unit/actor/test_configuration.py new file mode 100644 index 00000000..95f19f12 --- /dev/null +++ b/tests/unit/actor/test_configuration.py @@ -0,0 +1,21 @@ +import pytest + +from apify import Configuration + + +@pytest.mark.parametrize( + ('is_at_home', 'disable_browser_sandbox_in', 'disable_browser_sandbox_out'), + [ + (False, False, False), + (False, True, True), + (True, False, True), + (True, True, True), + ], +) +def test_disable_browser_sandbox( + *, is_at_home: bool, disable_browser_sandbox_in: bool, disable_browser_sandbox_out: bool +) -> None: + assert ( + Configuration(is_at_home=is_at_home, disable_browser_sandbox=disable_browser_sandbox_in).disable_browser_sandbox + == disable_browser_sandbox_out + )