Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/apify/_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/actor/test_configuration.py
Original file line number Diff line number Diff line change
@@ -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
)