Skip to content

Commit 3bda638

Browse files
authored
Set ulimit with Python (#19105)
* Set ulimit with python instead of in s6 startup * move to services and add env var * add comment
1 parent 687e118 commit 3bda638

File tree

4 files changed

+21
-58
lines changed

4 files changed

+21
-58
lines changed

docker/main/rootfs/etc/s6-overlay/s6-rc.d/frigate/run

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ function set_libva_version() {
5050
echo "[INFO] Preparing Frigate..."
5151
migrate_db_path
5252
set_libva_version
53-
/usr/local/ulimit/set_ulimit.sh
5453
echo "[INFO] Starting Frigate..."
5554

5655
cd /opt/frigate || echo "[ERROR] Failed to change working directory to /opt/frigate"

docker/main/rootfs/usr/local/ulimit/set_ulimit.sh

Lines changed: 0 additions & 57 deletions
This file was deleted.

frigate/app.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
from frigate.util.builtin import empty_and_close_queue
7272
from frigate.util.image import SharedMemoryFrameManager, UntrackedSharedMemory
7373
from frigate.util.object import get_camera_regions_grid
74+
from frigate.util.services import set_file_limit
7475
from frigate.version import VERSION
7576
from frigate.video import capture_camera, track_camera
7677
from frigate.watchdog import FrigateWatchdog
@@ -587,6 +588,9 @@ def start(self) -> None:
587588
# Ensure global state.
588589
self.ensure_dirs()
589590

591+
# Set soft file limits.
592+
set_file_limit()
593+
590594
# Start frigate services.
591595
self.init_camera_metrics()
592596
self.init_queues()

frigate/util/services.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import logging
66
import os
77
import re
8+
import resource
89
import signal
910
import subprocess as sp
1011
import traceback
@@ -632,3 +633,19 @@ async def calculate_duration(video: Optional[any]) -> float:
632633
result["fourcc"] = fourcc
633634

634635
return result
636+
637+
638+
def set_file_limit() -> None:
639+
# Newer versions of containerd 2.X+ impose a very low soft file limit of 1024
640+
# This applies to OSs like HA OS (see https://github.com/home-assistant/operating-system/issues/4110)
641+
# Attempt to increase this limit
642+
soft_limit = int(os.getenv("SOFT_FILE_LIMIT", "65536") or "65536")
643+
644+
current_soft, current_hard = resource.getrlimit(resource.RLIMIT_NOFILE)
645+
logger.info(f"Current file limits - Soft: {current_soft}, Hard: {current_hard}")
646+
647+
new_soft = min(soft_limit, current_hard)
648+
resource.setrlimit(resource.RLIMIT_NOFILE, (new_soft, current_hard))
649+
logger.info(
650+
f"File limit set. New soft limit: {new_soft}, Hard limit remains: {current_hard}"
651+
)

0 commit comments

Comments
 (0)