diff --git a/homeassistant/util/resource.py b/homeassistant/util/resource.py index 3fbbdd3a575..41982df9e50 100644 --- a/homeassistant/util/resource.py +++ b/homeassistant/util/resource.py @@ -15,10 +15,10 @@ DEFAULT_SOFT_FILE_LIMIT: Final = 2048 def set_open_file_descriptor_limit() -> None: """Set the maximum open file descriptor soft limit.""" - # Check environment variable first, then use default - soft_limit = int(os.environ.get("SOFT_FILE_LIMIT", DEFAULT_SOFT_FILE_LIMIT)) - try: + # Check environment variable first, then use default + soft_limit = int(os.environ.get("SOFT_FILE_LIMIT", DEFAULT_SOFT_FILE_LIMIT)) + # Get current limits current_soft, current_hard = resource.getrlimit(resource.RLIMIT_NOFILE) @@ -61,3 +61,5 @@ def set_open_file_descriptor_limit() -> None: except OSError as err: _LOGGER.error("Failed to set file descriptor limit: %s", err) + except ValueError as err: + _LOGGER.error("Invalid file descriptor limit value: %s", err) diff --git a/tests/util/test_resource.py b/tests/util/test_resource.py index f7e77e3a026..b81f8515de2 100644 --- a/tests/util/test_resource.py +++ b/tests/util/test_resource.py @@ -90,3 +90,18 @@ def test_set_open_file_descriptor_limit_os_error() -> None: assert ( "Failed to set file descriptor limit" in mock_logger.error.call_args[0][0] ) + + +def test_set_open_file_descriptor_limit_value_error() -> None: + """Test handling ValueError when setting file limit.""" + + with ( + patch.dict(os.environ, {"SOFT_FILE_LIMIT": "invalid_value"}), + patch("homeassistant.util.resource._LOGGER") as mock_logger, + ): + set_open_file_descriptor_limit() + + mock_logger.error.assert_called_once() + assert ( + "Invalid file descriptor limit value" in mock_logger.error.call_args[0][0] + )